2005-08-26 12:25:39 +02:00
|
|
|
{
|
|
|
|
:: Implements hashes with Variant values.
|
|
|
|
::
|
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtHashesVariants;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
|
|
|
Variants,
|
|
|
|
|
|
|
|
X2UtHashes;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
type
|
|
|
|
{
|
|
|
|
:$ Variant value class.
|
|
|
|
}
|
|
|
|
TX2HashVariantManager = class(TX2CustomHashManager)
|
|
|
|
public
|
2010-03-12 13:27:49 +01:00
|
|
|
procedure FreeCookie(var ACookie: PX2HashCookie); override;
|
2005-08-26 12:25:39 +02:00
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
function CreateCookie(const AValue: Variant): PX2HashCookie; overload;
|
|
|
|
function GetValue(const ACookie: PX2HashCookie): Variant; overload;
|
2005-08-26 12:25:39 +02:00
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
function Hash(ACookie: PX2HashCookie): Cardinal; override;
|
|
|
|
function Compare(const ACookie1, ACookie2: PX2HashCookie): Boolean; override;
|
|
|
|
function Clone(const ACookie: PX2HashCookie): PX2HashCookie; override;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
{
|
|
|
|
:$ Pointer-to-Variant hash.
|
|
|
|
}
|
|
|
|
TX2PVHash = class(TX2CustomPointerHash)
|
|
|
|
private
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
function GetValue(Key: Pointer): Variant;
|
|
|
|
procedure SetValue(Key: Pointer; const Value: Variant);
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetValueManager: TX2HashVariantManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
protected
|
2010-03-12 13:27:49 +01:00
|
|
|
function CreateValueManager: TX2CustomHashManager; override;
|
|
|
|
|
|
|
|
property ValueManager: TX2HashVariantManager read GetValueManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
public
|
|
|
|
property CurrentValue: Variant read GetCurrentValue;
|
|
|
|
property Values[Key: Pointer]: Variant read GetValue write SetValue; default;
|
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
{
|
|
|
|
:$ Integer-to-Variant hash.
|
|
|
|
}
|
|
|
|
TX2IVHash = class(TX2CustomIntegerHash)
|
|
|
|
private
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
function GetValue(Key: Integer): Variant;
|
|
|
|
procedure SetValue(Key: Integer; const Value: Variant);
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetValueManager: TX2HashVariantManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
protected
|
2010-03-12 13:27:49 +01:00
|
|
|
function CreateValueManager: TX2CustomHashManager; override;
|
|
|
|
|
|
|
|
property ValueManager: TX2HashVariantManager read GetValueManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
public
|
|
|
|
property CurrentValue: Variant read GetCurrentValue;
|
|
|
|
property Values[Key: Integer]: Variant read GetValue write SetValue; default;
|
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
{
|
|
|
|
:$ Object-to-Variant hash.
|
|
|
|
}
|
|
|
|
TX2OVHash = class(TX2CustomObjectHash)
|
|
|
|
private
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
function GetValue(Key: TObject): Variant;
|
|
|
|
procedure SetValue(Key: TObject; const Value: Variant);
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetValueManager: TX2HashVariantManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
protected
|
2010-03-12 13:27:49 +01:00
|
|
|
function CreateValueManager: TX2CustomHashManager; override;
|
|
|
|
|
|
|
|
property ValueManager: TX2HashVariantManager read GetValueManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
public
|
|
|
|
property CurrentValue: Variant read GetCurrentValue;
|
|
|
|
property Values[Key: TObject]: Variant read GetValue write SetValue; default;
|
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
{
|
|
|
|
:$ String-to-Variant hash.
|
|
|
|
}
|
|
|
|
TX2SVHash = class(TX2CustomStringHash)
|
|
|
|
private
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
function GetValue(Key: String): Variant;
|
|
|
|
procedure SetValue(Key: String; const Value: Variant);
|
2010-03-12 13:27:49 +01:00
|
|
|
function GetValueManager: TX2HashVariantManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
protected
|
2010-03-12 13:27:49 +01:00
|
|
|
function CreateValueManager: TX2CustomHashManager; override;
|
|
|
|
|
|
|
|
property ValueManager: TX2HashVariantManager read GetValueManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
public
|
|
|
|
property CurrentValue: Variant read GetCurrentValue;
|
|
|
|
property Values[Key: String]: Variant read GetValue write SetValue; default;
|
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
implementation
|
2010-03-12 13:27:49 +01:00
|
|
|
uses
|
|
|
|
SysUtils;
|
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
|
|
|
|
{========================================
|
|
|
|
TX2HashVariantManager
|
|
|
|
========================================}
|
2010-03-12 13:27:49 +01:00
|
|
|
procedure TX2HashVariantManager.FreeCookie(var ACookie: PX2HashCookie);
|
|
|
|
var
|
|
|
|
variantCookie: PVariant;
|
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
if Assigned(ACookie) then
|
|
|
|
begin
|
|
|
|
variantCookie := ACookie;
|
|
|
|
VarClear(variantCookie^);
|
|
|
|
|
|
|
|
Dispose(variantCookie);
|
|
|
|
end;
|
|
|
|
|
|
|
|
inherited;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2HashVariantManager.CreateCookie(const AValue: Variant): PX2HashCookie;
|
|
|
|
var
|
|
|
|
variantCookie: PVariant;
|
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
New(variantCookie);
|
|
|
|
VarCopy(variantCookie^, AValue);
|
2005-08-26 12:25:39 +02:00
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := variantCookie;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2HashVariantManager.GetValue(const ACookie: PX2HashCookie): Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
VarCopy(Result, PVariant(ACookie)^);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2HashVariantManager.Hash(ACookie: PX2HashCookie): Cardinal;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
{ For now, this manager is only used for Values, which aren't hashed. }
|
|
|
|
raise Exception.Create('Hash method is not supported for Variants');
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2HashVariantManager.Compare(const ACookie1, ACookie2: PX2HashCookie): Boolean;
|
|
|
|
begin
|
|
|
|
Result := (VarCompareValue(PVariant(ACookie1)^, PVariant(ACookie2)^) = vrEqual);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2HashVariantManager.Clone(const ACookie: PX2HashCookie): PX2HashCookie;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := CreateCookie(PVariant(ACookie)^);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
//
|
|
|
|
//function TX2HashVariantManager.ToValue(const AData: Pointer): Variant;
|
|
|
|
//begin
|
|
|
|
// Result := PVariant(AData)^;
|
|
|
|
//end;
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//function TX2HashVariantManager.Compare(const AData, AValue: Pointer;
|
|
|
|
// const ASize: Cardinal): Boolean;
|
|
|
|
//begin
|
|
|
|
// Result := (VarCompareValue(PVariant(AData)^, PVariant(AValue)^) = vrEqual);
|
|
|
|
//end;
|
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
|
|
|
|
{========================================
|
|
|
|
TX2PVHash
|
|
|
|
========================================}
|
2010-03-12 13:27:49 +01:00
|
|
|
function TX2PVHash.CreateValueManager: TX2CustomHashManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := TX2HashVariantManager.Create;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2PVHash.GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
CursorRequired;
|
|
|
|
Result := ValueManager.GetValue(Cursor.Current^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
function TX2PVHash.GetValue(Key: Pointer): Variant;
|
|
|
|
var
|
2010-03-12 13:27:49 +01:00
|
|
|
item: PX2HashValue;
|
2005-08-26 12:25:39 +02:00
|
|
|
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := Unassigned;
|
|
|
|
item := Find(Key, False);
|
|
|
|
if Assigned(item) then
|
|
|
|
Result := ValueManager.GetValue(item^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
procedure TX2PVHash.SetValue(Key: Pointer; const Value: Variant);
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
inherited SetValue(Find(Key, True), ValueManager.CreateCookie(Value));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2PVHash.GetValueManager: TX2HashVariantManager;
|
|
|
|
begin
|
|
|
|
Result := TX2HashVariantManager(inherited ValueManager);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{========================================
|
|
|
|
TX2IVHash
|
|
|
|
========================================}
|
2010-03-12 13:27:49 +01:00
|
|
|
function TX2IVHash.CreateValueManager: TX2CustomHashManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := TX2HashVariantManager.Create;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2IVHash.GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
CursorRequired;
|
|
|
|
Result := ValueManager.GetValue(Cursor.Current^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
function TX2IVHash.GetValue(Key: Integer): Variant;
|
|
|
|
var
|
2010-03-12 13:27:49 +01:00
|
|
|
item: PX2HashValue;
|
2005-08-26 12:25:39 +02:00
|
|
|
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := Unassigned;
|
|
|
|
item := Find(Key, False);
|
|
|
|
if Assigned(item) then
|
|
|
|
Result := ValueManager.GetValue(item^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
procedure TX2IVHash.SetValue(Key: Integer; const Value: Variant);
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
inherited SetValue(Find(Key, True), ValueManager.CreateCookie(Value));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2IVHash.GetValueManager: TX2HashVariantManager;
|
|
|
|
begin
|
|
|
|
Result := TX2HashVariantManager(inherited ValueManager);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{========================================
|
|
|
|
TX2OVHash
|
|
|
|
========================================}
|
2010-03-12 13:27:49 +01:00
|
|
|
function TX2OVHash.CreateValueManager: TX2CustomHashManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := TX2HashVariantManager.Create;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2OVHash.GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
CursorRequired;
|
|
|
|
Result := ValueManager.GetValue(Cursor.Current^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
function TX2OVHash.GetValue(Key: TObject): Variant;
|
|
|
|
var
|
2010-03-12 13:27:49 +01:00
|
|
|
item: PX2HashValue;
|
2005-08-26 12:25:39 +02:00
|
|
|
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := Unassigned;
|
|
|
|
item := Find(Key, False);
|
|
|
|
if Assigned(item) then
|
|
|
|
Result := ValueManager.GetValue(item^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
procedure TX2OVHash.SetValue(Key: TObject; const Value: Variant);
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
inherited SetValue(Find(Key, True), ValueManager.CreateCookie(Value));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2OVHash.GetValueManager: TX2HashVariantManager;
|
|
|
|
begin
|
|
|
|
Result := TX2HashVariantManager(inherited ValueManager);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{========================================
|
|
|
|
TX2SVHash
|
|
|
|
========================================}
|
2010-03-12 13:27:49 +01:00
|
|
|
function TX2SVHash.CreateValueManager: TX2CustomHashManager;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := TX2HashVariantManager.Create;
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
|
|
|
function TX2SVHash.GetCurrentValue: Variant;
|
2005-08-26 12:25:39 +02:00
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
CursorRequired;
|
|
|
|
Result := ValueManager.GetValue(Cursor.Current^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
function TX2SVHash.GetValue(Key: String): Variant;
|
|
|
|
var
|
2010-03-12 13:27:49 +01:00
|
|
|
item: PX2HashValue;
|
2005-08-26 12:25:39 +02:00
|
|
|
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
Result := Unassigned;
|
|
|
|
item := Find(Key, False);
|
|
|
|
if Assigned(item) then
|
|
|
|
Result := ValueManager.GetValue(item^.Value);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
2010-03-12 13:27:49 +01:00
|
|
|
|
2005-08-26 12:25:39 +02:00
|
|
|
procedure TX2SVHash.SetValue(Key: String; const Value: Variant);
|
|
|
|
begin
|
2010-03-12 13:27:49 +01:00
|
|
|
inherited SetValue(Find(Key, True), ValueManager.CreateCookie(Value));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TX2SVHash.GetValueManager: TX2HashVariantManager;
|
|
|
|
begin
|
|
|
|
Result := TX2HashVariantManager(inherited ValueManager);
|
2005-08-26 12:25:39 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|