| 1 |
unit Hashes; |
| 2 |
interface |
| 3 |
|
| 4 |
uses SysUtils; |
| 5 |
|
| 6 |
const |
| 7 |
c_HashInitialItemShift = 7; |
| 8 |
c_HashCompactR = 2; { This many spaces per item. } |
| 9 |
c_HashCompactM = 100; { Never for less than this number of spaces. } |
| 10 |
type |
| 11 |
EHashError = class(Exception); |
| 12 |
EHashFindError = class(EHashError); |
| 13 |
EHashIterateError = class(EHashError); |
| 14 |
EHashInvalidKeyError = class(EHashError); |
| 15 |
THashRecord = record |
| 16 |
Hash: Cardinal; |
| 17 |
ItemIndex: integer; |
| 18 |
Key: string; |
| 19 |
end; |
| 20 |
THashIterator = record |
| 21 |
ck, cx: integer; |
| 22 |
end; |
| 23 |
THash = class |
| 24 |
protected |
| 25 |
f_Keys: array of array of THashRecord; |
| 26 |
f_CurrentItemShift: integer; |
| 27 |
f_CurrentItemCount: integer; |
| 28 |
f_CurrentItemMask: integer; |
| 29 |
f_CurrentItemMaxIdx: integer; |
| 30 |
f_SpareItems: array of integer; |
| 31 |
f_NextAllowed: boolean; |
| 32 |
f_CurrentKey: string; |
| 33 |
f_AllowCompact: boolean; |
| 34 |
f_CurrentIterator: THashIterator; |
| 35 |
procedure FUpdateMasks; |
| 36 |
procedure FUpdateBuckets; |
| 37 |
function FFindKey(const Key: string; var k, x: integer): boolean; |
| 38 |
procedure FSetOrAddKey(const Key: string; ItemIndex: integer); |
| 39 |
procedure FDeleteIndex(i: integer); virtual; abstract; |
| 40 |
function FGetItemCount: integer; |
| 41 |
function FAllocItemIndex: integer; |
| 42 |
procedure FMoveIndex(oldIndex, newIndex: integer); virtual; abstract; |
| 43 |
procedure FTrimIndexes(count: integer); virtual; abstract; |
| 44 |
procedure FClearItems; virtual; abstract; |
| 45 |
function FIndexMax: integer; virtual; abstract; |
| 46 |
procedure FAutoCompact; |
| 47 |
public |
| 48 |
constructor Create; reintroduce; virtual; |
| 49 |
function Exists(const Key: string): boolean; |
| 50 |
procedure Rename(const Key, NewName: string); |
| 51 |
procedure Delete(const Key: string); |
| 52 |
procedure Restart; |
| 53 |
function Next: boolean; |
| 54 |
function Previous: boolean; |
| 55 |
function CurrentKey: string; |
| 56 |
property ItemCount: integer read FGetItemCount; |
| 57 |
procedure Compact; |
| 58 |
procedure Clear; |
| 59 |
property AllowCompact: boolean read f_AllowCompact write f_AllowCompact; |
| 60 |
property CurrentIterator: THashIterator read f_CurrentIterator write |
| 61 |
f_CurrentIterator; |
| 62 |
function NewIterator: THashIterator; |
| 63 |
end; |
| 64 |
TStringHash = class(THash) |
| 65 |
protected |
| 66 |
f_Items: array of string; |
| 67 |
procedure FDeleteIndex(i: integer); override; |
| 68 |
function FGetItem(const Key: string): string; |
| 69 |
procedure FSetItem(const Key, Value: string); |
| 70 |
procedure FMoveIndex(oldIndex, newIndex: integer); override; |
| 71 |
procedure FTrimIndexes(count: integer); override; |
| 72 |
procedure FClearItems; override; |
| 73 |
function FIndexMax: integer; override; |
| 74 |
public |
| 75 |
property Items[const Key: string]: string read FGetItem |
| 76 |
write FSetItem; default; |
| 77 |
end; |
| 78 |
TIntegerHash = class(THash) |
| 79 |
protected |
| 80 |
f_Items: array of integer; |
| 81 |
procedure FDeleteIndex(i: integer); override; |
| 82 |
function FGetItem(const Key: string): integer; |
| 83 |
procedure FSetItem(const Key: string; Value: integer); |
| 84 |
procedure FMoveIndex(oldIndex, newIndex: integer); override; |
| 85 |
procedure FTrimIndexes(count: integer); override; |
| 86 |
procedure FClearItems; override; |
| 87 |
function FIndexMax: integer; override; |
| 88 |
public |
| 89 |
property Items[const Key: string]: integer read FGetItem |
| 90 |
write FSetItem; default; |
| 91 |
end; |
| 92 |
TObjectHash = class(THash) |
| 93 |
protected |
| 94 |
f_Items: array of TObject; |
| 95 |
procedure FDeleteIndex(i: integer); override; |
| 96 |
function FGetItem(const Key: string): TObject; |
| 97 |
procedure FSetItem(const Key: string; Value: TObject); |
| 98 |
procedure FMoveIndex(oldIndex, newIndex: integer); override; |
| 99 |
procedure FTrimIndexes(count: integer); override; |
| 100 |
procedure FClearItems; override; |
| 101 |
function FIndexMax: integer; override; |
| 102 |
public |
| 103 |
property Items[const Key: string]: TObject read FGetItem |
| 104 |
write FSetItem; default; |
| 105 |
destructor Destroy; override; |
| 106 |
end; |
| 107 |
|
| 108 |
implementation |
| 109 |
|
| 110 |
function HashThis(const s: string): cardinal; |
| 111 |
var |
| 112 |
h, g, i: cardinal; |
| 113 |
begin |
| 114 |
if (s = '') then |
| 115 |
raise EHashInvalidKeyError.Create('Key cannot be an empty string'); |
| 116 |
h := $12345670; |
| 117 |
for i := 1 to Length(s) do begin |
| 118 |
h := (h shl 4) + ord(s[i]); |
| 119 |
g := h and $f0000000; |
| 120 |
if (g > 0) then |
| 121 |
h := h or (g shr 24) or g; |
| 122 |
end; |
| 123 |
result := h; |
| 124 |
end; |
| 125 |
|
| 126 |
constructor THash.Create; |
| 127 |
begin |
| 128 |
inherited Create; |
| 129 |
self.f_CurrentIterator.ck := -1; |
| 130 |
self.f_CurrentIterator.cx := 0; |
| 131 |
self.f_CurrentItemShift := c_HashInitialItemShift; |
| 132 |
self.FUpdateMasks; |
| 133 |
self.FUpdateBuckets; |
| 134 |
self.f_AllowCompact := true; |
| 135 |
end; |
| 136 |
|
| 137 |
procedure THash.Delete(const Key: string); |
| 138 |
var |
| 139 |
k, x, i: integer; |
| 140 |
begin |
| 141 |
{ Hash has been modified, so disallow Next. } |
| 142 |
self.f_NextAllowed := false; |
| 143 |
if (self.FFindKey(Key, k, x)) then begin |
| 144 |
{ Delete the Index entry. } |
| 145 |
i := self.f_Keys[k][x].ItemIndex; |
| 146 |
self.FDeleteIndex(i); |
| 147 |
{ Add the index to the Spares list. } |
| 148 |
SetLength(self.f_SpareItems, Length(self.f_SpareItems) + 1); |
| 149 |
self.f_SpareItems[High(self.f_SpareItems)] := i; |
| 150 |
{ Overwrite key with the last in the list. } |
| 151 |
self.f_Keys[k][x] := self.f_Keys[k][High(self.f_Keys[k])]; |
| 152 |
{ Delete the last in the list. } |
| 153 |
SetLength(self.f_Keys[k], Length(self.f_Keys[k]) - 1); |
| 154 |
end else |
| 155 |
raise EHashFindError.CreateFmt('Key "%s" not found', [Key]); |
| 156 |
|
| 157 |
self.FAutoCompact; |
| 158 |
end; |
| 159 |
|
| 160 |
function THash.Exists(const Key: string): boolean; |
| 161 |
var |
| 162 |
dummy1, dummy2: integer; |
| 163 |
begin |
| 164 |
result := FFindKey(Key, dummy1, dummy2); |
| 165 |
end; |
| 166 |
|
| 167 |
procedure THash.FSetOrAddKey(const Key: string; ItemIndex: integer); |
| 168 |
var |
| 169 |
k, x, i: integer; |
| 170 |
begin |
| 171 |
{ Exists already? } |
| 172 |
if (self.FFindKey(Key, k, x)) then begin |
| 173 |
{ Yep. Delete the old stuff and set the new value. } |
| 174 |
i := self.f_Keys[k][x].ItemIndex; |
| 175 |
self.FDeleteIndex(i); |
| 176 |
self.f_Keys[k][x].ItemIndex := ItemIndex; |
| 177 |
{ Add the index to the spares list. } |
| 178 |
SetLength(self.f_SpareItems, Length(self.f_SpareItems) + 1); |
| 179 |
self.f_SpareItems[High(self.f_SpareItems)] := i; |
| 180 |
end else begin |
| 181 |
{ No, create a new one. } |
| 182 |
SetLength(self.f_Keys[k], Length(self.f_Keys[k]) + 1); |
| 183 |
self.f_Keys[k][High(self.f_Keys[k])].Key := Key; |
| 184 |
self.f_Keys[k][High(self.f_Keys[k])].ItemIndex := ItemIndex; |
| 185 |
self.f_Keys[k][High(self.f_Keys[k])].Hash := HashThis(Key); |
| 186 |
end; |
| 187 |
end; |
| 188 |
|
| 189 |
function THash.FFindKey(const Key: string; var k, x: integer): boolean; |
| 190 |
var |
| 191 |
i: integer; |
| 192 |
h: cardinal; |
| 193 |
begin |
| 194 |
{ Which bucket? } |
| 195 |
h := HashThis(Key); |
| 196 |
k := h and f_CurrentItemMask; |
| 197 |
result := false; |
| 198 |
{ Look for it. } |
| 199 |
for i := 0 to High(self.f_Keys[k]) do |
| 200 |
if (self.f_Keys[k][i].Hash = h) or true then |
| 201 |
if (self.f_Keys[k][i].Key = Key) then begin |
| 202 |
{ Found it! } |
| 203 |
result := true; |
| 204 |
x := i; |
| 205 |
break; |
| 206 |
end; |
| 207 |
end; |
| 208 |
|
| 209 |
procedure THash.Rename(const Key, NewName: string); |
| 210 |
var |
| 211 |
k, x, i: integer; |
| 212 |
begin |
| 213 |
{ Hash has been modified, so disallow Next. } |
| 214 |
self.f_NextAllowed := false; |
| 215 |
if (self.FFindKey(Key, k, x)) then begin |
| 216 |
{ Remember the ItemIndex. } |
| 217 |
i := self.f_Keys[k][x].ItemIndex; |
| 218 |
{ Overwrite key with the last in the list. } |
| 219 |
self.f_Keys[k][x] := self.f_Keys[k][High(self.f_Keys[k])]; |
| 220 |
{ Delete the last in the list. } |
| 221 |
SetLength(self.f_Keys[k], Length(self.f_Keys[k]) - 1); |
| 222 |
{ Create the new item. } |
| 223 |
self.FSetOrAddKey(NewName, i); |
| 224 |
end else |
| 225 |
raise EHashFindError.CreateFmt('Key "%s" not found', [Key]); |
| 226 |
|
| 227 |
self.FAutoCompact; |
| 228 |
end; |
| 229 |
|
| 230 |
function THash.CurrentKey: string; |
| 231 |
begin |
| 232 |
if (not (self.f_NextAllowed)) then |
| 233 |
raise EHashIterateError.Create('Cannot find CurrentKey as the hash has ' |
| 234 |
+ 'been modified since Restart was called') |
| 235 |
else if (self.f_CurrentKey = '') then |
| 236 |
raise EHashIterateError.Create('Cannot find CurrentKey as Next has not yet ' |
| 237 |
+ 'been called after Restart') |
| 238 |
else |
| 239 |
result := self.f_CurrentKey; |
| 240 |
end; |
| 241 |
|
| 242 |
function THash.Next: boolean; |
| 243 |
begin |
| 244 |
if (not (self.f_NextAllowed)) then |
| 245 |
raise EHashIterateError.Create('Cannot get Next as the hash has ' |
| 246 |
+ 'been modified since Restart was called'); |
| 247 |
result := false; |
| 248 |
if (self.f_CurrentIterator.ck = -1) then begin |
| 249 |
self.f_CurrentIterator.ck := 0; |
| 250 |
self.f_CurrentIterator.cx := 0; |
| 251 |
end; |
| 252 |
while ((not result) and (self.f_CurrentIterator.ck <= f_CurrentItemMaxIdx)) do begin |
| 253 |
if (self.f_CurrentIterator.cx < Length(self.f_Keys[self.f_CurrentIterator.ck])) then begin |
| 254 |
result := true; |
| 255 |
self.f_CurrentKey := self.f_Keys[self.f_CurrentIterator.ck][self.f_CurrentIterator.cx].Key; |
| 256 |
inc(self.f_CurrentIterator.cx); |
| 257 |
end else begin |
| 258 |
inc(self.f_CurrentIterator.ck); |
| 259 |
self.f_CurrentIterator.cx := 0; |
| 260 |
end; |
| 261 |
end; |
| 262 |
end; |
| 263 |
|
| 264 |
procedure THash.Restart; |
| 265 |
begin |
| 266 |
self.f_CurrentIterator.ck := -1; |
| 267 |
self.f_CurrentIterator.cx := 0; |
| 268 |
self.f_NextAllowed := true; |
| 269 |
end; |
| 270 |
|
| 271 |
function THash.FGetItemCount: integer; |
| 272 |
var |
| 273 |
i: integer; |
| 274 |
begin |
| 275 |
{ Calculate our item count. } |
| 276 |
result := 0; |
| 277 |
for i := 0 to f_CurrentItemMaxIdx do |
| 278 |
inc(result, Length(self.f_Keys[i])); |
| 279 |
end; |
| 280 |
|
| 281 |
function THash.FAllocItemIndex: integer; |
| 282 |
begin |
| 283 |
if (Length(self.f_SpareItems) > 0) then begin |
| 284 |
{ Use the top SpareItem. } |
| 285 |
result := self.f_SpareItems[High(self.f_SpareItems)]; |
| 286 |
SetLength(self.f_SpareItems, Length(self.f_SpareItems) - 1); |
| 287 |
end else begin |
| 288 |
result := self.FIndexMax + 1; |
| 289 |
end; |
| 290 |
end; |
| 291 |
|
| 292 |
procedure THash.Compact; |
| 293 |
var |
| 294 |
aSpaces: array of boolean; |
| 295 |
aMapping: array of integer; |
| 296 |
i, j: integer; |
| 297 |
begin |
| 298 |
{ Find out where the gaps are. We could do this by sorting, but that's at |
| 299 |
least O(n log n), and sometimes O(n^2), so we'll go for the O(n) method, |
| 300 |
even though it involves multiple passes. Note that this is a lot faster |
| 301 |
than it looks. Disabling this saves about 3% in my benchmarks, but uses a |
| 302 |
lot more memory. } |
| 303 |
if (self.AllowCompact) then begin |
| 304 |
SetLength(aSpaces, self.FIndexMax + 1); |
| 305 |
SetLength(aMapping, self.FIndexMax + 1); |
| 306 |
for i := 0 to High(aSpaces) do |
| 307 |
aSpaces[i] := false; |
| 308 |
for i := 0 to High(aMapping) do |
| 309 |
aMapping[i] := i; |
| 310 |
for i := 0 to High(self.f_SpareItems) do |
| 311 |
aSpaces[self.f_SpareItems[i]] := true; |
| 312 |
|
| 313 |
{ Starting at the low indexes, fill empty ones from the high indexes. } |
| 314 |
i := 0; |
| 315 |
j := self.FIndexMax; |
| 316 |
while (i < j) do begin |
| 317 |
if (aSpaces[i]) then begin |
| 318 |
while ((i < j) and (aSpaces[j])) do |
| 319 |
dec(j); |
| 320 |
if (i < j) then begin |
| 321 |
aSpaces[i] := false; |
| 322 |
aSpaces[j] := true; |
| 323 |
self.FMoveIndex(j, i); |
| 324 |
aMapping[j] := i |
| 325 |
end; |
| 326 |
end else |
| 327 |
inc(i); |
| 328 |
end; |
| 329 |
|
| 330 |
j := self.FIndexMax; |
| 331 |
while (aSpaces[j]) do |
| 332 |
dec(j); |
| 333 |
|
| 334 |
{ Trim the items array down to size. } |
| 335 |
self.FTrimIndexes(j + 1); |
| 336 |
|
| 337 |
{ Clear the spaces. } |
| 338 |
SetLength(self.f_SpareItems, 0); |
| 339 |
|
| 340 |
{ Update our buckets. } |
| 341 |
for i := 0 to f_CurrentItemMaxIdx do |
| 342 |
for j := 0 to High(self.f_Keys[i]) do |
| 343 |
self.f_Keys[i][j].ItemIndex := aMapping[self.f_Keys[i][j].ItemIndex]; |
| 344 |
end; |
| 345 |
end; |
| 346 |
|
| 347 |
procedure THash.FAutoCompact; |
| 348 |
begin |
| 349 |
if (self.AllowCompact) then |
| 350 |
if (Length(self.f_SpareItems) >= c_HashCompactM) then |
| 351 |
if (self.FIndexMax * c_HashCompactR > Length(self.f_SpareItems)) then |
| 352 |
self.Compact; |
| 353 |
end; |
| 354 |
|
| 355 |
procedure THash.Clear; |
| 356 |
var |
| 357 |
i: integer; |
| 358 |
begin |
| 359 |
self.FClearItems; |
| 360 |
SetLength(self.f_SpareItems, 0); |
| 361 |
for i := 0 to f_CurrentItemMaxIdx do |
| 362 |
SetLength(self.f_Keys[i], 0); |
| 363 |
end; |
| 364 |
|
| 365 |
procedure THash.FUpdateMasks; |
| 366 |
begin |
| 367 |
f_CurrentItemMask := (1 shl f_CurrentItemShift) - 1; |
| 368 |
f_CurrentItemMaxIdx := (1 shl f_CurrentItemShift) - 1; |
| 369 |
f_CurrentItemCount := (1 shl f_CurrentItemShift); |
| 370 |
end; |
| 371 |
|
| 372 |
procedure THash.FUpdateBuckets; |
| 373 |
begin |
| 374 |
{ This is just a temporary thing. } |
| 375 |
SetLength(self.f_Keys, self.f_CurrentItemCount); |
| 376 |
end; |
| 377 |
|
| 378 |
function THash.NewIterator: THashIterator; |
| 379 |
begin |
| 380 |
result.ck := -1; |
| 381 |
result.cx := 0; |
| 382 |
end; |
| 383 |
|
| 384 |
function THash.Previous: boolean; |
| 385 |
begin |
| 386 |
if (not (self.f_NextAllowed)) then |
| 387 |
raise EHashIterateError.Create('Cannot get Next as the hash has ' |
| 388 |
+ 'been modified since Restart was called'); |
| 389 |
result := false; |
| 390 |
if (self.f_CurrentIterator.ck >= 0) then begin |
| 391 |
while ((not result) and (self.f_CurrentIterator.ck >= 0)) do begin |
| 392 |
dec(self.f_CurrentIterator.cx); |
| 393 |
if (self.f_CurrentIterator.cx >= 0) then begin |
| 394 |
result := true; |
| 395 |
self.f_CurrentKey := self.f_Keys[self.f_CurrentIterator.ck][self.f_CurrentIterator.cx].Key; |
| 396 |
end else begin |
| 397 |
dec(self.f_CurrentIterator.ck); |
| 398 |
if (self.f_CurrentIterator.ck >= 0) then |
| 399 |
self.f_CurrentIterator.cx := Length(self.f_Keys[self.f_CurrentIterator.ck]); |
| 400 |
end; |
| 401 |
end; |
| 402 |
end; |
| 403 |
end; |
| 404 |
|
| 405 |
{ TStringHash } |
| 406 |
|
| 407 |
procedure TStringHash.FDeleteIndex(i: integer); |
| 408 |
begin |
| 409 |
self.f_Items[i] := ''; |
| 410 |
end; |
| 411 |
|
| 412 |
function TStringHash.FGetItem(const Key: string): string; |
| 413 |
var |
| 414 |
k, x: integer; |
| 415 |
begin |
| 416 |
if (self.FFindKey(Key, k, x)) then |
| 417 |
result := self.f_Items[self.f_Keys[k][x].ItemIndex] |
| 418 |
else |
| 419 |
raise EHashFindError.CreateFmt('Key "%s" not found', [Key]); |
| 420 |
end; |
| 421 |
|
| 422 |
procedure TStringHash.FMoveIndex(oldIndex, newIndex: integer); |
| 423 |
begin |
| 424 |
self.f_Items[newIndex] := self.f_Items[oldIndex]; |
| 425 |
end; |
| 426 |
|
| 427 |
procedure TStringHash.FSetItem(const Key, Value: string); |
| 428 |
var |
| 429 |
k, x, i: integer; |
| 430 |
begin |
| 431 |
if (self.FFindKey(Key, k, x)) then |
| 432 |
self.f_Items[self.f_Keys[k][x].ItemIndex] := Value |
| 433 |
else begin |
| 434 |
{ New index entry, or recycle an old one. } |
| 435 |
i := self.FAllocItemIndex; |
| 436 |
if (i > High(self.f_Items)) then |
| 437 |
SetLength(self.f_Items, i + 1); |
| 438 |
self.f_Items[i] := Value; |
| 439 |
{ Add it to the hash. } |
| 440 |
SetLength(self.f_Keys[k], Length(self.f_Keys[k]) + 1); |
| 441 |
self.f_Keys[k][High(self.f_Keys[k])].Key := Key; |
| 442 |
self.f_Keys[k][High(self.f_Keys[k])].ItemIndex := i; |
| 443 |
self.f_Keys[k][High(self.f_Keys[k])].Hash := HashThis(Key); |
| 444 |
{ Hash has been modified, so disallow Next. } |
| 445 |
self.f_NextAllowed := false; |
| 446 |
end; |
| 447 |
end; |
| 448 |
|
| 449 |
function TStringHash.FIndexMax: integer; |
| 450 |
begin |
| 451 |
result := High(self.f_Items); |
| 452 |
end; |
| 453 |
|
| 454 |
procedure TStringHash.FTrimIndexes(count: integer); |
| 455 |
begin |
| 456 |
SetLength(self.f_Items, count); |
| 457 |
end; |
| 458 |
|
| 459 |
procedure TStringHash.FClearItems; |
| 460 |
begin |
| 461 |
SetLength(self.f_Items, 0); |
| 462 |
end; |
| 463 |
|
| 464 |
{ TIntegerHash } |
| 465 |
|
| 466 |
procedure TIntegerHash.FDeleteIndex(i: integer); |
| 467 |
begin |
| 468 |
self.f_Items[i] := 0; |
| 469 |
end; |
| 470 |
|
| 471 |
function TIntegerHash.FGetItem(const Key: string): integer; |
| 472 |
var |
| 473 |
k, x: integer; |
| 474 |
begin |
| 475 |
if (self.FFindKey(Key, k, x)) then |
| 476 |
result := self.f_Items[self.f_Keys[k][x].ItemIndex] |
| 477 |
else |
| 478 |
raise EHashFindError.CreateFmt('Key "%s" not found', [Key]); |
| 479 |
end; |
| 480 |
|
| 481 |
procedure TIntegerHash.FMoveIndex(oldIndex, newIndex: integer); |
| 482 |
begin |
| 483 |
self.f_Items[newIndex] := self.f_Items[oldIndex]; |
| 484 |
end; |
| 485 |
|
| 486 |
procedure TIntegerHash.FSetItem(const Key: string; Value: integer); |
| 487 |
var |
| 488 |
k, x, i: integer; |
| 489 |
begin |
| 490 |
if (self.FFindKey(Key, k, x)) then |
| 491 |
self.f_Items[self.f_Keys[k][x].ItemIndex] := Value |
| 492 |
else begin |
| 493 |
{ New index entry, or recycle an old one. } |
| 494 |
i := self.FAllocItemIndex; |
| 495 |
if (i > High(self.f_Items)) then |
| 496 |
SetLength(self.f_Items, i + 1); |
| 497 |
self.f_Items[i] := Value; |
| 498 |
{ Add it to the hash. } |
| 499 |
SetLength(self.f_Keys[k], Length(self.f_Keys[k]) + 1); |
| 500 |
self.f_Keys[k][High(self.f_Keys[k])].Key := Key; |
| 501 |
self.f_Keys[k][High(self.f_Keys[k])].ItemIndex := i; |
| 502 |
self.f_Keys[k][High(self.f_Keys[k])].Hash := HashThis(Key); |
| 503 |
{ Hash has been modified, so disallow Next. } |
| 504 |
self.f_NextAllowed := false; |
| 505 |
end; |
| 506 |
end; |
| 507 |
|
| 508 |
function TIntegerHash.FIndexMax: integer; |
| 509 |
begin |
| 510 |
result := High(self.f_Items); |
| 511 |
end; |
| 512 |
|
| 513 |
procedure TIntegerHash.FTrimIndexes(count: integer); |
| 514 |
begin |
| 515 |
SetLength(self.f_Items, count); |
| 516 |
end; |
| 517 |
|
| 518 |
procedure TIntegerHash.FClearItems; |
| 519 |
begin |
| 520 |
SetLength(self.f_Items, 0); |
| 521 |
end; |
| 522 |
|
| 523 |
{ TObjectHash } |
| 524 |
|
| 525 |
procedure TObjectHash.FDeleteIndex(i: integer); |
| 526 |
begin |
| 527 |
self.f_Items[i].Free; |
| 528 |
self.f_Items[i] := nil; |
| 529 |
end; |
| 530 |
|
| 531 |
function TObjectHash.FGetItem(const Key: string): TObject; |
| 532 |
var |
| 533 |
k, x: integer; |
| 534 |
begin |
| 535 |
if (self.FFindKey(Key, k, x)) then |
| 536 |
result := self.f_Items[self.f_Keys[k][x].ItemIndex] |
| 537 |
else |
| 538 |
raise EHashFindError.CreateFmt('Key "%s" not found', [Key]); |
| 539 |
end; |
| 540 |
|
| 541 |
procedure TObjectHash.FMoveIndex(oldIndex, newIndex: integer); |
| 542 |
begin |
| 543 |
self.f_Items[newIndex] := self.f_Items[oldIndex]; |
| 544 |
end; |
| 545 |
|
| 546 |
procedure TObjectHash.FSetItem(const Key: string; Value: TObject); |
| 547 |
var |
| 548 |
k, x, i: integer; |
| 549 |
begin |
| 550 |
if (self.FFindKey(Key, k, x)) then begin |
| 551 |
self.f_Items[self.f_Keys[k][x].ItemIndex].Free; |
| 552 |
self.f_Items[self.f_Keys[k][x].ItemIndex] := Value; |
| 553 |
end else begin |
| 554 |
{ New index entry, or recycle an old one. } |
| 555 |
i := self.FAllocItemIndex; |
| 556 |
if (i > High(self.f_Items)) then |
| 557 |
SetLength(self.f_Items, i + 1); |
| 558 |
self.f_Items[i] := Value; |
| 559 |
{ Add it to the hash. } |
| 560 |
SetLength(self.f_Keys[k], Length(self.f_Keys[k]) + 1); |
| 561 |
self.f_Keys[k][High(self.f_Keys[k])].Key := Key; |
| 562 |
self.f_Keys[k][High(self.f_Keys[k])].ItemIndex := i; |
| 563 |
self.f_Keys[k][High(self.f_Keys[k])].Hash := HashThis(Key); |
| 564 |
{ Hash has been modified, so disallow Next. } |
| 565 |
self.f_NextAllowed := false; |
| 566 |
end; |
| 567 |
end; |
| 568 |
|
| 569 |
function TObjectHash.FIndexMax: integer; |
| 570 |
begin |
| 571 |
result := High(self.f_Items); |
| 572 |
end; |
| 573 |
|
| 574 |
procedure TObjectHash.FTrimIndexes(count: integer); |
| 575 |
begin |
| 576 |
SetLength(self.f_Items, count); |
| 577 |
end; |
| 578 |
|
| 579 |
procedure TObjectHash.FClearItems; |
| 580 |
var |
| 581 |
i: integer; |
| 582 |
begin |
| 583 |
for i := 0 to High(self.f_Items) do |
| 584 |
if (Assigned(self.f_Items[i])) then |
| 585 |
self.f_Items[i].Free; |
| 586 |
SetLength(self.f_Items, 0); |
| 587 |
end; |
| 588 |
|
| 589 |
destructor TObjectHash.Destroy; |
| 590 |
var |
| 591 |
i: integer; |
| 592 |
begin |
| 593 |
for i := 0 to High(self.f_Items) do |
| 594 |
if (Assigned(self.f_Items[i])) then |
| 595 |
self.f_Items[i].Free; |
| 596 |
inherited; |
| 597 |
end; |
| 598 |
end. |
| 599 |
|