Skip to content

Commit

Permalink
new-feature: array: add ExtendIndex to allocate additional 0-bits aft…
Browse files Browse the repository at this point in the history
…er Bitmaps and Offsets
  • Loading branch information
drmingdrmer committed Jun 1, 2019
1 parent dd20fcd commit a297666
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions array/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ func (a *Base) InitIndex(index []int32) error {
return nil
}

// ExtendIndex allocaed additional 0-bits after Bitmap and Offset.
//
// Since 0.5.8
func (a *Base) ExtendIndex(n int32) {
nword := (n + 63) >> 6

if nword <= int32(len(a.Bitmaps)) {
return
}

bitmaps := make([]uint64, nword)
copy(bitmaps, a.Bitmaps)

a.Bitmaps = bitmaps
a.Offsets = newRankIndex1(a.Bitmaps)
for i, word := range a.Bitmaps {
if word == 0 {
a.Offsets[i] = 0
}
}
}

// GetEltIndex returns the position in a.Elts of element[idx] and a bool
// indicating if found or not.
// If "idx" absents it returns "0, false".
Expand Down
24 changes: 24 additions & 0 deletions array/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@ func TestBaseInitIndex(t *testing.T) {
}
}

func TestBase_ExtendIndex(t *testing.T) {

ta := require.New(t)

a := &array.Base{}
err := a.InitIndex([]int32{0, 1, 64, 66})
ta.Nil(err)

a.ExtendIndex(-1)
ta.Equal([]uint64{3, 5}, a.Bitmaps)
ta.Equal([]int32{0, 2}, a.Offsets)

a.ExtendIndex(128)
ta.Equal([]uint64{3, 5}, a.Bitmaps)
ta.Equal([]int32{0, 2}, a.Offsets)

a.ExtendIndex(256)
fmt.Println(a)

ta.Equal([]uint64{3, 5, 0, 0}, a.Bitmaps)
ta.Equal([]int32{0, 2, 0, 0}, a.Offsets)

}

func TestBaseInit(t *testing.T) {

ta := require.New(t)
Expand Down

0 comments on commit a297666

Please sign in to comment.