-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckTextBTree.c
2796 lines (2548 loc) · 77.5 KB
/
ckTextBTree.c
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
/*
* ckTextBTree.c --
*
* This file contains code that manages the B-tree representation
* of text for Ck's text widget and implements character and
* toggle segment types.
*
* Copyright (c) 1992-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 Christian Werner
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
#include "ckText.h"
/*
* The data structure below keeps summary information about one tag as part
* of the tag information in a node.
*/
typedef struct Summary {
CkTextTag *tagPtr; /* Handle for tag. */
int toggleCount; /* Number of transitions into or
* out of this tag that occur in
* the subtree rooted at this node. */
struct Summary *nextPtr; /* Next in list of all tags for same
* node, or NULL if at end of list. */
} Summary;
/*
* The data structure below defines a node in the B-tree.
*/
typedef struct Node {
struct Node *parentPtr; /* Pointer to parent node, or NULL if
* this is the root. */
struct Node *nextPtr; /* Next in list of siblings with the
* same parent node, or NULL for end
* of list. */
Summary *summaryPtr; /* First in malloc-ed list of info
* about tags in this subtree (NULL if
* no tag info in the subtree). */
int level; /* Level of this node in the B-tree.
* 0 refers to the bottom of the tree
* (children are lines, not nodes). */
union { /* First in linked list of children. */
struct Node *nodePtr; /* Used if level > 0. */
CkTextLine *linePtr; /* Used if level == 0. */
} children;
int numChildren; /* Number of children of this node. */
int numLines; /* Total number of lines (leaves) in
* the subtree rooted here. */
} Node;
/*
* Upper and lower bounds on how many children a node may have:
* rebalance when either of these limits is exceeded. MAX_CHILDREN
* should be twice MIN_CHILDREN and MIN_CHILDREN must be >= 2.
*/
#define MAX_CHILDREN 12
#define MIN_CHILDREN 6
/*
* The data structure below defines an entire B-tree.
*/
typedef struct BTree {
Node *rootPtr; /* Pointer to root of B-tree. */
} BTree;
/*
* The structure below is used to pass information between
* CkBTreeGetTags and IncCount:
*/
typedef struct TagInfo {
int numTags; /* Number of tags for which there
* is currently information in
* tags and counts. */
int arraySize; /* Number of entries allocated for
* tags and counts. */
CkTextTag **tagPtrs; /* Array of tags seen so far.
* Malloc-ed. */
int *counts; /* Toggle count (so far) for each
* entry in tags. Malloc-ed. */
} TagInfo;
/*
* Variable that indicates whether to enable consistency checks for
* debugging.
*/
int ckBTreeDebug = 0;
/*
* Macros that determine how much space to allocate for new segments:
*/
#define CSEG_SIZE(chars) ((unsigned) (Ck_Offset(CkTextSegment, body) \
+ 1 + (chars)))
#define TSEG_SIZE ((unsigned) (Ck_Offset(CkTextSegment, body) \
+ sizeof(CkTextToggle)))
/*
* Forward declarations for procedures defined in this file:
*/
static void ChangeNodeToggleCount _ANSI_ARGS_((Node *nodePtr,
CkTextTag *tagPtr, int delta));
static void CharCheckProc _ANSI_ARGS_((CkTextSegment *segPtr,
CkTextLine *linePtr));
static int CharDeleteProc _ANSI_ARGS_((CkTextSegment *segPtr,
CkTextLine *linePtr, int treeGone));
static CkTextSegment * CharCleanupProc _ANSI_ARGS_((CkTextSegment *segPtr,
CkTextLine *linePtr));
static CkTextSegment * CharSplitProc _ANSI_ARGS_((CkTextSegment *segPtr,
int index));
static void CheckNodeConsistency _ANSI_ARGS_((Node *nodePtr));
static void CleanupLine _ANSI_ARGS_((CkTextLine *linePtr));
static void DeleteSummaries _ANSI_ARGS_((Summary *tagPtr));
static void DestroyNode _ANSI_ARGS_((Node *nodePtr));
static void IncCount _ANSI_ARGS_((CkTextTag *tagPtr, int inc,
TagInfo *tagInfoPtr));
static void Rebalance _ANSI_ARGS_((BTree *treePtr, Node *nodePtr));
static void RecomputeNodeCounts _ANSI_ARGS_((Node *nodePtr));
static CkTextSegment * SplitSeg _ANSI_ARGS_((CkTextIndex *indexPtr));
static void ToggleCheckProc _ANSI_ARGS_((CkTextSegment *segPtr,
CkTextLine *linePtr));
static CkTextSegment * ToggleCleanupProc _ANSI_ARGS_((CkTextSegment *segPtr,
CkTextLine *linePtr));
static int ToggleDeleteProc _ANSI_ARGS_((CkTextSegment *segPtr,
CkTextLine *linePtr, int treeGone));
static void ToggleLineChangeProc _ANSI_ARGS_((CkTextSegment *segPtr,
CkTextLine *linePtr));
/*
* Type record for character segments:
*/
Ck_SegType ckTextCharType = {
"character", /* name */
0, /* leftGravity */
CharSplitProc, /* splitProc */
CharDeleteProc, /* deleteProc */
CharCleanupProc, /* cleanupProc */
(Ck_SegLineChangeProc *) NULL, /* lineChangeProc */
CkTextCharLayoutProc, /* layoutProc */
CharCheckProc /* checkProc */
};
/*
* Type record for segments marking the beginning of a tagged
* range:
*/
Ck_SegType ckTextToggleOnType = {
"toggleOn", /* name */
0, /* leftGravity */
(Ck_SegSplitProc *) NULL, /* splitProc */
ToggleDeleteProc, /* deleteProc */
ToggleCleanupProc, /* cleanupProc */
ToggleLineChangeProc, /* lineChangeProc */
(Ck_SegLayoutProc *) NULL, /* layoutProc */
ToggleCheckProc /* checkProc */
};
/*
* Type record for segments marking the end of a tagged
* range:
*/
Ck_SegType ckTextToggleOffType = {
"toggleOff", /* name */
1, /* leftGravity */
(Ck_SegSplitProc *) NULL, /* splitProc */
ToggleDeleteProc, /* deleteProc */
ToggleCleanupProc, /* cleanupProc */
ToggleLineChangeProc, /* lineChangeProc */
(Ck_SegLayoutProc *) NULL, /* layoutProc */
ToggleCheckProc /* checkProc */
};
/*
*----------------------------------------------------------------------
*
* CkBTreeCreate --
*
* This procedure is called to create a new text B-tree.
*
* Results:
* The return value is a pointer to a new B-tree containing
* one line with nothing but a newline character.
*
* Side effects:
* Memory is allocated and initialized.
*
*----------------------------------------------------------------------
*/
CkTextBTree
CkBTreeCreate()
{
register BTree *treePtr;
register Node *rootPtr;
register CkTextLine *linePtr, *linePtr2;
register CkTextSegment *segPtr;
/*
* The tree will initially have two empty lines. The second line
* isn't actually part of the tree's contents, but its presence
* makes several operations easier. The tree will have one node,
* which is also the root of the tree.
*/
rootPtr = (Node *) ckalloc(sizeof(Node));
linePtr = (CkTextLine *) ckalloc(sizeof(CkTextLine));
linePtr2 = (CkTextLine *) ckalloc(sizeof(CkTextLine));
rootPtr->parentPtr = NULL;
rootPtr->nextPtr = NULL;
rootPtr->summaryPtr = NULL;
rootPtr->level = 0;
rootPtr->children.linePtr = linePtr;
rootPtr->numChildren = 2;
rootPtr->numLines = 2;
linePtr->parentPtr = rootPtr;
linePtr->nextPtr = linePtr2;
segPtr = (CkTextSegment *) ckalloc(CSEG_SIZE(1));
linePtr->segPtr = segPtr;
segPtr->typePtr = &ckTextCharType;
segPtr->nextPtr = NULL;
segPtr->size = 1;
segPtr->body.chars[0] = '\n';
segPtr->body.chars[1] = 0;
linePtr2->parentPtr = rootPtr;
linePtr2->nextPtr = NULL;
segPtr = (CkTextSegment *) ckalloc(CSEG_SIZE(1));
linePtr2->segPtr = segPtr;
segPtr->typePtr = &ckTextCharType;
segPtr->nextPtr = NULL;
segPtr->size = 1;
segPtr->body.chars[0] = '\n';
segPtr->body.chars[1] = 0;
treePtr = (BTree *) ckalloc(sizeof(BTree));
treePtr->rootPtr = rootPtr;
return (CkTextBTree) treePtr;
}
/*
*----------------------------------------------------------------------
*
* CkBTreeDestroy --
*
* Delete a B-tree, recycling all of the storage it contains.
*
* Results:
* The tree given by treePtr is deleted. TreePtr should never
* again be used.
*
* Side effects:
* Memory is freed.
*
*----------------------------------------------------------------------
*/
void
CkBTreeDestroy(tree)
CkTextBTree tree; /* Pointer to tree to delete. */
{
BTree *treePtr = (BTree *) tree;
DestroyNode(treePtr->rootPtr);
ckfree((char *) treePtr);
}
/*
*----------------------------------------------------------------------
*
* DestroyNode --
*
* This is a recursive utility procedure used during the deletion
* of a B-tree.
*
* Results:
* None.
*
* Side effects:
* All the storage for nodePtr and its descendants is freed.
*
*----------------------------------------------------------------------
*/
static void
DestroyNode(nodePtr)
register Node *nodePtr;
{
if (nodePtr->level == 0) {
CkTextLine *linePtr;
CkTextSegment *segPtr;
while (nodePtr->children.linePtr != NULL) {
linePtr = nodePtr->children.linePtr;
nodePtr->children.linePtr = linePtr->nextPtr;
while (linePtr->segPtr != NULL) {
segPtr = linePtr->segPtr;
linePtr->segPtr = segPtr->nextPtr;
(*segPtr->typePtr->deleteProc)(segPtr, linePtr, 1);
}
ckfree((char *) linePtr);
}
} else {
register Node *childPtr;
while (nodePtr->children.nodePtr != NULL) {
childPtr = nodePtr->children.nodePtr;
nodePtr->children.nodePtr = childPtr->nextPtr;
DestroyNode(childPtr);
}
}
DeleteSummaries(nodePtr->summaryPtr);
ckfree((char *) nodePtr);
}
/*
*----------------------------------------------------------------------
*
* DeleteSummaries --
*
* Free up all of the memory in a list of tag summaries associated
* with a node.
*
* Results:
* None.
*
* Side effects:
* Storage is released.
*
*----------------------------------------------------------------------
*/
static void
DeleteSummaries(summaryPtr)
register Summary *summaryPtr; /* First in list of node's tag
* summaries. */
{
register Summary *nextPtr;
while (summaryPtr != NULL) {
nextPtr = summaryPtr->nextPtr;
ckfree((char *) summaryPtr);
summaryPtr = nextPtr;
}
}
/*
*----------------------------------------------------------------------
*
* CkBTreeInsertChars --
*
* Insert characters at a given position in a B-tree.
*
* Results:
* None.
*
* Side effects:
* Characters are added to the B-tree at the given position.
* If the string contains newlines, new lines will be added,
* which could cause the structure of the B-tree to change.
*
*----------------------------------------------------------------------
*/
void
CkBTreeInsertChars(indexPtr, string)
register CkTextIndex *indexPtr; /* Indicates where to insert text.
* When the procedure returns, this
* index is no longer valid because
* of changes to the segment
* structure. */
char *string; /* Pointer to bytes to insert (may
* contain newlines, must be null-
* terminated). */
{
register Node *nodePtr;
register CkTextSegment *prevPtr; /* The segment just before the first
* new segment (NULL means new segment
* is at beginning of line). */
CkTextSegment *curPtr; /* Current segment; new characters
* are inserted just after this one.
* NULL means insert at beginning of
* line. */
CkTextLine *linePtr; /* Current line (new segments are
* added to this line). */
register CkTextSegment *segPtr;
CkTextLine *newLinePtr;
int chunkSize; /* # characters in current chunk. */
register char *eol; /* Pointer to character just after last
* one in current chunk. */
int changeToLineCount; /* Counts change to total number of
* lines in file. */
prevPtr = SplitSeg(indexPtr);
linePtr = indexPtr->linePtr;
curPtr = prevPtr;
/*
* Chop the string up into lines and create a new segment for
* each line, plus a new line for the leftovers from the
* previous line.
*/
changeToLineCount = 0;
while (*string != 0) {
for (eol = string; *eol != 0; eol++) {
if (*eol == '\n') {
eol++;
break;
}
}
chunkSize = eol-string;
segPtr = (CkTextSegment *) ckalloc(CSEG_SIZE(chunkSize));
segPtr->typePtr = &ckTextCharType;
if (curPtr == NULL) {
segPtr->nextPtr = linePtr->segPtr;
linePtr->segPtr = segPtr;
} else {
segPtr->nextPtr = curPtr->nextPtr;
curPtr->nextPtr = segPtr;
}
segPtr->size = chunkSize;
strncpy(segPtr->body.chars, string, (size_t) chunkSize);
segPtr->body.chars[chunkSize] = 0;
curPtr = segPtr;
if (eol[-1] != '\n') {
break;
}
/*
* The chunk ended with a newline, so create a new CkTextLine
* and move the remainder of the old line to it.
*/
newLinePtr = (CkTextLine *) ckalloc(sizeof(CkTextLine));
newLinePtr->parentPtr = linePtr->parentPtr;
newLinePtr->nextPtr = linePtr->nextPtr;
linePtr->nextPtr = newLinePtr;
newLinePtr->segPtr = segPtr->nextPtr;
segPtr->nextPtr = NULL;
linePtr = newLinePtr;
curPtr = NULL;
changeToLineCount++;
string = eol;
}
/*
* Cleanup the starting line for the insertion, plus the ending
* line if it's different.
*/
CleanupLine(indexPtr->linePtr);
if (linePtr != indexPtr->linePtr) {
CleanupLine(linePtr);
}
/*
* Increment the line counts in all the parent nodes of the insertion
* point, then rebalance the tree if necessary.
*/
for (nodePtr = linePtr->parentPtr ; nodePtr != NULL;
nodePtr = nodePtr->parentPtr) {
nodePtr->numLines += changeToLineCount;
}
nodePtr = linePtr->parentPtr;
nodePtr->numChildren += changeToLineCount;
if (nodePtr->numChildren > MAX_CHILDREN) {
Rebalance((BTree *) indexPtr->tree, nodePtr);
}
if (ckBTreeDebug) {
CkBTreeCheck(indexPtr->tree);
}
}
/*
*--------------------------------------------------------------
*
* SplitSeg --
*
* This procedure is called before adding or deleting
* segments. It does three things: (a) it finds the segment
* containing indexPtr; (b) if there are several such
* segments (because some segments have zero length) then
* it picks the first segment that does not have left
* gravity; (c) if the index refers to the middle of
* a segment then it splits the segment so that the
* index now refers to the beginning of a segment.
*
* Results:
* The return value is a pointer to the segment just
* before the segment corresponding to indexPtr (as
* described above). If the segment corresponding to
* indexPtr is the first in its line then the return
* value is NULL.
*
* Side effects:
* The segment referred to by indexPtr is split unless
* indexPtr refers to its first character.
*
*--------------------------------------------------------------
*/
static CkTextSegment *
SplitSeg(indexPtr)
CkTextIndex *indexPtr; /* Index identifying position
* at which to split a segment. */
{
CkTextSegment *prevPtr, *segPtr;
int count;
for (count = indexPtr->charIndex, prevPtr = NULL,
segPtr = indexPtr->linePtr->segPtr; segPtr != NULL;
count -= segPtr->size, prevPtr = segPtr, segPtr = segPtr->nextPtr) {
if (segPtr->size > count) {
if (count == 0) {
return prevPtr;
}
segPtr = (*segPtr->typePtr->splitProc)(segPtr, count);
if (prevPtr == NULL) {
indexPtr->linePtr->segPtr = segPtr;
} else {
prevPtr->nextPtr = segPtr;
}
return segPtr;
} else if ((segPtr->size == 0) && (count == 0)
&& !segPtr->typePtr->leftGravity) {
return prevPtr;
}
}
panic("SplitSeg reached end of line!");
return NULL;
}
/*
*--------------------------------------------------------------
*
* CleanupLine --
*
* This procedure is called after modifications have been
* made to a line. It scans over all of the segments in
* the line, giving each a chance to clean itself up, e.g.
* by merging with the following segments, updating internal
* information, etc.
*
* Results:
* None.
*
* Side effects:
* Depends on what the segment-specific cleanup procedures do.
*
*--------------------------------------------------------------
*/
static void
CleanupLine(linePtr)
CkTextLine *linePtr; /* Line to be cleaned up. */
{
CkTextSegment *segPtr, **prevPtrPtr;
int anyChanges;
/*
* Make a pass over all of the segments in the line, giving each
* a chance to clean itself up. This could potentially change
* the structure of the line, e.g. by merging two segments
* together or having two segments cancel themselves; if so,
* then repeat the whole process again, since the first structure
* change might make other structure changes possible. Repeat
* until eventually there are no changes.
*/
while (1) {
anyChanges = 0;
for (prevPtrPtr = &linePtr->segPtr, segPtr = *prevPtrPtr;
segPtr != NULL;
prevPtrPtr = &(*prevPtrPtr)->nextPtr, segPtr = *prevPtrPtr) {
if (segPtr->typePtr->cleanupProc != NULL) {
*prevPtrPtr = (*segPtr->typePtr->cleanupProc)(segPtr, linePtr);
if (segPtr != *prevPtrPtr) {
anyChanges = 1;
}
}
}
if (!anyChanges) {
break;
}
}
}
/*
*----------------------------------------------------------------------
*
* CkBTreeDeleteChars --
*
* Delete a range of characters from a B-tree. The caller
* must make sure that the final newline of the B-tree is
* never deleted.
*
* Results:
* None.
*
* Side effects:
* Information is deleted from the B-tree. This can cause the
* internal structure of the B-tree to change. Note: because
* of changes to the B-tree structure, the indices pointed
* to by index1Ptr and index2Ptr should not be used after this
* procedure returns.
*
*----------------------------------------------------------------------
*/
void
CkBTreeDeleteChars(index1Ptr, index2Ptr)
register CkTextIndex *index1Ptr; /* Indicates first character that is
* to be deleted. */
register CkTextIndex *index2Ptr; /* Indicates character just after the
* last one that is to be deleted. */
{
CkTextSegment *prevPtr; /* The segment just before the start
* of the deletion range. */
CkTextSegment *lastPtr; /* The segment just after the end
* of the deletion range. */
CkTextSegment *segPtr, *nextPtr;
CkTextLine *curLinePtr;
Node *curNodePtr, *nodePtr;
/*
* Tricky point: split at index2Ptr first; otherwise the split
* at index2Ptr may invalidate segPtr and/or prevPtr.
*/
lastPtr = SplitSeg(index2Ptr);
if (lastPtr != NULL) {
lastPtr = lastPtr->nextPtr;
} else {
lastPtr = index2Ptr->linePtr->segPtr;
}
prevPtr = SplitSeg(index1Ptr);
if (prevPtr != NULL) {
segPtr = prevPtr->nextPtr;
prevPtr->nextPtr = lastPtr;
} else {
segPtr = index1Ptr->linePtr->segPtr;
index1Ptr->linePtr->segPtr = lastPtr;
}
/*
* Delete all of the segments between prevPtr and lastPtr.
*/
curLinePtr = index1Ptr->linePtr;
curNodePtr = curLinePtr->parentPtr;
while (segPtr != lastPtr) {
if (segPtr == NULL) {
CkTextLine *nextLinePtr;
/*
* We just ran off the end of a line. First find the
* next line, then go back to the old line and delete it
* (unless it's the starting line for the range).
*/
nextLinePtr = CkBTreeNextLine(curLinePtr);
if (curLinePtr != index1Ptr->linePtr) {
if (curNodePtr == index1Ptr->linePtr->parentPtr) {
index1Ptr->linePtr->nextPtr = curLinePtr->nextPtr;
} else {
curNodePtr->children.linePtr = curLinePtr->nextPtr;
}
for (nodePtr = curNodePtr; nodePtr != NULL;
nodePtr = nodePtr->parentPtr) {
nodePtr->numLines--;
}
curNodePtr->numChildren--;
ckfree((char *) curLinePtr);
}
curLinePtr = nextLinePtr;
segPtr = curLinePtr->segPtr;
/*
* If the node is empty then delete it and its parents,
* recursively upwards until a non-empty node is found.
*/
while (curNodePtr->numChildren == 0) {
Node *parentPtr;
parentPtr = curNodePtr->parentPtr;
if (parentPtr->children.nodePtr == curNodePtr) {
parentPtr->children.nodePtr = curNodePtr->nextPtr;
} else {
Node *prevNodePtr = parentPtr->children.nodePtr;
while (prevNodePtr->nextPtr != curNodePtr) {
prevNodePtr = prevNodePtr->nextPtr;
}
prevNodePtr->nextPtr = curNodePtr->nextPtr;
}
parentPtr->numChildren--;
ckfree((char *) curNodePtr);
curNodePtr = parentPtr;
}
curNodePtr = curLinePtr->parentPtr;
continue;
}
nextPtr = segPtr->nextPtr;
if ((*segPtr->typePtr->deleteProc)(segPtr, curLinePtr, 0) != 0) {
/*
* This segment refuses to die. Move it to prevPtr and
* advance prevPtr if the segment has left gravity.
*/
if (prevPtr == NULL) {
segPtr->nextPtr = index1Ptr->linePtr->segPtr;
index1Ptr->linePtr->segPtr = segPtr;
} else {
segPtr->nextPtr = prevPtr->nextPtr;
prevPtr->nextPtr = segPtr;
}
if (segPtr->typePtr->leftGravity) {
prevPtr = segPtr;
}
}
segPtr = nextPtr;
}
/*
* If the beginning and end of the deletion range are in different
* lines, join the two lines together and discard the ending line.
*/
if (index1Ptr->linePtr != index2Ptr->linePtr) {
CkTextLine *prevLinePtr;
for (segPtr = lastPtr; segPtr != NULL;
segPtr = segPtr->nextPtr) {
if (segPtr->typePtr->lineChangeProc != NULL) {
(*segPtr->typePtr->lineChangeProc)(segPtr, index2Ptr->linePtr);
}
}
curNodePtr = index2Ptr->linePtr->parentPtr;
for (nodePtr = curNodePtr; nodePtr != NULL;
nodePtr = nodePtr->parentPtr) {
nodePtr->numLines--;
}
curNodePtr->numChildren--;
prevLinePtr = curNodePtr->children.linePtr;
if (prevLinePtr == index2Ptr->linePtr) {
curNodePtr->children.linePtr = index2Ptr->linePtr->nextPtr;
} else {
while (prevLinePtr->nextPtr != index2Ptr->linePtr) {
prevLinePtr = prevLinePtr->nextPtr;
}
prevLinePtr->nextPtr = index2Ptr->linePtr->nextPtr;
}
ckfree((char *) index2Ptr->linePtr);
Rebalance((BTree *) index2Ptr->tree, curNodePtr);
}
/*
* Cleanup the segments in the new line.
*/
CleanupLine(index1Ptr->linePtr);
/*
* Lastly, rebalance the first node of the range.
*/
Rebalance((BTree *) index1Ptr->tree, index1Ptr->linePtr->parentPtr);
if (ckBTreeDebug) {
CkBTreeCheck(index1Ptr->tree);
}
}
/*
*----------------------------------------------------------------------
*
* CkBTreeFindLine --
*
* Find a particular line in a B-tree based on its line number.
*
* Results:
* The return value is a pointer to the line structure for the
* line whose index is "line", or NULL if no such line exists.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
CkTextLine *
CkBTreeFindLine(tree, line)
CkTextBTree tree; /* B-tree in which to find line. */
int line; /* Index of desired line. */
{
BTree *treePtr = (BTree *) tree;
register Node *nodePtr;
register CkTextLine *linePtr;
int linesLeft;
nodePtr = treePtr->rootPtr;
linesLeft = line;
if ((line < 0) || (line >= nodePtr->numLines)) {
return NULL;
}
/*
* Work down through levels of the tree until a node is found at
* level 0.
*/
while (nodePtr->level != 0) {
for (nodePtr = nodePtr->children.nodePtr;
nodePtr->numLines <= linesLeft;
nodePtr = nodePtr->nextPtr) {
if (nodePtr == NULL) {
panic("CkBTreeFindLine ran out of nodes");
}
linesLeft -= nodePtr->numLines;
}
}
/*
* Work through the lines attached to the level-0 node.
*/
for (linePtr = nodePtr->children.linePtr; linesLeft > 0;
linePtr = linePtr->nextPtr) {
if (linePtr == NULL) {
panic("CkBTreeFindLine ran out of lines");
}
linesLeft -= 1;
}
return linePtr;
}
/*
*----------------------------------------------------------------------
*
* CkBTreeNextLine --
*
* Given an existing line in a B-tree, this procedure locates the
* next line in the B-tree. This procedure is used for scanning
* through the B-tree.
*
* Results:
* The return value is a pointer to the line that immediately
* follows linePtr, or NULL if there is no such line.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
CkTextLine *
CkBTreeNextLine(linePtr)
register CkTextLine *linePtr; /* Pointer to existing line in
* B-tree. */
{
register Node *nodePtr;
if (linePtr->nextPtr != NULL) {
return linePtr->nextPtr;
}
/*
* This was the last line associated with the particular parent node.
* Search up the tree for the next node, then search down from that
* node to find the first line,
*/
for (nodePtr = linePtr->parentPtr; ; nodePtr = nodePtr->parentPtr) {
if (nodePtr->nextPtr != NULL) {
nodePtr = nodePtr->nextPtr;
break;
}
if (nodePtr->parentPtr == NULL) {
return (CkTextLine *) NULL;
}
}
while (nodePtr->level > 0) {
nodePtr = nodePtr->children.nodePtr;
}
return nodePtr->children.linePtr;
}
/*
*----------------------------------------------------------------------
*
* CkBTreeLineIndex --
*
* Given a pointer to a line in a B-tree, return the numerical
* index of that line.
*
* Results:
* The result is the index of linePtr within the tree, where 0
* corresponds to the first line in the tree.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
CkBTreeLineIndex(linePtr)
CkTextLine *linePtr; /* Pointer to existing line in
* B-tree. */
{
register CkTextLine *linePtr2;
register Node *nodePtr, *parentPtr, *nodePtr2;
int index;
/*
* First count how many lines precede this one in its level-0
* node.
*/
nodePtr = linePtr->parentPtr;
index = 0;
for (linePtr2 = nodePtr->children.linePtr; linePtr2 != linePtr;
linePtr2 = linePtr2->nextPtr) {
if (linePtr2 == NULL) {
panic("CkBTreeLineIndex couldn't find line");
}
index += 1;
}
/*
* Now work up through the levels of the tree one at a time,
* counting how many lines are in nodes preceding the current
* node.
*/
for (parentPtr = nodePtr->parentPtr ; parentPtr != NULL;
nodePtr = parentPtr, parentPtr = parentPtr->parentPtr) {
for (nodePtr2 = parentPtr->children.nodePtr; nodePtr2 != nodePtr;
nodePtr2 = nodePtr2->nextPtr) {
if (nodePtr2 == NULL) {
panic("CkBTreeLineIndex couldn't find node");
}
index += nodePtr2->numLines;
}
}
return index;
}
/*
*----------------------------------------------------------------------
*
* CkBTreeLinkSegment --
*
* This procedure adds a new segment to a B-tree at a given
* location.
*
* Results:
* None.
*
* Side effects:
* SegPtr will be linked into its tree.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
void
CkBTreeLinkSegment(segPtr, indexPtr)
CkTextSegment *segPtr; /* Pointer to new segment to be added to
* B-tree. Should be completely initialized
* by caller except for nextPtr field. */
CkTextIndex *indexPtr; /* Where to add segment: it gets linked
* in just before the segment indicated
* here. */
{
register CkTextSegment *prevPtr;
prevPtr = SplitSeg(indexPtr);
if (prevPtr == NULL) {
segPtr->nextPtr = indexPtr->linePtr->segPtr;