File: grbtree.pas

package info (click to toggle)
fpc 3.2.2%2Bdfsg-49
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 341,452 kB
  • sloc: pascal: 3,820,194; xml: 194,356; ansic: 9,637; asm: 8,482; java: 5,346; sh: 4,813; yacc: 3,956; makefile: 2,705; lex: 2,661; javascript: 2,454; sql: 929; php: 474; cpp: 145; perl: 136; sed: 132; csh: 34; tcl: 7
file content (669 lines) | stat: -rw-r--r-- 17,513 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
{   Red Black Tree implementation.

    Copyright (c) 2013 by Inoussa OUEDRAOGO

    Inspired by ideas of Julienne Walker
      see http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_bst1.aspx

    The source code is distributed under the Library GNU
    General Public License with the following modification:

        - object files and libraries linked into an application may be
          distributed without source code.

    If you didn't receive a copy of the file COPYING, contact:
          Free Software Foundation
          675 Mass Ave
          Cambridge, MA  02139
          USA

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. }

unit grbtree;

{$ifdef FPC}
  {$mode delphi}
  {$H+}
{$endif FPC}
{$TYPEDADDRESS ON}
{$define RB_DEBUG}

interface

const
  HEIGHT_LIMIT = 64;

type


   {KCOMP = class
    public
      // Return
      //    * if A>B then  1
      //    * if A=B then  0
      //    * if A<B then -1
      class function Compare(const A, B : TRBTreeNodeData) : Integer;
    end;    }

  TRBTree<T, KCOMP> = class
  public type
    TRBTreeNodeData = T;
    PRBTreeNode = ^TRBTreeNode;
    PRBTreeAllocator = ^TRBTreeAllocator;
    TRBTreeNode = record
      Links : array[Boolean] of PRBTreeNode;
      Data  : TRBTreeNodeData;
      Red   : Boolean;
    end;
    TRBTreeNodeCreator = function(AContext : Pointer) : PRBTreeNode;
    TRBTreeNodeDestructor = procedure(ANode : PRBTreeNode; AContext : Pointer);
    TRBTreeAllocator = record
      CreateNode : TRBTreeNodeCreator;
      FreeNode   : TRBTreeNodeDestructor;
    end;

    TRBTreeNodeComparator = KCOMP;
    ThisType = TRBTree<T,KCOMP>;

  private type
    TBaseIterator = record
      Tree         : ThisType;
      StartingNode : PRBTreeNode;
      StartingDir  : Boolean;
      Current      : PRBTreeNode;
      Top          : NativeInt;
      Path         : array[0..(HEIGHT_LIMIT-1)] of PRBTreeNode;
    end;
    PBaseIterator = ^TBaseIterator;
  public type

    TIterator = class
    private
      FHandle : PBaseIterator;
      FResetState : Boolean;
    private
    public
      constructor Create(AHandle : PBaseIterator);
      destructor Destroy;override;
      procedure Reset();
      function MoveNext() : Boolean;inline;
      function MovePrevious() : Boolean;inline;
      function GetCurrent : TRBTreeNodeData;inline;
      function GetCurrentNode : PRBTreeNode;inline;
    end;

  public var
    Root       : PRBTreeNode;
    //FSize      : Integer;
    Allocator  : TRBTreeAllocator;
    Comparator : TRBTreeNodeComparator;
  private
    class function TreeCreateIterator() : PBaseIterator;static;inline;
    class procedure TreeFreeIterator(AItem : PBaseIterator);static;inline;
    class procedure TreeInitIterator(
            AIterator     : PBaseIterator;
      const ATree         : ThisType;
      const AStartingNode : PRBTreeNode;
      const ADirection    : Boolean
    );static;
    class function TreeIteratorMove(
      AIterator     : PBaseIterator;
      ADirection    : Boolean
    ) : PRBTreeNode;static;
    class function TreeIteratorMoveNext(AIterator : PBaseIterator) : PRBTreeNode;static;inline;
    class function TreeIteratorMovePrevious(AIterator : PBaseIterator) : PRBTreeNode;static;inline;
    function CreateIterator(
      const ANode      : PRBTreeNode;
      const ADirection : Boolean
    ) : TIterator;inline;
  private
    class function DefaultCreateNode(AContext : Pointer) : PRBTreeNode;static;
    class procedure DefaultFreeNode(ANode : PRBTreeNode; AContext : Pointer);static;

    function InitNode(ANode : PRBTreeNode; AData : TRBTreeNodeData) : PRBTreeNode;inline;
    function IsRed(ANode : PRBTreeNode): Boolean;inline;
    function RotateDouble(ARoot : PRBTreeNode; const ADir : Boolean) : PRBTreeNode;inline;
    function RotateSingle(ARoot : PRBTreeNode; const ADir : Boolean) : PRBTreeNode;
  public
    constructor Create(const AAllocator  : PRBTreeAllocator);overload;
    constructor Create();overload;
    destructor Destroy;override;
    procedure Clear();
    function FindNode(const AData : TRBTreeNodeData) : PRBTreeNode;
    function Insert(const AData : TRBTreeNodeData) : PRBTreeNode;
    function Remove(const AData : TRBTreeNodeData) : Boolean;
    function CreateForwardIterator(const ANode : PRBTreeNode) : TIterator;overload;inline;
    function CreateForwardIterator() : TIterator;overload;inline;
    function CreateBackwardIterator(const ANode : PRBTreeNode) : TIterator;overload;inline;
    function CreateBackwardIterator() : TIterator;overload;inline;
{$ifdef RB_DEBUG}
    function SelfAssert(ARoot : PRBTreeNode; var AErrorMessage : string) : Boolean;overload;
    function SelfAssert(var AErrorMessage : string) : Boolean;overload;
{$endif RB_DEBUG}
  end;

  TOrdinalComparator<T> = class
  public type
    TOrdinalType = T;
  public
    // Return
    //    * if A>B then  1
    //    * if A=B then  0
    //    * if A<B then -1
    class function Compare(const A, B : TOrdinalType) : Integer;static;inline;
  end;

