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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056 | -- --
-- package Copyright (c) Dmitry A. Kazakov --
-- Generic_Indefinite_B_Tree Luebeck --
-- Implementation Spring, 2014 --
-- --
-- Last revision : 22:45 07 Apr 2016 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- 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. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
with Ada.Exceptions; use Ada.Exceptions;
with Ada.IO_Exceptions; use Ada.IO_Exceptions;
with Ada.Tags; use Ada.Tags;
with Ada.Unchecked_Deallocation;
--with Ada.Text_IO, Strings_Edit.Integers, System.Storage_Elements;
package body Generic_Indefinite_B_Tree is
Left_Half : constant Positive := (Width + 1) / 2 - 1;
Right_Half : constant Positive := Width - Left_Half;
-- function Image (Node : Node_Ptr) return String is
-- use System.Storage_Elements;
-- begin
-- if Node = null then
-- return "0";
-- else
-- declare
-- Text : String :=
-- Integer_Address'Image (To_Integer (Node.all'Address));
-- begin
-- return Text (Text'First + 1..Text'Last);
-- end;
-- end if;
-- end Image;
--
-- procedure Dump (Node : Node_Ptr; Prefix : String := "") is
-- use Ada.Text_IO;
-- use Strings_Edit.Integers;
-- use System.Storage_Elements;
-- begin
-- Put (Prefix);
-- Put ("at ");
-- Put (Image (Node));
-- if Node.Parent.Node /= null then
-- Put (" [");
-- Put (Image (Node.Parent.Node));
-- Put ("|");
-- Put (Image (Node.Parent.Index));
-- Put ("]");
-- end if;
-- Put (":");
-- for Index in 1..Node.Length loop
-- if Node.Children (Index) = null then
-- Put (" .");
-- else
-- Put (' ' & Image (Node.Children (Index)));
-- end if;
-- Put (" /");
-- Put (Ada.Strings.Unbounded.To_String (Image (Node.Pairs (Index).Key.all)));
-- Put ("\");
-- end loop;
-- if Node.Children (Node.Length + 1) = null then
-- Put (" .");
-- else
-- Put (' ' & Image (Node.Children (Node.Length + 1)));
-- end if;
-- New_Line;
-- for Index in 1..Node.Length + 1 loop
-- if Node.Children (Index) /= null then
-- Dump
-- ( Node.Children (Index),
-- " " & Prefix & Image (Index) & "."
-- );
-- end if;
-- end loop;
-- end Dump;
procedure Copy
( To_Node : Node_Ptr;
To_First : Positive;
To_Last : Natural;
From_Node : in out Node_Type;
From_First : Positive;
Tail : Natural := 1
) is
pragma Inline (Copy);
From_Last : constant Integer := From_First + To_Last - To_First;
begin
To_Node.Pairs (To_First..To_Last) :=
From_Node.Pairs (From_First..From_Last);
To_Node.Children (To_First..To_Last + Tail) :=
From_Node.Children (From_First..From_Last + Tail);
for Index in To_First..To_Last + Tail loop
if To_Node.Children (Index) /= null then
To_Node.Children (Index).Parent := (To_Node, Index);
end if;
end loop;
end Copy;
procedure Move
( Node : Node_Ptr;
To_First : Positive;
To_Last : Natural;
From_First : Positive;
Tail : Natural := 1
) is
pragma Inline (Move);
From_Last : constant Integer := From_First + To_Last - To_First;
begin
Node.Pairs (To_First..To_Last) :=
Node.Pairs (From_First..From_Last);
Node.Children (To_First..To_Last + Tail) :=
Node.Children (From_First..From_Last + Tail);
for Index in To_First..To_Last + Tail loop
if Node.Children (Index) /= null then
Node.Children (Index).Parent := (Node, Index);
end if;
end loop;
end Move;
procedure Free is
new Ada.Unchecked_Deallocation (Key_Type, Key_Ptr);
procedure Free is
new Ada.Unchecked_Deallocation (Object_Type, Object_Ptr);
procedure Free is
new Ada.Unchecked_Deallocation (Node_Type, Node_Ptr);
function Is_Empty (Container : B_Tree) return Boolean is
begin
return Container.Root = null or else Container.Root.Length = 0;
end Is_Empty;
function Is_In (Container : B_Tree; Key : Key_Type)
return Boolean is
Node : Node_Ptr;
Index : Integer;
begin
Search (Container.Root, Key, Node, Index);
return Node /= null and then Index > 0;
end Is_In;
procedure Add
( Container : in out B_Tree;
Key : Key_Type;
Value : Object_Type
) is
begin
if Container.Root = null then
Container.Root := new Node_Type;
declare
Root : Node_Type renames Container.Root.all;
Pair : Pair_Type :=
( Key => new Key_Type'(Key),
Value => new Object_Type'(Value)
);
begin
Root.Length := 1;
Root.Pairs (1) := Pair;
exception
when others =>
Free (Pair.Key);
Free (Pair.Value);
raise;
end;
else
declare
Node : Node_Ptr;
Index : Integer;
begin
Search (Container.Root, Key, Node, Index);
if Index < 0 then
declare
Pair : Pair_Type :=
( Key => new Key_Type'(Key),
Value => new Object_Type'(Value)
);
begin
Insert
( Container => Container,
Parent => (Node, -Index),
Child => null,
Pair => Pair
);
exception
when others =>
Free (Pair.Key);
Free (Pair.Value);
raise;
end;
else
Raise_Exception
( Constraint_Error'Identity,
"The key is already in use"
);
end if;
end;
end if;
end Add;
procedure Erase (Root : in out Node_Ptr) is
begin
if Root /= null then
declare
Node : Node_Type := Root.all;
begin
for Index in 1..Node.Length loop
Free (Node.Pairs (Index).Key);
Free (Node.Pairs (Index).Value);
end loop;
for Index in 1..Node.Length + 1 loop
Erase (Node.Children (Index));
end loop;
end;
Free (Root);
end if;
end Erase;
procedure Erase (Container : in out B_Tree) is
begin
Erase (Container.Root);
end Erase;
procedure Finalize (Container : in out B_Tree) is
begin
Erase (Container);
end Finalize;
function Find
( Pairs : Pair_Array;
Size : Natural;
Key : Key_Type
) return Integer is
pragma Inline (Find);
From : Natural := 0;
To : Natural := Size + 1;
This : Natural;
begin
if Size = 0 then
return -1;
end if;
loop
This := (From + To) / 2;
if Key = Pairs (This).Key.all then
return This;
elsif Key < Pairs (This).Key.all then
if This - From <= 1 then
return -This;
end if;
To := This;
else
if To - This <= 1 then
return - This - 1;
end if;
From := This;
end if;
end loop;
end Find;
function Find (Container : B_Tree; Key : Key_Type) return Item_Ptr is
Node : Node_Ptr;
Index : Integer;
begin
Search (Container.Root, Key, Node, Index);
if Index <= 0 then
return No_Item;
else
return (Node, Index);
end if;
end Find;
function Get
( Container : B_Tree;
Key : Key_Type
) return Object_Type is
Item : constant Item_Ptr := Find (Container, Key);
begin
if Item.Node = null then
Raise_Exception
( Constraint_Error'Identity,
"No item"
);
end if;
return Item.Node.Pairs (Item.Index).Value.all;
end Get;
function Get_Bucket_Address
( Item : Item_Ptr
) return System.Address is
begin
if Item.Node = null then
return System.Null_Address;
else
return Item.Node.all'Address;
end if;
end Get_Bucket_Address;
function Get_Bucket_Size (Item : Item_Ptr) return Natural is
begin
if Item.Node = null then
return 0;
else
return Item.Node.Length;
end if;
end Get_Bucket_Size;
function Get_First (Root : Node_Ptr) return Item_Ptr is
This : Node_Ptr := Root;
Next : Node_Ptr;
begin
if This = null or else This.Length = 0 then
return No_Item;
else
loop
Next := This.Children (1);
if Next = null then
return (This, 1);
end if;
This := Next;
end loop;
end if;
end Get_First;
function Get_First (Container : B_Tree) return Item_Ptr is
begin
return Get_First (Container.Root);
end Get_First;
function Get_Index (Item : Item_Ptr) return Positive is
begin
if Item.Node = null then
Raise_Exception
( Constraint_Error'Identity,
"No item"
);
end if;
return Item.Index;
end Get_Index;
function Get_Key (Item : Item_Ptr) return Key_Type is
begin
if Item.Node = null then
Raise_Exception
( Constraint_Error'Identity,
"No item"
);
end if;
declare
Node : Node_Type renames Item.Node.all;
begin
if Item.Index > Node.Length then
Raise_Exception
( Constraint_Error'Identity,
"Key with illegal item index"
);
end if;
return Node.Pairs (Item.Index).Key.all;
end;
end Get_Key;
function Get_Last (Root : Node_Ptr) return Item_Ptr is
This : Node_Ptr := Root;
Next : Node_Ptr;
begin
if This = null or else This.Length < 0 then
return No_Item;
else
loop
Next := This.Children (This.Length + 1);
if Next = null then
if This.Length = 0 then
return No_Item;
else
return (This, This.Length);
end if;
end if;
This := Next;
end loop;
end if;
end Get_Last;
function Get_Last (Container : B_Tree) return Item_Ptr is
begin
return Get_Last (Container.Root);
end Get_Last;
function Get_Next (Item : Item_Ptr) return Item_Ptr is
begin
if Item.Node = null then
return No_Item;
end if;
declare
Node : Node_Type renames Item.Node.all;
begin
if Item.Index > Node.Length then
Raise_Exception
( Constraint_Error'Identity,
"Next with illegal item index"
);
end if;
declare
First : Item_Ptr :=
Get_First (Node.Children (Item.Index + 1));
begin
if First.Node = null or else First.Node = Item.Node then
if Item.Index = Node.Length then
First := Node.Parent;
while First.Node /= null loop
if First.Index <= First.Node.Length then
return First;
end if;
First := First.Node.Parent;
end loop;
return No_Item;
else
return (Item.Node, Item.Index + 1);
end if;
else
return First;
end if;
end;
end;
end Get_Next;
function Get_Previous (Item : Item_Ptr) return Item_Ptr is
begin
if Item.Node = null then
return No_Item;
end if;
declare
Node : Node_Type renames Item.Node.all;
begin
if Item.Index > Node.Length then
Raise_Exception
( Constraint_Error'Identity,
"Previous to illegal item index"
);
end if;
declare
Last : Item_Ptr := Get_Last (Node.Children (Item.Index));
begin
if Last.Node = null or else Last.Node = Item.Node then
if Item.Index = 1 then
Last := Node.Parent;
while Last.Node /= null loop
if Last.Index > 1 then
return (Last.Node, Last.Index - 1);
end if;
Last := Last.Node.Parent;
end loop;
return No_Item;
else
return (Item.Node, Item.Index - 1);
end if;
else
return Last;
end if;
end;
end;
end Get_Previous;
function Get_Root (Item : Item_Ptr) return Item_Ptr is
begin
if Item.Node = null then
return No_Item;
else
declare
Node : Node_Ptr := Item.Node;
begin
while Node.Parent.Node /= null loop
Node := Node.Parent.Node;
end loop;
return (Node, 1);
end;
end if;
end Get_Root;
function Get_Value (Item : Item_Ptr) return Object_Type is
begin
if Item.Node = null then
Raise_Exception
( Constraint_Error'Identity,
"No item"
);
end if;
declare
Node : Node_Type renames Item.Node.all;
begin
if Item.Index > Node.Length then
Raise_Exception
( Constraint_Error'Identity,
"Value with illegal item index"
);
end if;
return Node.Pairs (Item.Index).Value.all;
end;
end Get_Value;
function Inf (Container : B_Tree; Key : Key_Type) return Item_Ptr is
Node : Node_Ptr;
Index : Integer;
begin
Search (Container.Root, Key, Node, Index);
if Index = 0 then
return No_Item;
else
return (Node, abs Index);
end if;
end Inf;
procedure Insert
( Container : in out B_Tree;
Parent : Node_Ptr;
Pair : Pair_Type;
Child : Node_Ptr
) is
begin
if Parent = null then
Insert (Container, No_Item, Pair, Child);
else
declare
Index : constant Integer :=
Find (Parent.Pairs, Parent.Length, Pair.Key.all);
begin
if Index > 0 then
Raise_Exception
( Data_Error'Identity,
"Re-inserting a key"
);
end if;
Insert (Container, (Parent, -Index), Pair, Child);
end;
end if;
end Insert;
procedure Insert
( Container : in out B_Tree;
Parent : Item_Ptr;
Pair : Pair_Type;
Child : Node_Ptr
) is
procedure New_Root
( Pair : Pair_Type;
Left : Node_Ptr;
Right : Node_Ptr
) is
New_Node : constant Node_Ptr := new Node_Type;
Root : Node_Type renames New_Node.all;
begin
Root.Length := 1;
Root.Pairs (1) := Pair;
Root.Children (1) := Left;
Root.Children (2) := Right;
Container.Root := New_Node;
end New_Root;
begin
if Parent.Node = null then -- Creating new root node
Raise_Exception
( Data_Error'Identity,
"Inserting at null node"
);
end if;
declare
Right : Node_Type renames Parent.Node.all;
Index : Integer := Parent.Index;
begin
if Right.Length < Width then -- Have place in the bucket
Move (Parent.Node, Index + 1, Right.Length + 1, Index);
Right.Pairs (Index) := Pair;
Right.Children (Index) := Child;
Right.Length := Right.Length + 1;
if Child /= null then
Child.Parent := (Parent.Node, Index);
end if;
elsif Index = Width + 1 and then Underfilled_Left (Right) then
declare
Split : constant Integer := Right.Parent.Index - 1;
This : Node_Type renames Right.Parent.Node.all;
Left : Node_Type renames This.Children (Split).all;
begin
Left.Pairs (Left.Length + 1) := This.Pairs (Split);
Left.Length := Left.Length + 1;
Copy
( This.Children (Split),
Left.Length + 1,
Left.Length,
Right,
1
);
This.Pairs (Split) := Right.Pairs (1);
Move (Parent.Node, 1, Width - 1, 2, 0);
Right.Pairs (Width) := Pair;
Right.Children (Width) := Child;
if Child /= null then
Child.Parent := (Parent.Node, Width);
end if;
end;
elsif Index = 1 and then Underfilled_Right (Right) then
declare
Split : constant Integer := Right.Parent.Index;
Other : constant Node_Ptr :=
Right.Parent.Node.Children (Split + 1);
begin
Move (Other, 2, Other.Length + 1, 1);
Other.Length := Other.Length + 1;
Copy (Other, 1, 0, Right, Width + 1);
Other.Pairs (1) := Right.Parent.Node.Pairs (Split);
Right.Parent.Node.Pairs (Split) :=
Parent.Node.Pairs (Width);
Move (Parent.Node, 2, Width, 1);
Right.Pairs (1) := Pair;
Right.Children (1) := Child;
if Child /= null then
Child.Parent := (Parent.Node, 1);
end if;
end;
else
declare
New_Node : constant Node_Ptr := new Node_Type;
Left : Node_Type renames New_Node.all;
begin
Left.Length := Left_Half;
Right.Length := Right_Half;
if Index = Left_Half + 1 then
if Right.Parent.Node = null then
New_Root (Pair, New_Node, Parent.Node);
Left.Parent := (Container.Root, 1);
Right.Parent := (Container.Root, 2);
else
Insert
( Container,
Right.Parent.Node,
Pair,
New_Node
);
end if;
Copy (New_Node, 1, Left_Half, Right, 1);
Left.Children (Left_Half + 1) := Child;
Move (Parent.Node, 1, Right_Half, Left_Half + 1);
if Child /= null then
Child.Parent := (New_Node, Left_Half + 1);
end if;
elsif Index <= Left_Half then
if Right.Parent.Node = null then
New_Root
( Right.Pairs (Left_Half),
New_Node,
Parent.Node
);
Left.Parent := (Container.Root, 1);
Right.Parent := (Container.Root, 2);
else
Insert
( Container,
Right.Parent.Node,
Right.Pairs (Left_Half),
New_Node
);
end if;
Copy (New_Node, 1, Index - 1, Right, 1, 0);
Copy
( New_Node,
Index + 1,
Left_Half + 1,
Right,
Index
);
Left.Pairs (Index) := Pair;
Left.Children (Index) := Child;
Move (Parent.Node, 1, Right_Half, Left_Half + 1);
if Child /= null then
Child.Parent := (New_Node, Index);
end if;
else
if Right.Parent.Node = null then
New_Root
( Right.Pairs (Left_Half + 1),
New_Node,
Parent.Node
);
Left.Parent := (Container.Root, 1);
Right.Parent := (Container.Root, 2);
else
Insert
( Container,
Right.Parent.Node,
Right.Pairs (Left_Half + 1),
New_Node
);
end if;
Copy (New_Node, 1, Left_Half, Right, 1);
Index := Index - Left_Half - 1;
Move
( Parent.Node,
1,
Index - 1,
Left_Half + 2,
0
);
Move
( Parent.Node,
Index + 1,
Right_Half,
Index + Left_Half + 1
);
Right.Pairs (Index) := Pair;
Right.Children (Index) := Child;
if Child /= null then
Child.Parent := (Parent.Node, Index);
end if;
end if;
end;
end if;
end;
end Insert;
procedure Remove (Item : in out Item_Ptr) is
begin
if Item.Node = null then
return;
elsif Item.Index > Item.Node.Length then
Raise_Exception
( Constraint_Error'Identity,
"Removing an item with illegal index"
);
end if;
declare
Node : Node_Type renames Item.Node.all;
Index : constant Integer := Item.Index;
begin
if Node.Children (Index) = null then
if Node.Children (Index + 1) = null then
if Node.Length > 1 then
Move (Item.Node, Index, Node.Length - 1, Index + 1);
Node.Length := Node.Length - 1;
elsif Node.Parent.Node = null then -- No parent
Node.Length := 0;
else -- Remove at the parent
Node.Parent.Node.Children (Node.Parent.Index) := null;
Free (Item.Node);
end if;
else
if Node.Length > 1 then
Move (Item.Node, Index, Node.Length - 1, Index + 1);
Node.Length := Node.Length - 1;
else
declare
Right : Node_Ptr := Node.Children (2);
begin
if Node.Parent.Node = null then -- No parent
Node.Length := Right.Length;
Copy (Item.Node, 1, Node.Length, Right.all, 1);
Free (Right);
else
Right.Parent := Node.Parent;
Node.Parent.Node.Children (Node.Parent.Index) :=
Right;
Free (Item.Node);
end if;
end;
end if;
end if;
elsif Node.Children (Index + 1) = null then
if Node.Length > 1 then
declare
Left : constant Node_Ptr := Node.Children (Index);
begin
Move (Item.Node, Index, Node.Length - 1, Index + 1);
Node.Children (Index) := Left;
Node.Length := Node.Length - 1;
end;
else
declare
Left : Node_Ptr := Node.Children (1);
begin
if Node.Parent.Node = null then -- No parent
Node.Length := Left.Length;
Copy (Item.Node, 1, Node.Length, Left.all, 1);
Free (Left);
else
Left.Parent := Node.Parent;
Node.Parent.Node.Children (Node.Parent.Index) :=
Left;
Free (Item.Node);
end if;
end;
end if;
elsif ( Node.Children (Index).Length
> Node.Children (Index + 1).Length
) then
declare
Left : Node_Ptr := Node.Children (Index);
begin
while Left.Children (Left.Length + 1) /= null loop
Left := Left.Children (Left.Length + 1);
end loop;
Node.Pairs (Index) := Left.Pairs (Left.Length);
if Left.Length = 1 then
Left.Parent.Node.Children (Left.Parent.Index) := null;
Free (Left);
else
Left.Length := Left.Length - 1;
end if;
end;
else
declare
Right : Node_Ptr := Node.Children (Index + 1);
begin
while Right.Children (1) /= null loop
Right := Right.Children (1);
end loop;
Node.Pairs (Index) := Right.Pairs (1);
if Right.Length = 1 then
Right.Parent.Node.Children (Right.Parent.Index) :=
null;
Free (Right);
else
Move (Right, 1, Right.Length - 1, 2);
Right.Length := Right.Length - 1;
end if;
end;
end if;
end;
Item.Node := null;
Item.Index := 1;
end Remove;
procedure Remove (Container : in out B_Tree; Key : Key_Type) is
Item : Item_Ptr := Find (Container, Key);
begin
Remove (Item);
end Remove;
procedure Replace (Item : Item_Ptr; Value : Object_Type) is
begin
if Item.Node = null then
Raise_Exception
( Constraint_Error'Identity,
"Replacing an non-item"
);
else
declare
Node : Node_Type renames Item.Node.all;
begin
if Item.Index <= Node.Length then
declare
Ptr : Object_Ptr renames
Node.Pairs (Item.Index).Value;
Old : Object_Ptr := Ptr;
begin
Ptr := new Object_Type'(Value);
Free (Old);
end;
else
Raise_Exception
( Constraint_Error'Identity,
"Replacing an item with wrong index"
);
end if;
end;
end if;
end Replace;
procedure Replace
( Container : in out B_Tree;
Key : Key_Type;
Value : Object_Type
) is
begin
if Container.Root = null then
declare
Pair : Pair_Type :=
( Key => new Key_Type'(Key),
Value => new Object_Type'(Value)
);
begin
Insert (Container, No_Item, Pair, null);
exception
when others =>
Free (Pair.Key);
Free (Pair.Value);
raise;
end;
else
declare
Node : Node_Ptr;
Index : Integer;
begin
Search (Container.Root, Key, Node, Index);
if Index > 0 then
declare
Ptr : Object_Ptr renames Node.Pairs (Index).Value;
Old : Object_Ptr := Ptr;
begin
Ptr := new Object_Type'(Value);
Free (Old);
end;
else
declare
Pair : Pair_Type :=
( Key => new Key_Type'(Key),
Value => new Object_Type'(Value)
);
begin
Insert (Container, (Node, -Index), Pair, null);
exception
when others =>
Free (Pair.Key);
Free (Pair.Value);
raise;
end;
end if;
end;
end if;
end Replace;
procedure Search
( Root : Node_Ptr;
Key : Key_Type;
Node : out Node_Ptr;
Index : out Integer
) is
begin
Node := Root;
if Node = null then
Index := 0;
else
loop
Index := Find (Node.Pairs, Node.Length, Key);
exit when Index > 0;
declare
Next : constant Node_Ptr := Node.Children (-Index);
begin
exit when Next = null;
Node := Next;
end;
end loop;
end if;
end Search;
function Sup (Container : B_Tree; Key : Key_Type) return Item_Ptr is
Node : Node_Ptr;
Index : Integer;
begin
Search (Container.Root, Key, Node, Index);
if Index = 0 then
return No_Item;
elsif Index > 0 then
return (Node, Index);
else
return Get_Next ((Node, -Index));
end if;
end Sup;
function Underfilled_Left (Right : Node_Type) return Boolean is
begin
if Right.Parent.Node = null then
return False;
else
declare
Index : Integer := Right.Parent.Index;
begin
if Index < 2 then
return False;
else
Index := Index - 1;
declare
Parent : Node_Type renames Right.Parent.Node.all;
begin
if Parent.Children (Index) = null then
return False;
else
return Parent.Children (Index).Length < Width;
end if;
end;
end if;
end;
end if;
end Underfilled_Left;
function Underfilled_Right (Left : Node_Type) return Boolean is
begin
if Left.Parent.Node = null then
return False;
else
declare
Parent : Node_Type renames Left.Parent.Node.all;
Index : Integer := Left.Parent.Index;
begin
if Index > Parent.Length then
return False;
else
Index := Index + 1;
if Parent.Children (Index) = null then
return False;
else
return Parent.Children (Index).Length < Width;
end if;
end if;
end;
end if;
end Underfilled_Right;
function "=" (Left, Right : Pair_Type) return Boolean is
begin
return
( Left.Key.all = Right.Key.all
and then
Left.Value.all = Right.Value.all
);
end "=";
function "=" (Left, Right : B_Tree) return Boolean is
Item_1 : Item_Ptr := Get_First (Left);
Item_2 : Item_Ptr := Get_First (Right);
begin
while Item_1.Node /= null loop
if ( Item_2.Node = null
or else
( Item_1.Node.Pairs (Item_1.Index)
/= Item_2.Node.Pairs (Item_2.Index)
) )
then
return False;
end if;
Item_1 := Get_Next (Item_1);
Item_2 := Get_Next (Item_2);
end loop;
return Item_2.Node = null;
end "=";
end Generic_Indefinite_B_Tree;
|