@@ -776,8 +776,8 @@ func (t *btree[M]) Insert(item M) error {
776
776
// tableMetadataIter returns a new iterator over a B-Tree of *TableMetadata. It
777
777
// is not safe to continue using an iterator after modifications are made to the
778
778
// tree. If modifications are made, create a new iterator.
779
- func tableMetadataIter (tree * btree [* TableMetadata ]) iterator {
780
- return iterator {r : tree .root , pos : - 1 , cmp : tree .bcmp }
779
+ func tableMetadataIter (tree * btree [* TableMetadata ]) iterator [ * TableMetadata ] {
780
+ return iterator [ * TableMetadata ] {r : tree .root , pos : - 1 , cmp : tree .bcmp }
781
781
}
782
782
783
783
// Count returns the number of files contained within the B-Tree.
@@ -821,28 +821,28 @@ func (n *node[M]) writeString(b *strings.Builder) {
821
821
822
822
// iterStack represents a stack of (node, pos) tuples, which captures
823
823
// iteration state as an iterator descends a btree of TableMetadata.
824
- type iterStack struct {
824
+ type iterStack [ M fileMetadata ] struct {
825
825
// a contains aLen stack frames when an iterator stack is short enough.
826
826
// If the iterator stack overflows the capacity of iterStackArr, the stack
827
827
// is moved to s and aLen is set to -1.
828
- a iterStackArr
828
+ a iterStackArr [ M ]
829
829
aLen int16 // -1 when using s
830
- s []iterFrame
830
+ s []iterFrame [ M ]
831
831
}
832
832
833
833
// Used to avoid allocations for stacks below a certain size.
834
- type iterStackArr [3 ]iterFrame
834
+ type iterStackArr [ M fileMetadata ] [3 ]iterFrame [ M ]
835
835
836
- type iterFrame struct {
837
- n * node [* TableMetadata ]
836
+ type iterFrame [ M fileMetadata ] struct {
837
+ n * node [M ]
838
838
pos int16
839
839
}
840
840
841
- func (is * iterStack ) push (f iterFrame ) {
841
+ func (is * iterStack [ M ] ) push (f iterFrame [ M ] ) {
842
842
if is .aLen == - 1 {
843
843
is .s = append (is .s , f )
844
844
} else if int (is .aLen ) == len (is .a ) {
845
- is .s = make ([]iterFrame , int (is .aLen )+ 1 , 2 * int (is .aLen ))
845
+ is .s = make ([]iterFrame [ M ] , int (is .aLen )+ 1 , 2 * int (is .aLen ))
846
846
copy (is .s , is .a [:])
847
847
is .s [int (is .aLen )] = f
848
848
is .aLen = - 1
@@ -852,7 +852,7 @@ func (is *iterStack) push(f iterFrame) {
852
852
}
853
853
}
854
854
855
- func (is * iterStack ) pop () iterFrame {
855
+ func (is * iterStack [ M ] ) pop () iterFrame [ M ] {
856
856
if is .aLen == - 1 {
857
857
f := is .s [len (is .s )- 1 ]
858
858
is .s = is .s [:len (is .s )- 1 ]
@@ -862,26 +862,26 @@ func (is *iterStack) pop() iterFrame {
862
862
return is .a [is .aLen ]
863
863
}
864
864
865
- func (is * iterStack ) len () int {
865
+ func (is * iterStack [ M ] ) len () int {
866
866
if is .aLen == - 1 {
867
867
return len (is .s )
868
868
}
869
869
return int (is .aLen )
870
870
}
871
871
872
- func (is * iterStack ) clone () iterStack {
872
+ func (is * iterStack [ M ] ) clone () iterStack [ M ] {
873
873
// If the iterator is using the embedded iterStackArr, we only need to
874
874
// copy the struct itself.
875
875
if is .s == nil {
876
876
return * is
877
877
}
878
878
clone := * is
879
- clone .s = make ([]iterFrame , len (is .s ))
879
+ clone .s = make ([]iterFrame [ M ] , len (is .s ))
880
880
copy (clone .s , is .s )
881
881
return clone
882
882
}
883
883
884
- func (is * iterStack ) nth (n int ) (f iterFrame , ok bool ) {
884
+ func (is * iterStack [ M ] ) nth (n int ) (f iterFrame [ M ] , ok bool ) {
885
885
if is .aLen == - 1 {
886
886
if n >= len (is .s ) {
887
887
return f , false
@@ -894,7 +894,7 @@ func (is *iterStack) nth(n int) (f iterFrame, ok bool) {
894
894
return is .a [n ], true
895
895
}
896
896
897
- func (is * iterStack ) reset () {
897
+ func (is * iterStack [ M ] ) reset () {
898
898
if is .aLen == - 1 {
899
899
is .s = is .s [:0 ]
900
900
} else {
@@ -903,26 +903,26 @@ func (is *iterStack) reset() {
903
903
}
904
904
905
905
// an iterator provides search and traversal within a btree of *TableMetadata.
906
- type iterator struct {
906
+ type iterator [ M fileMetadata ] struct {
907
907
// the root node of the B-Tree.
908
- r * node [* TableMetadata ]
908
+ r * node [M ]
909
909
// n and pos make up the current position of the iterator.
910
910
// If valid, n.items[pos] is the current value of the iterator.
911
911
//
912
912
// n may be nil iff i.r is nil.
913
- n * node [* TableMetadata ]
913
+ n * node [M ]
914
914
pos int16
915
915
// cmp dictates the ordering of the TableMetadata.
916
- cmp func (* TableMetadata , * TableMetadata ) int
916
+ cmp func (M , M ) int
917
917
// a stack of n's ancestors within the B-Tree, alongside the position
918
918
// taken to arrive at n. If non-empty, the bottommost frame of the stack
919
919
// will always contain the B-Tree root.
920
- s iterStack
920
+ s iterStack [ M ]
921
921
}
922
922
923
923
// countLeft returns the count of files that are to the left of the current
924
924
// iterator position.
925
- func (i * iterator ) countLeft () int {
925
+ func (i * iterator [ M ] ) countLeft () int {
926
926
if i .r == nil {
927
927
return 0
928
928
}
@@ -973,19 +973,19 @@ func (i *iterator) countLeft() int {
973
973
return count
974
974
}
975
975
976
- func (i * iterator ) clone () iterator {
976
+ func (i * iterator [ M ] ) clone () iterator [ M ] {
977
977
c := * i
978
978
c .s = i .s .clone ()
979
979
return c
980
980
}
981
981
982
- func (i * iterator ) reset () {
982
+ func (i * iterator [ M ] ) reset () {
983
983
i .n = i .r
984
984
i .pos = - 1
985
985
i .s .reset ()
986
986
}
987
987
988
- func (i iterator ) String () string {
988
+ func (i iterator [ M ] ) String () string {
989
989
var buf bytes.Buffer
990
990
for n := 0 ; ; n ++ {
991
991
f , ok := i .s .nth (n )
@@ -1002,7 +1002,7 @@ func (i iterator) String() string {
1002
1002
return buf .String ()
1003
1003
}
1004
1004
1005
- func cmpIter (a , b iterator ) int {
1005
+ func cmpIter [ M fileMetadata ] (a , b iterator [ M ] ) int {
1006
1006
if a .r != b .r {
1007
1007
panic ("compared iterators from different btrees" )
1008
1008
}
@@ -1044,7 +1044,7 @@ func cmpIter(a, b iterator) int {
1044
1044
// end sentinel state which sorts after everything else.
1045
1045
var aok , bok bool
1046
1046
for i := 0 ; ; i ++ {
1047
- var af , bf iterFrame
1047
+ var af , bf iterFrame [ M ]
1048
1048
af , aok = a .s .nth (i )
1049
1049
bf , bok = b .s .nth (i )
1050
1050
if ! aok || ! bok {
@@ -1095,15 +1095,15 @@ func cmpIter(a, b iterator) int {
1095
1095
}
1096
1096
}
1097
1097
1098
- func (i * iterator ) descend (n * node [* TableMetadata ], pos int16 ) {
1099
- i .s .push (iterFrame {n : n , pos : pos })
1098
+ func (i * iterator [ M ] ) descend (n * node [M ], pos int16 ) {
1099
+ i .s .push (iterFrame [ M ] {n : n , pos : pos })
1100
1100
i .n = n .children [pos ]
1101
1101
i .pos = 0
1102
1102
}
1103
1103
1104
1104
// ascend ascends up to the current node's parent and resets the position
1105
1105
// to the one previously set for this parent node.
1106
- func (i * iterator ) ascend () {
1106
+ func (i * iterator [ M ] ) ascend () {
1107
1107
f := i .s .pop ()
1108
1108
i .n = f .n
1109
1109
i .pos = f .pos
@@ -1112,7 +1112,7 @@ func (i *iterator) ascend() {
1112
1112
// find seeks the iterator to the provided table metadata if it exists in the
1113
1113
// tree. It returns true if the table metadata is found and false otherwise. If
1114
1114
// find returns false, the position of the iterator is undefined.
1115
- func (i * iterator ) find (m * TableMetadata ) bool {
1115
+ func (i * iterator [ M ] ) find (m M ) bool {
1116
1116
i .reset ()
1117
1117
if i .r == nil {
1118
1118
return false
@@ -1131,7 +1131,7 @@ func (i *iterator) find(m *TableMetadata) bool {
1131
1131
}
1132
1132
1133
1133
// first seeks to the first item in the btree.
1134
- func (i * iterator ) first () {
1134
+ func (i * iterator [ M ] ) first () {
1135
1135
i .reset ()
1136
1136
if i .r == nil {
1137
1137
return
@@ -1143,7 +1143,7 @@ func (i *iterator) first() {
1143
1143
}
1144
1144
1145
1145
// last seeks to the last item in the btree.
1146
- func (i * iterator ) last () {
1146
+ func (i * iterator [ M ] ) last () {
1147
1147
i .reset ()
1148
1148
if i .r == nil {
1149
1149
return
@@ -1156,7 +1156,7 @@ func (i *iterator) last() {
1156
1156
1157
1157
// next positions the iterator to the item immediately following
1158
1158
// its current position.
1159
- func (i * iterator ) next () {
1159
+ func (i * iterator [ M ] ) next () {
1160
1160
if i .r == nil {
1161
1161
return
1162
1162
}
@@ -1183,7 +1183,7 @@ func (i *iterator) next() {
1183
1183
1184
1184
// prev positions the iterator to the item immediately preceding
1185
1185
// its current position.
1186
- func (i * iterator ) prev () {
1186
+ func (i * iterator [ M ] ) prev () {
1187
1187
if i .r == nil {
1188
1188
return
1189
1189
}
@@ -1208,13 +1208,13 @@ func (i *iterator) prev() {
1208
1208
}
1209
1209
1210
1210
// valid returns whether the iterator is positioned at a valid position.
1211
- func (i * iterator ) valid () bool {
1211
+ func (i * iterator [ M ] ) valid () bool {
1212
1212
return i .r != nil && i .pos >= 0 && i .pos < i .n .count
1213
1213
}
1214
1214
1215
1215
// cur returns the table metadata at the iterator's current position. It is
1216
1216
// illegal to call cur if the iterator is not valid.
1217
- func (i * iterator ) cur () * TableMetadata {
1217
+ func (i * iterator [ M ] ) cur () M {
1218
1218
if invariants .Enabled && ! i .valid () {
1219
1219
panic ("btree iterator.cur invoked on invalid iterator" )
1220
1220
}
0 commit comments