HashTable があり、not_found
結果を返す方法が必要です。
type
TCell<T> = record
.....
property key: cardinal read FKey write FKey;
property data: T read FData write FData;
end;
THashTable<T> = class(TEnumerable<T>)
private
FCells: array of TCell<T>;
FEmpty: T;
...
constructor Create(InitialSize: cardinal); overload;
function Lookup(key: cardinal): T;
...
end;
constructor THashTable<T>.Create(InitialSize: cardinal);
begin
inherited Create;
// Initialize regular cells
FArraySize:= InitialSize;
Assert((FArraySize and (FArraySize - 1)) = 0); // Must be a power of 2
SetLength(FCells, FArraySize);
FillChar(FEmpty, SizeOf(FEmpty), #0); //Superfluous I know, just there to
//demonstrate the point.
end;
上記の構造を考えると、どうすればnot found
結果を返すことができますか?
ポインターがあれば、へのnil
ポインターを返しますT
。
ただし、ジェネリック型へのポインターは許可されていません。
だから私は以下の解決策を思いついた:
function THashTable<T>.Lookup(key: cardinal): T;
var
Cell: NativeUInt;
begin
if (key <> 0) then begin
// Check regular cells
Cell:= First_Cell(IntegerHash(key));
while (true) do begin
if (FCells[Cell].key = key) then Exit(FCells[Cell].data);
if not (FCells[Cell].key = 0) then Exit(FEmpty); <<-- is this correct?
Cell:= Circular_Next(Cell);
end;
end else begin
Result:= FEmpty; <<--- Can I return an empty generic like this?
end;
end;
ゼロで初期化されたジェネリックを意味に返すことはできますno result
か?
または、構造化されたタイプ(クラス/レコード/バリアント/文字列など)で問題が発生しますか。
T
が整数であるときのあいまいさを理解していることに注意してください。ゼロは有効な値である可能性が非常に高いため、not_found と区別できません。
私はそれらの結果について心配していません。