6262// for any type, and require a passed-in "less" function to define their ordering.
6363// Those without this prefix are specific to the 'Item' interface, and use
6464// its 'Less' function for ordering.
65-
6665package btree
6766
6867import (
69- "fmt"
70- "io"
7168 "sort"
72- "strings"
7369 "sync"
7470)
7571
@@ -83,9 +79,8 @@ type Item interface {
8379 Less (than Item ) bool
8480}
8581
86- const (
87- DefaultFreeListSize = 32
88- )
82+ // DefaultFreeListSize is the default capacity of a BTree node free list.
83+ const DefaultFreeListSize = 32
8984
9085// FreeListG represents a free list of btree nodes. By default each
9186// BTree has its own FreeList, but multiple BTrees can share the same
@@ -136,7 +131,7 @@ type Ordered interface {
136131 ~ int | ~ int8 | ~ int16 | ~ int32 | ~ int64 | ~ uint | ~ uint8 | ~ uint16 | ~ uint32 | ~ uint64 | ~ float32 | ~ float64 | ~ string
137132}
138133
139- // Less[T] returns a default LessFunc that uses the '<' operator for types that support it.
134+ // Less returns a default LessFunc that uses the '<' operator for types that support it.
140135func Less [T Ordered ]() LessFunc [T ] {
141136 return func (a , b T ) bool { return a < b }
142137}
@@ -575,14 +570,6 @@ func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeSta
575570 return hit , true
576571}
577572
578- // print is used for testing/debugging purposes.
579- func (n * node [T ]) print (w io.Writer , level int ) {
580- fmt .Fprintf (w , "%sNODE:%v\n " , strings .Repeat (" " , level ), n .items )
581- for _ , c := range n .children {
582- c .print (w , level + 1 )
583- }
584- }
585-
586573// BTreeG is a generic implementation of a B-Tree.
587574//
588575// BTreeG stores items of type T in an ordered structure, allowing easy insertion,
@@ -597,7 +584,7 @@ type BTreeG[T any] struct {
597584 cow * copyOnWriteContext [T ]
598585}
599586
600- // LessFunc[T] determines how to order a type 'T'. It should implement a strict
587+ // LessFunc determines how to order a type 'T'. It should implement a strict
601588// ordering, and should return true if within that ordering, 'a' < 'b'.
602589type LessFunc [T any ] func (a , b T ) bool
603590
@@ -680,12 +667,10 @@ func (c *copyOnWriteContext[T]) freeNode(n *node[T]) freeType {
680667 n .cow = nil
681668 if c .freelist .freeNode (n ) {
682669 return ftStored
683- } else {
684- return ftFreelistFull
685670 }
686- } else {
687- return ftNotOwned
671+ return ftFreelistFull
688672 }
673+ return ftNotOwned
689674}
690675
691676// ReplaceOrInsert adds the given item to the tree. If an item in the tree
@@ -699,15 +684,14 @@ func (t *BTreeG[T]) ReplaceOrInsert(item T) (_ T, _ bool) {
699684 t .root .items = append (t .root .items , item )
700685 t .length ++
701686 return
702- } else {
703- t .root = t .root .mutableFor (t .cow )
704- if len (t .root .items ) >= t .maxItems () {
705- item2 , second := t .root .split (t .maxItems () / 2 )
706- oldroot := t .root
707- t .root = t .cow .newNode ()
708- t .root .items = append (t .root .items , item2 )
709- t .root .children = append (t .root .children , oldroot , second )
710- }
687+ }
688+ t .root = t .root .mutableFor (t .cow )
689+ if len (t .root .items ) >= t .maxItems () {
690+ item2 , second := t .root .split (t .maxItems () / 2 )
691+ oldroot := t .root
692+ t .root = t .cow .newNode ()
693+ t .root .items = append (t .root .items , item2 )
694+ t .root .children = append (t .root .children , oldroot , second )
711695 }
712696 out , outb := t .root .insert (item , t .maxItems ())
713697 if ! outb {
0 commit comments