implementation

{ TRBTree<T> }

function TRBTree<T,KCOMP>.IsRed(ANode : PRBTreeNode): Boolean;inline;
begin
  Result := (ANode <> nil) and ANode^.Red;
end;

function TRBTree<T,KCOMP>.InitNode(ANode: PRBTreeNode; AData: TRBTreeNodeData): PRBTreeNode;inline;
begin
  Result := ANode;
  Result^.Data := AData;
  Result^.Red := True;
  Result^.Links[False] := nil;
  Result^.Links[True] := nil;
end;

function TRBTree<T,KCOMP>.RotateDouble(ARoot: PRBTreeNode; const ADir: Boolean): PRBTreeNode;inline;
begin
  ARoot^.Links[not ADir] := RotateSingle(ARoot^.Links[not ADir], not ADir );
  Result := RotateSingle(ARoot,ADir);
end;

function TRBTree<T,KCOMP>.RotateSingle(ARoot: PRBTreeNode; const ADir: Boolean): PRBTreeNode;
var
  t : PRBTreeNode;
begin
  t := ARoot^.Links[not ADir];

  ARoot^.Links[not ADir] := t^.Links[ADir];
  t^.Links[ADir] := ARoot;

  ARoot^.Red := True;
  t^.Red := False;

  Result := t;
end;

class function TRBTree<T,KCOMP>.TreeCreateIterator() : PBaseIterator;static;
begin
  Result := AllocMem(SizeOf(TBaseIterator));
end;

class procedure TRBTree<T,KCOMP>.TreeFreeIterator(AItem : PBaseIterator);static;
begin
  if (AItem <> nil) then
    FreeMem(AItem,SizeOf(AItem^));
end;

class procedure TRBTree<T,KCOMP>.TreeInitIterator(
        AIterator     : PBaseIterator;
  const ATree         : ThisType;
  const AStartingNode : PRBTreeNode;
  const ADirection    : Boolean
);static;
begin
  AIterator^.Tree := ATree;
  AIterator^.StartingNode := AStartingNode;
  AIterator^.StartingDir := ADirection;
  if (AStartingNode = nil) then
    AIterator^.Current := AIterator^.Tree.Root
  else
    AIterator^.Current := AStartingNode;
  AIterator^.Top := 0;

  // Save the path for later traversal
  if (AIterator^.Current <> nil) then begin
    while (AIterator^.Current^.Links[ADirection] <> nil) do begin
      AIterator^.Path[AIterator^.Top] := AIterator^.Current;
      Inc(AIterator^.Top);
      AIterator^.Current := AIterator^.Current^.Links[ADirection];
    end;
  end;
