Class TgoValueDictionary
Unit
Grijjy.Collections
Declaration
type TgoValueDictionary<TKey;TValue:record> = class(TObject)
Description
A light-weight dictionary where the values are of value types (primitive types and records). Differs from TDictionary<TKey, TValue> in the following regards:
The values in the dictionary cannot be reference types (objects, interfaces, strings or dynamic arrays).
It is more light-weight since it doesn't support noticications and has better optimized code.
When requesting a value (using TryGetValue or Items), it returns a Pointer to the type instead of the actual type. This can be both more efficient, and it allows you to directly modify the value in the dictionary.
The pointer is of type TgoPtr<TValue>.P. It may be easier to declare your own pointer type as in PFoo = TgoPtr<TFoo>.P .
Note that you should not cache these pointers for long-term use as they become invalid when you modify the dictionary (add or remove items).
Hierarchy
- TObject
- TgoValueDictionary
Overview
Internal Types
Methods
 |
constructor Create; overload; |
 |
constructor Create(const AComparer: IEqualityComparer<TKey>); overload; |
 |
destructor Destroy; override; |
 |
procedure Add(const AKey: TKey; const AValue: TValue); |
 |
procedure AddOrSetValue(const AKey: TKey; const AValue: TValue); |
 |
function Remove(const AKey: TKey): Boolean; |
 |
procedure Clear; |
 |
function TryGetValue(const AKey: TKey; out AValue: PValue): Boolean; |
 |
function ContainsKey(const AKey: TKey): Boolean; |
 |
function GetEnumerator: TEnumerator; |
 |
function ToArray: TArray<TPair<TKey, TValue>>; |
Properties
 |
property Items[constAKey:TKey]: PValue read GetItem; |
 |
property Keys: TKeyCollection read GetKeys; |
 |
property Values: TValueCollection read GetValues; |
 |
property Count: Integer read FCount; |
Description
Internal Types
 |
PValue = TgoPtr<TValue>.P; |
The pointer type for referencing values in this dictionary.
|
Methods
 |
constructor Create; overload; |
Creates a dictionary using a default comparer
|
 |
constructor Create(const AComparer: IEqualityComparer<TKey>); overload; |
Creates a dictionary using a custom comparer.
Parameters
- AComparer
- the comparer to use to check for item equality. Pass nil to use the default comparer.
|
 |
destructor Destroy; override; |
Destructor
|
 |
procedure Add(const AKey: TKey; const AValue: TValue); |
Add a value to the dictionary for a specified key.
Parameters
- AKey
- the key
- AValue
- the value to
add for the key
Exceptions raised
EListError
- if the dictionary already contains the given Key. Use AddOrSetValue to overwrite an existing key with a new value.
|
 |
procedure AddOrSetValue(const AKey: TKey; const AValue: TValue); |
Adds or sets a value in the dictionary for a specified key.
If the dictionary already contains a value for the given key, then that value is replaced. Otherwise, the value is added to the dictionary.
Parameters
- AKey
- the key
- AValue
- the value to set or replace for the key
|
 |
function Remove(const AKey: TKey): Boolean; |
Removes the value for a specified key from the dictionary.
Parameters
- AKey
- the key to
remove
Returns
True if the key was removed, or False if the dictionary does not contain the given key. |
 |
procedure Clear; |
Clears the dictionary
|
 |
function TryGetValue(const AKey: TKey; out AValue: PValue): Boolean; |
Tries to get a value for the given key.
Note: You should not cache the pointer returned in AValue for long-term use as it is only valid as long as you don't modify the dictionary.
Parameters
- AKey
- the key to search for
- AValue
- is set to a pointer to the value associated with the given key, or nil if the dictionary does not contain the key.
Returns
True if the dictionary contains the given key. In that case, AValue is set to a pointer to the value associated with the key. False if the dictionary does not contain the given key. In that case, AValue is set to nil. |
 |
function ContainsKey(const AKey: TKey): Boolean; |
Checks if the dictionary contains a given key.
Parameters
- AKey
- the key to check.
Returns
True if the dictionary contains AKey, False if not. |
 |
function GetEnumerator: TEnumerator; |
Allows for for..in enumeration of pairs in the dictionary. The value in each pair is a pointer to the actual value, so you would use the enumerator like this:
var
Dictionary: TgrValueDictionary<String, TFoo>;
Pair: TPair<String, TgrPtr<TFoo>.P>;
begin
for Pair in Dictionary do...
end;
|
 |
function ToArray: TArray<TPair<TKey, TValue>>; |
Copies the pairs in the dictionary to a dynamic array
|
Properties
 |
property Items[constAKey:TKey]: PValue read GetItem; |
Returns the value for a given key, raising an exception if the dictionary does not contain the key.
Note: You should not cache the returned pointer for long-term use as it is only valid as long as you don't modify the dictionary.
Parameters
- AKey
- the key to check.
Returns
A pointer to the value for the given key. Exceptions raised
EListError
- if the dictionary doesn't contain AKey
|
 |
property Keys: TKeyCollection read GetKeys; |
All keys in the dictionary. You can enumerate all keys like this:
var
Dictionary: TgrValueDictionary<String, TFoo>;
Key: String;
begin
for Key in Dictionary.Keys do...
end;
|
 |
property Values: TValueCollection read GetValues; |
All values in the dictionary. You can enumerate pointers to all values like this:
var
Dictionary: TgrValueDictionary<String, TFoo>;
Value: TgrPtr<TFoo>.P;
begin
for Value in Dictionary.Values do...
end;
|
 |
property Count: Integer read FCount; |
The number of items in the dictionary
|
Generated by P2PasDoc 0.13.0 on 2017-04-25 12:54:26
|