github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/packed/growableWriter.go (about) 1 package packed 2 3 import ( 4 "github.com/balzaczyy/golucene/core/util" 5 ) 6 7 type GrowableWriter struct { 8 *abstractMutable 9 currentMask int64 10 current Mutable 11 acceptableOverheadRatio float32 12 } 13 14 func NewGrowableWriter(startBitsPerValue, valueCount int, 15 acceptableOverheadRatio float32) *GrowableWriter { 16 m := MutableFor(valueCount, startBitsPerValue, acceptableOverheadRatio) 17 ans := &GrowableWriter{ 18 acceptableOverheadRatio: acceptableOverheadRatio, 19 current: m, 20 currentMask: mask(m.BitsPerValue()), 21 } 22 ans.abstractMutable = newMutable(ans) 23 return ans 24 } 25 26 func mask(bitsPerValue int) int64 { 27 if bitsPerValue == 64 { 28 return ^0 29 } 30 return MaxValue(bitsPerValue) 31 } 32 33 func (w *GrowableWriter) Get(index int) int64 { 34 return w.current.Get(index) 35 } 36 37 func (w *GrowableWriter) Size() int { 38 return w.current.Size() 39 } 40 41 func (w *GrowableWriter) BitsPerValue() int { 42 return w.current.BitsPerValue() 43 } 44 45 func (w *GrowableWriter) ensureCapacity(value int64) { 46 if (value & w.currentMask) == value { 47 return 48 } 49 var bitsRequired int 50 if value < 0 { 51 bitsRequired = 64 52 } else { 53 bitsRequired = UnsignedBitsRequired(value) 54 } 55 assert(bitsRequired > w.current.BitsPerValue()) 56 valueCount := int(w.Size()) 57 next := MutableFor(valueCount, bitsRequired, w.acceptableOverheadRatio) 58 Copy(w.current, 0, next, 0, valueCount, DEFAULT_BUFFER_SIZE) 59 w.current = next 60 w.currentMask = mask(w.current.BitsPerValue()) 61 } 62 63 func (w *GrowableWriter) Set(index int, value int64) { 64 w.ensureCapacity(value) 65 w.current.Set(index, value) 66 } 67 68 func (w *GrowableWriter) Clear() { 69 w.current.Clear() 70 } 71 72 func (w *GrowableWriter) getBulk(index int, arr []int64) int { 73 panic("niy") 74 } 75 76 func (w *GrowableWriter) setBulk(index int, arr []int64) int { 77 panic("niy") 78 } 79 80 func (w *GrowableWriter) fill(from, to int, val int64) { 81 panic("niy") 82 } 83 84 func (w *GrowableWriter) RamBytesUsed() int64 { 85 return util.AlignObjectSize( 86 util.NUM_BYTES_OBJECT_HEADER+ 87 util.NUM_BYTES_OBJECT_REF+ 88 util.NUM_BYTES_LONG+ 89 util.NUM_BYTES_FLOAT) + 90 w.current.RamBytesUsed() 91 } 92 93 func (w *GrowableWriter) Save(out util.DataOutput) error { 94 return w.current.Save(out) 95 }