end;

class function TRBTree<T,KCOMP>.TreeIteratorMove(
  AIterator  : PBaseIterator;
  ADirection : Boolean
) : PRBTreeNode;static;
var
  last : PRBTreeNode;
begin
  Result := nil;
  if (AIterator^.Current = nil) then
    exit;

  if (AIterator^.Current^.Links[ADirection] <> nil) then begin
    // Continue down this branch
    AIterator^.Path[AIterator^.Top] := AIterator^.Current;
    Inc(AIterator^.Top);
    AIterator^.Current := AIterator^.Current^.Links[ADirection];

    while ( AIterator^.Current^.Links[not ADirection] <> nil) do begin
      AIterator^.Path[AIterator^.Top] := AIterator^.Current;
      Inc(AIterator^.Top);
      AIterator^.Current := AIterator^.Current^.Links[not ADirection];
    end;
  end else begin
    // Move to the next branch
    repeat
      if (AIterator^.Top = 0) then begin
        AIterator^.Current := nil;
        break;
      end;

      last := AIterator^.Current;
      Dec(AIterator^.Top);
      AIterator^.Current := AIterator^.Path[AIterator^.Top];
    until (last <> AIterator^.Current^.Links[ADirection]);
  end;

  Result := AIterator^.Current;
end;

class function TRBTree<T,KCOMP>.TreeIteratorMoveNext(
  AIterator : PBaseIterator
) : PRBTreeNode;static;
begin
  Result := TreeIteratorMove(AIterator,True);
end;

class function TRBTree<T,KCOMP>.TreeIteratorMovePrevious(
  AIterator : PBaseIterator
) : PRBTreeNode;static;
begin
  Result := TreeIteratorMove(AIterator,False);
end;

function TRBTree<T,KCOMP>.CreateIterator(
  const ANode      : PRBTreeNode;
  const ADirection : Boolean
) : TIterator;
var
  h : PBaseIterator;
begin
  h := TreeCreateIterator();
  TreeInitIterator(h,Self,ANode,ADirection);
  Result := TIterator.Create(h);
end;

class function TRBTree<T,KCOMP>.DefaultCreateNode(AContext: Pointer): PRBTreeNode;
begin
  New(Result);
end;

class procedure TRBTree<T,KCOMP>.DefaultFreeNode(ANode: PRBTreeNode; AContext: Pointer);
begin
  Dispose(ANode);
end;

constructor TRBTree<T,KCOMP>.Create(const AAllocator  : PRBTreeAllocator);
begin
  Root := nil;
  Allocator := AAllocator^;
  //Comparator := TRBTreeNodeComparator.Create();
end;

constructor TRBTree < T, KCOMP > .Create();
var
  a : TRBTreeAllocator;
begin
  a.CreateNode := TRBTreeNodeCreator(DefaultCreateNode);
  a.FreeNode := TRBTreeNodeDestructor(DefaultFreeNode);
  Create(@a);
end;

destructor TRBTree<T,KCOMP>.Destroy;
begin
  Clear();
  //Comparator.Free();
  inherited;
end;

procedure TRBTree<T,KCOMP>.Clear();
var
  it, save : PRBTreeNode;
begin
  it := Root;

  while (it <> nil) do begin
    if (it^.Links[False] <> nil) then begin
      // Right rotation
      save := it^.Links[False];
      it^.Links[False] := save^.Links[True];
      save^.Links[True] := it;
    end else begin
      save := it^.Links[True];
      Allocator.FreeNode(it,Self);
    end;
    it := save;
  end;
end;

function TRBTree<T,KCOMP>.FindNode(const AData: TRBTreeNodeData): PRBTreeNode;
var
  it : PRBTreeNode;
  cp : TRBTreeNodeComparator;
  dir : Boolean;
begin
  Result := nil;
  it := Root;
  if (it = nil) then
    exit;
  cp := Comparator;
  while (it <> nil) do begin
    if (cp.Compare(it^.Data,AData) = 0) then begin
      Result := it;
      Break;
    end;
    dir := (cp.Compare(it^.Data,AData) < 0);
    it := it^.Links[dir];
  end;
end;

