Snippets

Stefan Glienke CreateDictionary folding

Created by Stefan Glienke

File snippet.txt Added

  • Ignore whitespace
  • Hide word diff
+class function TCollections.CreateDictionary<TKey, TValue>(capacity: Integer;
+  const keyComparer: IEqualityComparer<TKey>;
+  const valueComparer: IEqualityComparer<TValue>;
+  ownerships: TDictionaryOwnerships): IDictionary<TKey, TValue>;
+begin
+{$IFDEF DELPHIXE7_UP}
+  if (GetTypeKind(TKey) in FoldedTypeKinds) and (GetTypeKind(TValue) in FoldedTypeKinds) then
+  if not IsManagedType(TKey) then
+    case SizeOf(TKey) of
+      1:
+        if not IsManagedType(TValue) then
+          case SizeOf(TValue) of
+            1: CreateDictionaryByteByte(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            2: CreateDictionaryByteWord(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            4: CreateDictionaryByteCardinal(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+//            8:
+          end
+        else
+          case GetTypeKind(TValue) of
+            tkUString: CreateDictionaryByteString(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            tkInterface: CreateDictionaryByteInterface(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+          end;
+      2:
+        if not IsManagedType(TValue) then
+          case SizeOf(TValue) of
+            1: CreateDictionaryWordByte(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            2: CreateDictionaryWordWord(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            4: CreateDictionaryWordCardinal(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+//            8:
+          end
+        else
+          case GetTypeKind(TValue) of
+            tkUString: CreateDictionaryWordString(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            tkInterface: CreateDictionaryWordInterface(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+          end;
+      4:
+        if not IsManagedType(TValue) then
+          case SizeOf(TValue) of
+            1: CreateDictionaryCardinalByte(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            2: CreateDictionaryCardinalWord(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            4: CreateDictionaryCardinalCardinal(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+//            8:
+          end
+        else
+          case GetTypeKind(TValue) of
+            tkUString: CreateDictionaryCardinalString(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            tkInterface: CreateDictionaryCardinalInterface(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+          end;
+//      8:
+    end
+  else
+    case GetTypeKind(TKey) of
+      tkUString:
+        if not IsManagedType(TValue) then
+          case SizeOf(TValue) of
+            1: CreateDictionaryStringByte(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            2: CreateDictionaryStringWord(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            4: CreateDictionaryStringCardinal(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+//            8:
+          end
+        else
+          case GetTypeKind(TValue) of
+            tkUString: CreateDictionaryStringString(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            tkInterface: CreateDictionaryStringInterface(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+          end;
+      tkInterface:
+        if not IsManagedType(TValue) then
+          case SizeOf(TValue) of
+            1: CreateDictionaryInterfaceByte(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            2: CreateDictionaryInterfaceWord(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            4: CreateDictionaryInterfaceCardinal(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+//            8:
+          end
+        else
+          case GetTypeKind(TValue) of
+            tkUString: CreateDictionaryInterfaceString(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+            tkInterface: CreateDictionaryInterfaceInterface(capacity, Pointer(keyComparer), Pointer(valueComparer), ownerships, result, TypeInfo(TKey), TypeInfo(TValue));
+          end;
+    end
+  else
+{$ENDIF}
+  Result := TDictionary<TKey, TValue>.Create(capacity, keyComparer, valueComparer, ownerships);
+end;
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.