Debian Codegraph @

src/compatibility/delphi-only/FPHashCompatibility.pas

raw
1 {
2 Based on Contnrs from FPC:
3
4 This file is part of the Free Component Library (FCL)
5 Copyright (c) 2002 by Florian Klaempfl
6
7 See the file COPYING.FPC, included in this distribution,
8 for details about the copyright.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 **********************************************************************
15 }
16
17 unit FPHashCompatibility;
18
19 interface
20
21 uses
22 SysUtils,Classes, Types;
23
24 type
25 TObjectListCallback = Procedure(data:TObject;arg:pointer) of object;
26 TObjectListStaticCallback = Procedure(data:TObject;arg:pointer);
27 TListCallback = procedure(data,arg:pointer) of object;
28 TListStaticCallback = procedure(data,arg:pointer);
29
30
31 THashItem=record
32 HashValue : UInt32;
33 StrIndex : Integer;
34 NextIndex : Integer;
35 Data : Pointer;
36 end;
37 PHashItem=^THashItem;
38
39 const
40 MaxHashListSize = Maxint div SizeOf(THashItem);
41 MaxHashStrSize = Maxint;
42 MaxHashTableSize = Maxint div SizeOf(Integer);
43
44 MaxItemsPerHash = 3;
45 SListIndexError = 'List index (%d) out of bounds';
46 SListCapacityError = 'List capacity (%d) exceeded.';
47 SListCountError = 'List count (%d) out of bounds.';
48
49 type
50 PHashItemList = ^THashItemList;
51 THashItemList = array[0..MaxHashListSize - 1] of THashItem;
52 PHashTable = ^THashTable;
53 THashTable = array[0..MaxHashTableSize - 1] of Integer;
54
55 TFPHashList = class(TObject)
56 private
57 { ItemList }
58 FHashList : PHashItemList;
59 FCount,
60 FCapacity : Integer;
61 { Hash }
62 FHashTable : PHashTable;
63 FHashCapacity : Integer;
64 { Strings }
65 FStrs : PAnsiChar;
66 FStrCount,
67 FStrCapacity : Integer;
68 Function InternalFind(AHash:UInt32;const AName:shortstring;out PrevIndex:Integer):Integer;
69 protected
70 Function Get(Index: Integer): Pointer; {$ifdef CCLASSESINLINE}inline;{$endif}
71 Procedure Put(Index: Integer; Item: Pointer); {$ifdef CCLASSESINLINE}inline;{$endif}
72 Procedure SetCapacity(NewCapacity: Integer);
73 Procedure SetCount(NewCount: Integer);
74 Procedure RaiseIndexError(Index : Integer);
75 Function AddStr(const s:shortstring): Integer;
76 Procedure AddToHashTable(Index: Integer);
77 Procedure StrExpand(MinIncSize:Integer);
78 Procedure SetStrCapacity(NewCapacity: Integer);
79 Procedure SetHashCapacity(NewCapacity: Integer);
80 Procedure ReHash;
81 public
82 constructor Create;
83 destructor Destroy; override;
84 Function Add(const AName:shortstring;Item: Pointer): Integer;
85 Procedure Clear;
86 Function NameOfIndex(Index: Integer): ShortString; {$ifdef CCLASSESINLINE}inline;{$endif}
87 Function HashOfIndex(Index: Integer): UInt32; {$ifdef CCLASSESINLINE}inline;{$endif}
88 Function GetNextCollision(Index: Integer): Integer;
89 Procedure Delete(Index: Integer);
90 class Procedure Error(const Msg: string; Data: NativeInt);
91 Function Expand: TFPHashList;
92 Function Extract(item: Pointer): Pointer;
93 Function IndexOf(Item: Pointer): Integer;
94 Function Find(const AName:shortstring): Pointer;
95 Function FindIndexOf(const AName:shortstring): Integer;
96 Function FindWithHash(const AName:shortstring;AHash:UInt32): Pointer;
97 Function Rename(const AOldName,ANewName:shortstring): Integer;
98 Function Remove(Item: Pointer): Integer;
99 Procedure Pack;
100 Procedure ShowStatistics;
101 Procedure ForEachCall(proc2call:TListCallback;arg:pointer); overload;
102 Procedure ForEachCall(proc2call:TListStaticCallback;arg:pointer); overload;
103 property Capacity: Integer read FCapacity write SetCapacity;
104 property Count: Integer read FCount write SetCount;
105 property Items[Index: Integer]: Pointer read Get write Put; default;
106 property List: PHashItemList read FHashList;
107 property Strs: PAnsiChar read FStrs;
108 end;
109
110
111 {*******************************************************
112 TFPHashObjectList (From fcl/inc/contnrs.pp)
113 ********************************************************}
114
115 TFPHashObjectList = class;
116
117 { TFPHashObject }
118
119 TFPHashObject = class
120 private
121 FOwner : TFPHashObjectList;
122 FCachedStr : pshortstring;
123 FStrIndex : Integer;
124 Procedure InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:shortstring);
125 protected
126 Function GetName:shortstring;virtual;
127 Function GetHash:UInt32;virtual;
128 public
129 constructor CreateNotOwned;
130 constructor Create(HashObjectList:TFPHashObjectList;const s:shortstring);
131 Procedure ChangeOwner(HashObjectList:TFPHashObjectList); {$ifdef CCLASSESINLINE}inline;{$endif}
132 Procedure ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:shortstring); {$ifdef CCLASSESINLINE}inline;{$endif}
133 Procedure Rename(const ANewName:shortstring);
134 property Name:shortstring read GetName;
135 property Hash:UInt32 read GetHash;
136 end;
137
138 TFPHashObjectList = class(TObject)
139 private
140 FFreeObjects : Boolean;
141 FHashList: TFPHashList;
142 Function GetCount: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
143 Procedure SetCount(const AValue: integer); {$ifdef CCLASSESINLINE}inline;{$endif}
144 protected
145 Function GetItem(Index: Integer): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
146 Procedure SetItem(Index: Integer; AObject: TObject); {$ifdef CCLASSESINLINE}inline;{$endif}
147 Procedure SetCapacity(NewCapacity: Integer); {$ifdef CCLASSESINLINE}inline;{$endif}
148 Function GetCapacity: integer; {$ifdef CCLASSESINLINE}inline;{$endif}
149 public
150 constructor Create(FreeObjects : boolean = True);
151 destructor Destroy; override;
152 Procedure Clear;
153 Function Add(const AName:shortstring;AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
154 Function NameOfIndex(Index: Integer): ShortString; {$ifdef CCLASSESINLINE}inline;{$endif}
155 Function HashOfIndex(Index: Integer): UInt32; {$ifdef CCLASSESINLINE}inline;{$endif}
156 Function GetNextCollision(Index: Integer): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
157 Procedure Delete(Index: Integer);
158 Function Expand: TFPHashObjectList; {$ifdef CCLASSESINLINE}inline;{$endif}
159 Function Extract(Item: TObject): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
160 Function Remove(AObject: TObject): Integer;
161 Function IndexOf(AObject: TObject): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
162 Function Find(const s:shortstring): TObject; {$ifdef CCLASSESINLINE}inline;{$endif}
163 Function FindIndexOf(const s:shortstring): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
164 Function FindWithHash(const AName:shortstring;AHash:UInt32): Pointer;
165 Function Rename(const AOldName,ANewName:shortstring): Integer; {$ifdef CCLASSESINLINE}inline;{$endif}
166 Function FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt: Integer): Integer;
167 Procedure Pack; {$ifdef CCLASSESINLINE}inline;{$endif}
168 Procedure ShowStatistics; {$ifdef CCLASSESINLINE}inline;{$endif}
169 Procedure ForEachCall(proc2call:TObjectListCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif} overload;
170 Procedure ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer); {$ifdef CCLASSESINLINE}inline;{$endif} overload;
171 property Capacity: Integer read GetCapacity write SetCapacity;
172 property Count: Integer read GetCount write SetCount;
173 property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
174 property Items[Index: Integer]: TObject read GetItem write SetItem; default;
175 property List: TFPHashList read FHashList;
176 end;
177
178 implementation
179
180 uses CastleUtils;
181
182 {*****************************************************************************
183 TFPHashList
184 *****************************************************************************}
185
186 Function FPHash(const s:shortstring):UInt32; overload;
187 var
188 p,pmax : PAnsiChar;
189 begin
190 {$I norqcheckbegin.inc}
191 Result:=0;
192 p:=@s[1];
193 pmax:=@s[length(s)+1];
194 while (p<pmax) do
195 begin
196 Result:=UInt32(Int32(Result shl 5) - Int32(Result)) xor UInt32(P^);
197 Inc(p);
198 end;
199 {$I norqcheckend.inc}
200 end;
201
202 Function FPHash(P: PAnsiChar; Len: Integer): UInt32; overload;
203 var
204 pmax : PAnsiChar;
205 begin
206 {$I norqcheckbegin.inc}
207 Result:=0;
208 pmax:=p+len;
209 while (p<pmax) do
210 begin
211 Result:=UInt32(Int32(Result shl 5) - Int32(Result)) xor UInt32(P^);
212 Inc(p);
213 end;
214 {$I norqcheckend.inc}
215 end;
216
217
218 Procedure TFPHashList.RaiseIndexError(Index : Integer);
219 begin
220 Error(SListIndexError, Index);
221 end;
222
223
224 Function TFPHashList.Get(Index: Integer): Pointer;
225 begin
226 If (Index < 0) or (Index >= FCount) then
227 RaiseIndexError(Index);
228 Result:=FHashList^[Index].Data;
229 end;
230
231
232 Procedure TFPHashList.Put(Index: Integer; Item: Pointer);
233 begin
234 if (Index < 0) or (Index >= FCount) then
235 RaiseIndexError(Index);
236 FHashList^[Index].Data:=Item;
237 end;
238
239
240 Function TFPHashList.NameOfIndex(Index: Integer): shortstring;
241 begin
242 if (Index < 0) or (Index >= FCount) then
243 RaiseIndexError(Index);
244 with FHashList^[Index] do
245 begin
246 if StrIndex>=0 then
247 Result:=PShortString(@FStrs[StrIndex])^
248 else
249 Result:='';
250 end;
251 end;
252
253
254 Function TFPHashList.HashOfIndex(Index: Integer): UInt32;
255 begin
256 If (Index < 0) or (Index >= FCount) then
257 RaiseIndexError(Index);
258 Result:=FHashList^[Index].HashValue;
259 end;
260
261
262 Function TFPHashList.GetNextCollision(Index: Integer): Integer;
263 begin
264 Result:=-1;
265 if ((Index > -1) and (Index < FCount)) then
266 Result:=FHashList^[Index].NextIndex;
267 end;
268
269
270 Function TFPHashList.Extract(item: Pointer): Pointer;
271 var
272 i : Integer;
273 begin
274 Result:=nil;
275 i:=IndexOf(item);
276 if i >= 0 then
277 begin
278 Result:=item;
279 Delete(i);
280 end;
281 end;
282
283
284 Procedure TFPHashList.SetCapacity(NewCapacity: Integer);
285 begin
286 if (NewCapacity < FCount) or (NewCapacity > MaxHashListSize) then
287 Error (SListCapacityError, NewCapacity);
288 if NewCapacity = FCapacity then
289 Exit;
290 ReallocMem(FHashList, NewCapacity*SizeOf(THashItem));
291 FCapacity:=NewCapacity;
292 { Maybe expand hash also }
293 if FCapacity>FHashCapacity*MaxItemsPerHash then
294 SetHashCapacity(FCapacity div MaxItemsPerHash);
295 end;
296
297
298 Procedure TFPHashList.SetCount(NewCount: Integer);
299 begin
300 if (NewCount < 0) or (NewCount > MaxHashListSize)then
301 Error(SListCountError, NewCount);
302 if NewCount > FCount then
303 begin
304 if NewCount > FCapacity then
305 SetCapacity(NewCount);
306 if FCount < NewCount then
307 FillChar(FHashList^[FCount], (NewCount-FCount) div SizeOf(THashItem), 0);
308 end;
309 FCount:=NewCount;
310 end;
311
312
313 Procedure TFPHashList.SetStrCapacity(NewCapacity: Integer);
314 begin
315 {$warn COMPARISON_FALSE off}
316 if (NewCapacity < FStrCount) or (NewCapacity > MaxHashStrSize) then
317 Error(SListCapacityError, NewCapacity);
318 {$warn COMPARISON_FALSE on}
319 if NewCapacity = FStrCapacity then
320 Exit;
321 ReallocMem(FStrs, NewCapacity);
322 FStrCapacity:=NewCapacity;
323 end;
324
325
326 Procedure TFPHashList.SetHashCapacity(NewCapacity: Integer);
327 begin
328 if (NewCapacity < 1) then
329 Error(SListCapacityError, NewCapacity);
330 if FHashCapacity=NewCapacity then
331 Exit;
332 FHashCapacity:=NewCapacity;
333 ReallocMem(FHashTable, FHashCapacity*SizeOf(Integer));
334 ReHash;
335 end;
336
337
338 Procedure TFPHashList.ReHash;
339 var
340 i : Integer;
341 begin
342 FillDword(FHashTable^, FHashCapacity, High(UInt32));
343 for i:=0 to FCount-1 do
344 AddToHashTable(i);
345 end;
346
347
348 constructor TFPHashList.Create;
349 begin
350 SetHashCapacity(1);
351 end;
352
353
354 destructor TFPHashList.Destroy;
355 begin
356 Clear;
357 if Assigned(FHashTable) then
358 FreeMem(FHashTable);
359 inherited Destroy;
360 end;
361
362
363 Function TFPHashList.AddStr(const s:shortstring): Integer;
364 var
365 Len : Integer;
366 begin
367 len:=Length(s)+1;
368 if FStrCount+Len >= FStrCapacity then
369 StrExpand(Len);
370 System.Move(s[0],FStrs[FStrCount],Len);
371 Result:=FStrCount;
372 Inc(FStrCount,Len);
373 end;
374
375
376 Procedure TFPHashList.AddToHashTable(Index: Integer);
377 var
378 HashIndex : Integer;
379 begin
380 with FHashList^[Index] do
381 begin
382 if not Assigned(Data) then
383 Exit;
384 HashIndex:=HashValue mod UInt32(FHashCapacity);
385 NextIndex:=FHashTable^[HashIndex];
386 FHashTable^[HashIndex]:=Index;
387 end;
388 end;
389
390
391 Function TFPHashList.Add(const AName:shortstring;Item: Pointer): Integer;
392 begin
393 if FCount = FCapacity then
394 Expand;
395 with FHashList^[FCount] do
396 begin
397 HashValue:=FPHash(AName);
398 Data:=Item;
399 StrIndex:=AddStr(AName);
400 end;
401 AddToHashTable(FCount);
402 Result:=FCount;
403 Inc(FCount);
404 end;
405
406 Procedure TFPHashList.Clear;
407 begin
408 if Assigned(FHashList) then
409 begin
410 FCount:=0;
411 SetCapacity(0);
412 FHashList:=nil;
413 end;
414 SetHashCapacity(1);
415 FHashTable^[0]:=(-1); // sethashcapacity does not always call rehash
416 if Assigned(FStrs) then
417 begin
418 FStrCount:=0;
419 SetStrCapacity(0);
420 FStrs:=nil;
421 end;
422 end;
423
424 Procedure TFPHashList.Delete(Index: Integer);
425 begin
426 if (Index<0) or (Index>=FCount) then
427 Error(SListIndexError, Index);
428 { Remove from HashList }
429 Dec(FCount);
430 System.Move(FHashList^[Index+1], FHashList^[Index], (FCount - Index) * SizeOf(THashItem));
431 { All indexes are updated, we need to build the hashtable again }
432 ReHash;
433 { Shrink the list if appropriate }
434 if (FCapacity > 256) and (FCount < FCapacity shr 2) then
435 begin
436 FCapacity:=FCapacity shr 1;
437 ReAllocMem(FHashList, SizeOf(THashItem) * FCapacity);
438 end;
439 end;
440
441 Function TFPHashList.Remove(Item: Pointer): Integer;
442 begin
443 Result:=IndexOf(Item);
444 If Result <> -1 then
445 Self.Delete(Result);
446 end;
447
448 class Procedure TFPHashList.Error(const Msg: string; Data: PtrInt);
449 begin
450 ///raise EListError.CreateFmt(Msg,[Data]) at get_caller_addr(get_frame), get_caller_frame(get_frame);
451 raise EListError.Create(Msg);
452 end;
453
454 Function TFPHashList.Expand: TFPHashList;
455 var
456 IncSize : Int32;
457 begin
458 Result:=Self;
459 if FCount < FCapacity then
460 Exit;
461 IncSize:=SizeOf(PtrInt)*2;
462 if FCapacity > 127 then
463 Inc(IncSize, FCapacity shr 2)
464 else if FCapacity > SizeOf(PtrInt)*3 then
465 Inc(IncSize, FCapacity shr 1)
466 else if FCapacity >= SizeOf(PtrInt) then
467 Inc(IncSize,sizeof(PtrInt));
468 SetCapacity(FCapacity + IncSize);
469 end;
470
471 Procedure TFPHashList.StrExpand(MinIncSize:Integer);
472 var
473 IncSize : Int32;
474 begin
475 if FStrCount+MinIncSize < FStrCapacity then
476 Exit;
477 IncSize:=64;
478 if FStrCapacity > 255 then
479 Inc(IncSize, FStrCapacity shr 2);
480 SetStrCapacity(FStrCapacity + IncSize + MinIncSize);
481 end;
482
483 Function TFPHashList.IndexOf(Item: Pointer): Integer;
484 var
485 psrc : PHashItem;
486 Index : integer;
487 begin
488 Result:=-1;
489 psrc:=@FHashList^[0];
490 for Index:=0 to FCount-1 do
491 begin
492 if psrc^.Data=Item then
493 begin
494 Result:=Index;
495 Exit;
496 end;
497 Inc(psrc);
498 end;
499 end;
500
501 Function TFPHashList.InternalFind(AHash:UInt32;const AName:shortstring;out PrevIndex:Integer):Integer;
502 var
503 HashIndex : Integer;
504 Len,
505 LastChar : AnsiChar;
506 begin
507 HashIndex:=AHash mod UInt32(FHashCapacity);
508 Result:=FHashTable^[HashIndex];
509 Len:=AnsiChar(Length(AName));
510 LastChar:=AName[Byte(Len)];
511 PrevIndex:=-1;
512 while Result<>-1 do
513 with FHashList^[Result] do
514 begin
515 if Assigned(Data) and
516 (HashValue=AHash) and
517 (Len=FStrs[StrIndex]) and
518 (LastChar=FStrs[StrIndex+Byte(Len)]) and
519 (AName=PShortString(@FStrs[StrIndex])^) then
520 Exit;
521 PrevIndex:=Result;
522 Result:=NextIndex;
523 end;
524 end;
525
526
527 Function TFPHashList.Find(const AName:shortstring): Pointer;
528 var
529 Index,
530 PrevIndex : Integer;
531 begin
532 Result:=nil;
533 Index:=InternalFind(FPHash(AName),AName,PrevIndex);
534 if Index=-1 then
535 Exit;
536 Result:=FHashList^[Index].Data;
537 end;
538
539
540 Function TFPHashList.FindIndexOf(const AName:shortstring): Integer;
541 var
542 PrevIndex : Integer;
543 begin
544 Result:=InternalFind(FPHash(AName),AName,PrevIndex);
545 end;
546
547
548 Function TFPHashList.FindWithHash(const AName:shortstring;AHash:UInt32): Pointer;
549 var
550 Index,
551 PrevIndex : Integer;
552 begin
553 Result:=nil;
554 Index:=InternalFind(AHash,AName,PrevIndex);
555 if Index=-1 then
556 Exit;
557 Result:=FHashList^[Index].Data;
558 end;
559
560
561 Function TFPHashList.Rename(const AOldName,ANewName:shortstring): Integer;
562 var
563 PrevIndex,
564 Index : Integer;
565 OldHash : UInt32;
566 begin
567 Result:=-1;
568 OldHash:=FPHash(AOldName);
569 Index:=InternalFind(OldHash,AOldName,PrevIndex);
570 if Index=-1 then
571 Exit;
572 { Remove from current Hash }
573 if PrevIndex<>-1 then
574 FHashList^[PrevIndex].NextIndex:=FHashList^[Index].NextIndex
575 else
576 FHashTable^[OldHash mod UInt32(FHashCapacity)]:=FHashList^[Index].NextIndex;
577 { Set new name and hash }
578 with FHashList^[Index] do
579 begin
580 HashValue:=FPHash(ANewName);
581 StrIndex:=AddStr(ANewName);
582 end;
583 { Insert back in Hash }
584 AddToHashTable(Index);
585 { Return Index }
586 Result:=Index;
587 end;
588
589 Procedure TFPHashList.Pack;
590 var
591 NewCount,
592 i : integer;
593 pdest,
594 psrc : PHashItem;
595 FOldStr : PAnsichar;
596 begin
597 NewCount:=0;
598 psrc:=@FHashList^[0];
599 FOldStr:=FStrs;
600 try
601 FStrs:=nil;
602 FStrCount:=0;
603 FStrCapacity:=0;
604 pdest:=psrc;
605 for I:=0 to FCount-1 do
606 begin
607 if Assigned(psrc^.Data) then
608 begin
609 pdest^:=psrc^;
610 pdest^.StrIndex:=AddStr(PShortString(@FOldStr[PDest^.StrIndex])^);
611 Inc(pdest);
612 Inc(NewCount);
613 end;
614 Inc(psrc);
615 end;
616 finally
617 FreeMem(FoldStr);
618 end;
619 FCount:=NewCount;
620 { We need to ReHash to update the IndexNext }
621 ReHash;
622 { Release over-capacity }
623 SetCapacity(FCount);
624 SetStrCapacity(FStrCount);
625 end;
626
627
628 Procedure TFPHashList.ShowStatistics;
629 var
630 HashMean,
631 HashStdDev : Double;
632 Index,
633 i,j : Integer;
634 begin
635 { Calculate Mean and StdDev }
636 HashMean:=0;
637 HashStdDev:=0;
638 for i:=0 to FHashCapacity-1 do
639 begin
640 j:=0;
641 Index:=FHashTable^[i];
642 while (Index<>-1) do
643 begin
644 Inc(j);
645 Index:=FHashList^[Index].NextIndex;
646 end;
647 HashMean:=HashMean+j;
648 HashStdDev:=HashStdDev+Sqr(j);
649 end;
650 HashMean:=HashMean/FHashCapacity;
651 HashStdDev:=(HashStdDev-FHashCapacity*Sqr(HashMean));
652 if FHashCapacity>1 then
653 HashStdDev:=Sqrt(HashStdDev/(FHashCapacity-1))
654 else
655 HashStdDev:=0;
656 { Print info to stdout }
657 Writeln('HashSize : ',FHashCapacity);
658 Writeln('HashMean : ',HashMean:1:4);
659 Writeln('HashStdDev : ',HashStdDev:1:4);
660 Writeln('ListSize : ',FCount,'/',FCapacity);
661 Writeln('StringSize : ',FStrCount,'/',FStrCapacity);
662 end;
663
664
665 Procedure TFPHashList.ForEachCall(proc2call:TListCallback;arg:pointer);
666 var
667 i : integer;
668 p : pointer;
669 begin
670 for i:=0 to Count-1 Do
671 begin
672 p:=FHashList^[i].Data;
673 if Assigned(p) then
674 proc2call(p,arg);
675 end;
676 end;
677
678
679 Procedure TFPHashList.ForEachCall(proc2call:TListStaticCallback;arg:pointer);
680 var
681 i : integer;
682 p : pointer;
683 begin
684 for i:=0 to Count-1 Do
685 begin
686 p:=FHashList^[i].Data;
687 if Assigned(p) then
688 proc2call(p,arg);
689 end;
690 end;
691
692
693 {*****************************************************************************
694 TFPHashObject
695 *****************************************************************************}
696
697 Procedure TFPHashObject.InternalChangeOwner(HashObjectList:TFPHashObjectList;const s:shortstring);
698 var
699 Index : integer;
700 begin
701 FOwner:=HashObjectList;
702 Index:=HashObjectList.Add(s,Self);
703 FStrIndex:=HashObjectList.List.List^[Index].StrIndex;
704 FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
705 end;
706
707
708 constructor TFPHashObject.CreateNotOwned;
709 begin
710 FStrIndex:=-1;
711 end;
712
713
714 constructor TFPHashObject.Create(HashObjectList:TFPHashObjectList;const s:shortstring);
715 begin
716 InternalChangeOwner(HashObjectList,s);
717 end;
718
719
720 Procedure TFPHashObject.ChangeOwner(HashObjectList:TFPHashObjectList);
721 begin
722 InternalChangeOwner(HashObjectList,PShortString(@FOwner.List.Strs[FStrIndex])^);
723 end;
724
725
726 Procedure TFPHashObject.ChangeOwnerAndName(HashObjectList:TFPHashObjectList;const s:shortstring);
727 begin
728 InternalChangeOwner(HashObjectList,s);
729 end;
730
731
732 Procedure TFPHashObject.Rename(const ANewName:shortstring);
733 var
734 Index : integer;
735 begin
736 Index:=FOwner.Rename(PShortString(@FOwner.List.Strs[FStrIndex])^,ANewName);
737 if Index<>-1 then
738 begin
739 FStrIndex:=FOwner.List.List^[Index].StrIndex;
740 FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
741 end;
742 end;
743
744
745 Function TFPHashObject.GetName:shortstring;
746 begin
747 if FOwner<>nil then
748 begin
749 FCachedStr:=PShortString(@FOwner.List.Strs[FStrIndex]);
750 Result:=FCachedStr^;
751 end
752 else
753 Result:='';
754 end;
755
756
757 Function TFPHashObject.GetHash:UInt32;
758 begin
759 if FOwner<>nil then
760 Result:=FPHash(PShortString(@FOwner.List.Strs[FStrIndex])^)
761 else
762 Result:=$ffffffff;
763 end;
764
765
766 {*****************************************************************************
767 TFPHashObjectList (Copied from rtl/objpas/classes/lists.inc)
768 *****************************************************************************}
769
770 constructor TFPHashObjectList.Create(FreeObjects : boolean = True);
771 begin
772 inherited Create;
773 FHashList:=TFPHashList.Create;
774 FFreeObjects:=Freeobjects;
775 end;
776
777 destructor TFPHashObjectList.Destroy;
778 begin
779 if (FHashList <> nil) then
780 begin
781 Clear;
782 FHashList.Destroy;
783 end;
784 inherited Destroy;
785 end;
786
787 Procedure TFPHashObjectList.Clear;
788 var
789 i: integer;
790 begin
791 if FFreeObjects then
792 for i:=0 to FHashList.Count - 1 do
793 TObject(FHashList[i]).Free;
794 FHashList.Clear;
795 end;
796
797 Function TFPHashObjectList.GetCount: integer;
798 begin
799 Result:=FHashList.Count;
800 end;
801
802 Procedure TFPHashObjectList.SetCount(const AValue: integer);
803 begin
804 if FHashList.Count <> AValue then
805 FHashList.Count:=AValue;
806 end;
807
808 Function TFPHashObjectList.GetItem(Index: Integer): TObject;
809 begin
810 Result:=TObject(FHashList[Index]);
811 end;
812
813 Procedure TFPHashObjectList.SetItem(Index: Integer; AObject: TObject);
814 begin
815 if OwnsObjects then
816 TObject(FHashList[Index]).Free;
817 FHashList[Index]:=AObject;
818 end;
819
820 Procedure TFPHashObjectList.SetCapacity(NewCapacity: Integer);
821 begin
822 FHashList.Capacity:=NewCapacity;
823 end;
824
825 Function TFPHashObjectList.GetCapacity: integer;
826 begin
827 Result:=FHashList.Capacity;
828 end;
829
830 Function TFPHashObjectList.Add(const AName:shortstring;AObject: TObject): Integer;
831 begin
832 Result:=FHashList.Add(AName,AObject);
833 end;
834
835 Function TFPHashObjectList.NameOfIndex(Index: Integer): shortstring;
836 begin
837 Result:=FHashList.NameOfIndex(Index);
838 end;
839
840 Function TFPHashObjectList.HashOfIndex(Index: Integer): UInt32;
841 begin
842 Result:=FHashList.HashOfIndex(Index);
843 end;
844
845 Function TFPHashObjectList.GetNextCollision(Index: Integer): Integer;
846 begin
847 Result:=FHashList.GetNextCollision(Index);
848 end;
849
850 Procedure TFPHashObjectList.Delete(Index: Integer);
851 begin
852 if OwnsObjects then
853 TObject(FHashList[Index]).Free;
854 FHashList.Delete(Index);
855 end;
856
857 Function TFPHashObjectList.Expand: TFPHashObjectList;
858 begin
859 FHashList.Expand;
860 Result:=Self;
861 end;
862
863 Function TFPHashObjectList.Extract(Item: TObject): TObject;
864 begin
865 Result:=TObject(FHashList.Extract(Item));
866 end;
867
868 Function TFPHashObjectList.Remove(AObject: TObject): Integer;
869 begin
870 Result:=IndexOf(AObject);
871 if (Result <> -1) then
872 begin
873 if OwnsObjects then
874 TObject(FHashList[Result]).Free;
875 FHashList.Delete(Result);
876 end;
877 end;
878
879 Function TFPHashObjectList.IndexOf(AObject: TObject): Integer;
880 begin
881 Result:=FHashList.IndexOf(Pointer(AObject));
882 end;
883
884
885 Function TFPHashObjectList.Find(const s:shortstring): TObject;
886 begin
887 Result:=TObject(FHashList.Find(s));
888 end;
889
890
891 Function TFPHashObjectList.FindIndexOf(const s:shortstring): Integer;
892 begin
893 Result:=FHashList.FindIndexOf(s);
894 end;
895
896
897 Function TFPHashObjectList.FindWithHash(const AName:shortstring;AHash:UInt32): Pointer;
898 begin
899 Result:=TObject(FHashList.FindWithHash(AName,AHash));
900 end;
901
902
903 Function TFPHashObjectList.Rename(const AOldName,ANewName:shortstring): Integer;
904 begin
905 Result:=FHashList.Rename(AOldName,ANewName);
906 end;
907
908
909 Function TFPHashObjectList.FindInstanceOf(AClass: TClass; AExact: Boolean; AStartAt : Integer): Integer;
910 var
911 I : Integer;
912 begin
913 I:=AStartAt;
914 Result:=-1;
915 if AExact then
916 while (I<Count) and (Result=-1) do
917 if Items[i].ClassType=AClass then
918 Result:=I
919 else
920 Inc(I)
921 else
922 while (I<Count) and (Result=-1) do
923 if Items[i].InheritsFrom(AClass) then
924 Result:=I
925 else
926 Inc(I);
927 end;
928
929
930 Procedure TFPHashObjectList.Pack;
931 begin
932 FHashList.Pack;
933 end;
934
935
936 Procedure TFPHashObjectList.ShowStatistics;
937 begin
938 FHashList.ShowStatistics;
939 end;
940
941
942 Procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListCallback;arg:pointer);
943 begin
944 FHashList.ForEachCall(TListCallBack(proc2call),arg);
945 end;
946
947
948 Procedure TFPHashObjectList.ForEachCall(proc2call:TObjectListStaticCallback;arg:pointer);
949 begin
950 FHashList.ForEachCall(TListStaticCallBack(proc2call),arg);
951 end;
952
953 end.