function TRBTree<T,KCOMP>.Insert(const AData: TRBTreeNodeData): PRBTreeNode;
var
  head : TRBTreeNode;
  g, t : PRBTreeNode; // Grandparent & parent
  p, q : PRBTreeNode; // Iterator & parent
  dir, last, dir2 : Boolean;
  cp : TRBTreeNodeComparator;
begin
  if (Root = nil) then begin
    // Empty tree case
    Root := InitNode(Allocator.CreateNode(Self),AData);
    Result := Root;
  end else begin
    FillChar(head,SizeOf(head),0); // False tree root

    dir := False;
    last := False;

    // Set up helpers
    t := @head;
    g := nil;
    p := nil;
    t^.Links[True] := Root;
    q := t^.Links[True];

  // Search down the tree
    cp := Comparator;
    while True do begin
      if (q = nil) then begin
        // Insert new node at the bottom
        q := InitNode(Allocator.CreateNode(Self),AData);
        p^.Links[dir] := q;
      end else if IsRed(q^.Links[False]) and IsRed(q^.Links[True]) then begin
        // Color flip
        q^.Red := True;
        q^.Links[False]^.Red := False;
        q^.Links[True]^.Red := False;
      end;

      // Fix red violation
      if IsRed(q) and IsRed(p) then begin
        dir2 := (t^.Links[True] = g);
        if (q = p^.Links[last]) then
          t^.Links[dir2] := RotateSingle(g, not last)
        else
          t^.Links[dir2] := RotateDouble(g, not last );
      end;

      // Stop if found
      if (cp.Compare(q^.Data,AData) = 0) then begin
        Result := q;
        break;
      end;

      last := dir;
      dir := (cp.Compare(q^.Data,AData) < 0);

      // Update helpers
      if (g <> nil) then
        t := g;
      g := p;
      p := q;
      q := q^.Links[dir];
    end;

    // Update root
     Root := head.Links[True];
  end;

  // Make root black
  Root^.Red := False;
end;

function TRBTree<T,KCOMP>.Remove(const AData: TRBTreeNodeData): Boolean;
var
  head : TRBTreeNode;
  q, p, g, f, s : PRBTreeNode;
  dir, last, dir2 : Boolean;
  cp : TRBTreeNodeComparator;
begin
  Result := False;
  if (Root = nil) then
    exit;

  FillChar(head,SizeOf(head),0); // False tree root
  f := nil;
  dir := True;

  // Set up helpers
  q := @head;
  p := nil;
  g := nil;
  q^.Links[True] := Root;

  // Search and push a red down
  cp := Comparator;
  while (q^.Links[dir] <> nil) do begin
    last := dir;

    // Update helpers
    g := p;
    p := q;
    q := q^.Links[dir];
    dir := (cp.Compare(q^.Data,AData) < 0);

    // Save found node
    if (cp.Compare(q^.Data,AData) = 0) then
      f := q;

    // Push the red node down
    if not(IsRed(q)) and not(IsRed(q^.Links[dir])) then begin
      if IsRed(q^.Links[not dir]) then begin
        p^.Links[last] := RotateSingle(q,dir);
        p := p^.Links[last];
      end else if not IsRed(q^.Links[not dir]) then begin
        s := p^.Links[not last];

        if (s <> nil) then begin
          if not(IsRed(s^.Links[not last])) and not(IsRed(s^.Links[last])) then begin
            // Color flip
            p^.Red := False;
            s^.Red := True;
            q^.Red := True;
          end else begin
            dir2 := (g^.Links[True] = p);

            if IsRed(s^.Links[last]) then
              g^.Links[dir2] := RotateDouble(p,last)
            else if IsRed(s^.Links[not last]) then
              g^.Links[dir2] := RotateSingle(p,last);

            // Ensure correct coloring
            g^.Links[dir2]^.Red := True;
            q^.Red := g^.Links[dir2]^.Red;
            g^.Links[dir2]^.Links[False]^.Red := False;
            g^.Links[dir2]^.Links[True]^.Red := False;
          end;
        end;
      end;
    end;
  end;

  // Replace and remove if found
  if (f <> nil) then begin
    f^.Data := q^.Data;
    p^.Links[(p^.Links[True] = q)] :=
      q^.Links[(q^.Links[False] = nil)];
    Allocator.FreeNode(q,Self);
    Result := True;
  end;

  // Update root and make it black
  Root := head.Links[True];
  if (Root <> nil) then
    Root^.Red := False;
