Hi Code Typhon community!
I'm pleased to announce the generic library, compatible with Delphi Generics.Collections (almost
).
Homepage
code.google.com/p/fpc-generics-collections/
ZIP
code.google.com/p/fpc-generics-collections/downloads/list
SVN
fpc-generics-collections.googlecode.com/svn/trunk/
modules
- Generics.Defaults
- Generics.Collections
- Generics.Helpers
- Generics.Hashes
compatible classes
- TEnumerator<T>
- TEnumerable<T>
- TList<T>
- TQueue<T>
- TStack<T>
- TDictionary<TKey, TValue>
- TObjectList<T: TObject>
- TObjectStack<T: TObject>
- TObjectDictionary<TKey, TValue>
renamed (because compiler limations/bug)
- TArrayHelper<T> instead of TArray<T>
additional classes
- TDictionary<TKey, TValue, TProbeSequence, THash, THashFactory>
TObjectDictionary<TKey, TValue, TProbeSequence, THash, THashFactory>
TKey - key, TValue - value, THashFactory - hash factory
TProbeSequence - TProbeSequence is special record for open addresing. To choose from a predefined:
TLinearProbing, TQuadraticProbing, TDoubleHashing,
TFastLinearProbing, TFastQuadraticProbing, TFastDoubleHashing (fast version is without "mod")
more info at en.wikipedia.org/wiki/Open_addressing
THash - type for hash (to reduce size of dictionary - can be UInt8, UInt16, UInt32 etc.)
For example, Delphi TDictionary version in this library is defined as:
TDictionary<TKey, TValue> = class(TDictionary<TKey, TValue, TFastLinearProbing, UInt32, TDelphiHashFactory>);
- TDictionaryList<TKey, TValue, TIndex, THash, THashFactory>
class, that combines a TList and TDictionary
TKey - key, TValue - value, THashFactory - hash factory
TIndex - type for index (to reduce size of dictionary - can be UInt8, UInt16, UInt32 etc.)
THash - type for hash (to reduce size of dictionary - can be UInt8, UInt16, UInt32 etc.)
- Similar to TList, TDictionary and TDictionaryList, but uses normal operators instead of IEqualityComparer.
- TFastList
- TFastDictionary
- TFastDictionaryList
- TFastArrayHelper<T> - similar rules as above
not implemented
TThreadedQueue<T>
FAQ
1. How to use record as Key or Value in dictionary?
You need to wait for some compiler magic ( system functions GetReferenceToValue and GetValueSize described bolow)
or add two methods to record:
TRecord = record
(* ... *)
function GetValueSize: Integer; inline;
function GetReferenceToValue: Pointer; inline;
end;
2. How to use Pointer or some custom type as Key or Value in dictionary?
You need to wait for some compiler magic ( system functions GetReferenceToValue and GetValueSize described bolow)
or use TValueHelper:
uses
Generics.Collections, Generics.Helpers;
(* ... *)
type
TValueHelperPointer = TValueHelper<Pointer>;
var
d: TDictionary<TValueHelperPointer, string>;
begin
(* ... *)
d.Add(nil, 'foo');
TODO
Comparer from Generics.Defaults can be optimized.
I have no knowledge of FPC compiler design, so i need a little help...
First thing : To finish my work I need critical compiler magic functions/feature. At first look mayby there is no sense for this functions
but during work on Generic library it's necessary:
function GetReferenceToValue(Value: T): Pointer; // for string types return @s[1] or nil for empty string for Int32 return @i etc. returns a reference to the value, as measured by human/programmer
function GetValueSize(Value: T): Integer; // for string types return Length(s), for Int32 returs 4 etc.
This functions should be implemented as operators (like Inc, Dec operators).
There is half-workaround/cripled solution as record/type helpers (module Generics.Helpers). Don't work for custom strings, and for custom "basic" types like:
type MyStr = type string;
type MyInt = type Int32;
Second thing: Bugs. Look at Critical - fix is needed to perform compatibility with Delphi and proper work.
Regards,
HNB