Class TgoValueDictionary

DescriptionHierarchyInternal TypesFieldsMethodsProperties

Unit

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

Public PValue = TgoPtr<TValue>.P;

Methods

Public constructor Create; overload;
Public constructor Create(const AComparer: IEqualityComparer<TKey>); overload;
Public destructor Destroy; override;
Public procedure Add(const AKey: TKey; const AValue: TValue);
Public procedure AddOrSetValue(const AKey: TKey; const AValue: TValue);
Public function Remove(const AKey: TKey): Boolean;
Public procedure Clear;
Public function TryGetValue(const AKey: TKey; out AValue: PValue): Boolean;
Public function ContainsKey(const AKey: TKey): Boolean;
Public function GetEnumerator: TEnumerator;
Public function ToArray: TArray<TPair<TKey, TValue>>;

Properties

Public property Items[constAKey:TKey]: PValue read GetItem;
Public property Keys: TKeyCollection read GetKeys;
Public property Values: TValueCollection read GetValues;
Public property Count: Integer read FCount;

Description

Internal Types

Public PValue = TgoPtr<TValue>.P;

The pointer type for referencing values in this dictionary.

Methods

Public constructor Create; overload;

Creates a dictionary using a default comparer

Public 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.
Public destructor Destroy; override;

Destructor

Public 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.
Public 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
Public 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.

Public procedure Clear;

Clears the dictionary

Public 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.

Public 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.

Public 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
        // Initialize Dictionary
        for Pair in Dictionary do...
      end;

Public function ToArray: TArray<TPair<TKey, TValue>>;

Copies the pairs in the dictionary to a dynamic array

Properties

Public 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
Public 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
        // Initialize Dictionary
        for Key in Dictionary.Keys do...
      end;

Public 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
        // Initialize Dictionary
        for Value in Dictionary.Values do...
      end;

Public 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