end;

function TRBTree<T,KCOMP>.CreateForwardIterator(const ANode : PRBTreeNode) : TIterator;
begin
  Result := CreateIterator(ANode,False);
end;

function TRBTree<T,KCOMP>.CreateForwardIterator() : TIterator;
begin
  Result := CreateForwardIterator(Root);
end;

function TRBTree<T,KCOMP>.CreateBackwardIterator(const ANode : PRBTreeNode) : TIterator;
begin
  Result := CreateIterator(ANode,True);
end;

function TRBTree<T,KCOMP>.CreateBackwardIterator() : TIterator;
begin
  Result := CreateBackwardIterator(Root);
end;

{$ifdef RB_DEBUG}
function TRBTree<T,KCOMP>.SelfAssert(ARoot : PRBTreeNode; var AErrorMessage: string): Boolean;
var
  lh, rh : Boolean;
  ln, rn : PRBTreeNode;
  e : string;
begin
  AErrorMessage := '';
  if (ARoot = nil) then begin
    Result := True;
    exit;
  end;

  e := '';
  ln := ARoot^.Links[False];
  rn := ARoot^.Links[True];

  // Consecutive red links
  if IsRed(ARoot) then begin
    if IsRed(ln) or IsRed(rn) then begin
      AErrorMessage := 'Red violation';
      Result := False;
      exit;
    end;
  end;

  lh := SelfAssert(ln,e);
  AErrorMessage := AErrorMessage + ' ' + e;

  rh := SelfAssert(rn,e);
  AErrorMessage := AErrorMessage + ' ' + e;

  // Invalid binary search tree
  if ( ( (ln <> nil) and (Comparator.Compare(ln^.Data,ARoot^.Data) >= 0) ) or
     ( (rn <> nil) and (Comparator.Compare(rn^.Data,ARoot^.Data) <= 0) ) )
  then begin
    AErrorMessage := AErrorMessage + ' ' + 'Binary tree violation';
    Result := False;
    Exit;
  end;

  // Black height mismatch
  if ( lh and rh and (lh <> rh) ) then begin
    AErrorMessage := AErrorMessage + ' ' + 'Black violation';
    Result := False;
    Exit;
  end;

  Result := lh and rh;
end;

function TRBTree<T,KCOMP>.SelfAssert(var AErrorMessage: string): Boolean;
begin
  Result := Self.SelfAssert(Root, AErrorMessage);
end;

{$endif RB_DEBUG}

constructor TRBTree<T,KCOMP>.TIterator.Create(AHandle : PBaseIterator);
begin
  inherited Create();
  FHandle := AHandle;
  FResetState := True;
end;

destructor TRBTree<T,KCOMP>.TIterator.Destroy();
begin
  TreeFreeIterator(FHandle);
  inherited Destroy;
end;

function TRBTree<T,KCOMP>.TIterator.MoveNext : Boolean;
begin
  if FResetState then begin
    FResetState := False;
    Result := (FHandle^.Current <> nil);
    exit;
  end;
  Result := (TreeIteratorMoveNext(FHandle) <> nil);
end;

function TRBTree<T,KCOMP>.TIterator.MovePrevious : Boolean;
begin
  if FResetState then begin
    FResetState := False;
    Result := (FHandle^.Current <> nil);
    exit;
  end;
  Result := (TreeIteratorMovePrevious(FHandle) <> nil);
end;

function TRBTree<T,KCOMP>.TIterator.GetCurrent : TRBTreeNodeData;
begin
  Result := GetCurrentNode()^.Data;
end;

function TRBTree<T,KCOMP>.TIterator.GetCurrentNode : PRBTreeNode;
begin
  Result := FHandle^.Current;
end;

procedure TRBTree<T,KCOMP>.TIterator.Reset();
begin
  FResetState := True;
  TreeInitIterator(FHandle,FHandle^.Tree,FHandle^.StartingNode,FHandle^.StartingDir)
end;

{ TOrdinalComparator<T> }

class function TOrdinalComparator<T>.Compare(const A, B: TOrdinalType): Integer;
begin
  if (A = B) then
    exit(0);
  if (A > B) then
    exit(1);
  exit(-1);
end;

end.