X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2Fbinheap.pas;h=4b4a64866d010f25c75a4dcd01688f7d25bc5071;hb=08095f30d97fe8ca473173173df6158aa352f64a;hp=50531ed6f453d79044597739bf48d813dfe96c6f;hpb=cac277b4226c3de6401dae59c91dd35b77625304;p=d2df-sdl.git diff --git a/src/shared/binheap.pas b/src/shared/binheap.pas index 50531ed..4b4a648 100644 --- a/src/shared/binheap.pas +++ b/src/shared/binheap.pas @@ -1,4 +1,4 @@ -(* Copyright (C) DooM 2D:Forever Developers +(* Copyright (C) Doom 2D: Forever Developers * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,23 +20,22 @@ unit binheap; interface +(* + * CmpObjT: class that contains class methods: + * class function less (const[ref] a, b: KeyT): Boolean; + *) type // WARNING! don't put structures into heap, use ponters or ids! - generic TBinaryHeapBase = class(TObject) - private - type - TBinaryHeapLessFn = function (a, b: ITP): Boolean; - + generic TBinaryHeapBase = class(TObject) private elem: array of ITP; elemUsed: Integer; - lessfn: TBinaryHeapLessFn; private procedure heapify (root: Integer); public - constructor Create (alessfn: TBinaryHeapLessFn); + constructor Create (); destructor Destroy (); override; procedure clear (); @@ -51,12 +50,20 @@ type type - TBinaryHeapObj = specialize TBinaryHeapBase; - TBinaryHeapInt = specialize TBinaryHeapBase; + TBinHeapKeyIntLess = class + public + class function less (const a, b: Integer): Boolean; inline; + end; + + TBinHeapKeyIntGreat = class + public + class function less (const a, b: Integer): Boolean; inline; + end; -function binHeapNewIntLess (): TBinaryHeapInt; -function binHeapNewIntGreat (): TBinaryHeapInt; +type + TBinaryHeapIntLess = specialize TBinaryHeapBase; + TBinaryHeapIntGreat = specialize TBinaryHeapBase; implementation @@ -66,19 +73,13 @@ uses // ////////////////////////////////////////////////////////////////////////// // -function intLess (a, b: Integer): Boolean; begin result := (a < b); end; -function intGreat (a, b: Integer): Boolean; begin result := (a > b); end; - - -function binHeapNewIntLess (): TBinaryHeapInt; begin result := TBinaryHeapInt.Create(@intLess); end; -function binHeapNewIntGreat (): TBinaryHeapInt; begin result := TBinaryHeapInt.Create(@intGreat); end; +class function TBinHeapKeyIntLess.less (const a, b: Integer): Boolean; inline; begin result := (a < b); end; +class function TBinHeapKeyIntGreat.less (const a, b: Integer): Boolean; inline; begin result := (a > b); end; // ////////////////////////////////////////////////////////////////////////// // -constructor TBinaryHeapBase.Create (alessfn: TBinaryHeapLessFn); +constructor TBinaryHeapBase.Create (); begin - if not assigned(alessfn) then raise Exception.Create('wutafuck?!'); - lessfn := alessfn; SetLength(elem, 128); // 'cause why not? elemUsed := 0; end; @@ -107,8 +108,8 @@ begin smallest := 2*root+1; // left child if (smallest >= elemUsed) then break; // anyway right := smallest+1; // right child - if not lessfn(elem[smallest], elem[root]) then smallest := root; - if (right < elemUsed) and (lessfn(elem[right], elem[smallest])) then smallest := right; + if not CmpObjT.less(elem[smallest], elem[root]) then smallest := root; + if (right < elemUsed) and (CmpObjT.less(elem[right], elem[smallest])) then smallest := right; if (smallest = root) then break; // swap tmp := elem[root]; @@ -137,7 +138,7 @@ begin while (i <> 0) do begin par := (i-1) div 2; // parent - if not lessfn(val, elem[par]) then break; + if not CmpObjT.less(val, elem[par]) then break; elem[i] := elem[par]; i := par; end;