Skip to content

Commit c514e5d

Browse files
committed
doc number vs integer
1 parent 44a362a commit c514e5d

File tree

5 files changed

+49
-41
lines changed

5 files changed

+49
-41
lines changed

README.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# VarInt: fast & memory efficient arbitrary bit length integers.
2+
3+
## Introduction
4+
5+
## Examples
6+
7+
## Benchmarks
8+
19
## Licence
210

311
VarInt is licensed under the MIT License.

bits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// b62digits const preallocated alphabet.
1414
const b62digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
1515

16-
// Bits is immutable intermediate representation for single number inside VarInt.
16+
// Bits is immutable intermediate representation for single integer inside VarInt.
1717
// It's used as data transfer object for most of VarInt operations, and
1818
// provides a number of convenient methods to convert it back and forth
1919
// between other numerical presentations. Bits type somewhat resembles

support_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ func TestSortable(t *testing.T) {
115115
sort.Sort(Sortable(vint))
116116
for i, j := 0, 1; i < len-1; i, j = i+1, j+1 {
117117
bi, bj := h.VarIntGet(i), h.VarIntGet(j)
118-
// Numbers could be equal too.
118+
// Integers could be equal too.
119119
h.Equal(Compare(bi, bj) <= 0, true)
120120
}
121121
sort.Sort(sort.Reverse(Sortable(vint)))
122122
for i, j := 0, 1; i < len-1; i, j = i+1, j+1 {
123123
bi, bj := h.VarIntGet(i), h.VarIntGet(j)
124-
// Numbers could be equal too.
124+
// Integers could be equal too.
125125
h.Equal(Compare(bi, bj) >= 0, true)
126126
}
127127
})

varinit.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@ import math_bits "math/bits"
55
// wsize const alias to system uint word size in bits.
66
const wsize = math_bits.UintSize
77

8-
// VarInt defines the memory efficient unsigned numeric array type that provides
9-
// basic arithmetic and bitwise operations on variadic bit length numbers. The purpose
10-
// of VarInt to provide the memory optimal way to use unsigned custom bit len numbers.
11-
// It does so by storing all the numbers adjacent to each other inside
8+
// VarInt defines the memory efficient unsigned integer array type that provides
9+
// basic arithmetic and bitwise operations on arbitrary variadic bit length integers.
10+
// The purpose of VarInt to provide the memory optimal way to use unsigned custom bit len integers.
11+
// It does so by storing all the integers adjacent to each other inside
1212
// continuous numeric bytes slice. To function, it allocates the underlying
1313
// numeric bytes slice only once on creation and doesn't expect to allocate
1414
// any more new memory for any of its operations thereafter. To apply any
1515
// of its operations, some bits manipulations are required which implies
1616
// some computational overhead. Thus providing a tradeoff between CPU time and memeory.
1717
// Overhead grows lineraly proportionally to bit len and comparable with overhead
1818
// provided by big.Int type operations. Unlike big.Int however, VarInt uses exact number
19-
// of bits to store the numbers inside. Which makes VarInt extremely memory efficient. For example,
20-
// to store a slice of 100 number 100 bit each, big.Int requires 12400 bits while
19+
// of bits to store the integers inside. Which makes VarInt extremely memory efficient. For example,
20+
// to store a slice of 100 integers 100 bit each, big.Int requires 12400 bits while
2121
// VarInt needs exactly 10000 bits (excluding fixed internal overhead).
22-
// In the same way VarInt also provides the efficient way to store numbers smaller than 64 bits.
23-
// For example, to store a slice of 1000 number 2 bit each, []uin8 requires 8000 bits
22+
// In the same way VarInt also provides the efficient way to store integers smaller than 64 bits.
23+
// For example, to store a slice of 1000 integers 2 bit each, []uin8 requires 8000 bits
2424
// while VarInt needs exactly 2000 bits (excluding fixed internal overhead).
2525
// Note however, that VarInt is no way close to be optimized as well as big.Int, and
26-
// provides a diminishing returns as bit length grows above certain threshold. Currently,
26+
// provides diminishing returns as bit length grows above certain threshold. Currently,
2727
// in a conscious decision multiple operations implemented in favor of simplicity and
2828
// not computational complexity, this includes Mul that uses standard long multiplication
2929
// instead of fast multiplication algorithms like Karatsuba multiplication, and Div that
3030
// uses standard slow division instead of fast division algorithms. The main rationale behind
31-
// the choice is the fact that VarInt has the most efficiency when used for small and medium size numbers
31+
// the choice is the fact that VarInt has the most efficiency when used for small and medium size integers
3232
// in the range of 1 to 5000 bits, therefore asymptotic complexity should have less of impact.
3333
// VarInt carries a small fixed overhead internaly, it allocates 2 separate uint cells at the beginning
3434
// of the numeric bytes slice to store length and bit length somewhere. It also collocates extra Bits
@@ -41,7 +41,7 @@ const wsize = math_bits.UintSize
4141
type VarInt []uint
4242

4343
// NewVarInt allocates and returns VarInt instance that is capable to
44-
// fit the provided len number of items each of the provided bit len in size.
44+
// fit the provided number of integers each of the provided bit len in width.
4545
// In case the provided bit len is not positive, invalid number and ErrorBitLengthIsNotPositive is returned.
4646
// In case the len is not positive, invalid number and ErrorLengthIsNotPositive is returned.
4747
// In case the provided bit len is larger than predefined threshold of 4096,
@@ -56,7 +56,7 @@ func NewVarInt(blen, len int) (VarInt, error) {
5656
if len <= 0 {
5757
return nil, ErrorLengthIsNotPositive
5858
}
59-
// Calculate capacity to fit all numbers with
59+
// Calculate capacity to fit all integers with
6060
// provided bit length and capacity.
6161
cap := (blen*len+wsize-1)/wsize + 2
6262
// Calculate number of whole words plus
@@ -85,7 +85,7 @@ func NewVarInt(blen, len int) (VarInt, error) {
8585
}
8686
}
8787

88-
// Get sets the provided bits to the number inside VarInt at the provided index.
88+
// Get sets the provided bits to the integer inside VarInt at the provided index.
8989
// It never allocates new Bits, the provided Bits are expected to be preallocated by the caller.
9090
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
9191
// In case negative index is provided, ErrorIndexIsNegative is returned.
@@ -143,7 +143,7 @@ func (vint VarInt) Get(i int, bits Bits) error {
143143
return nil
144144
}
145145

146-
// Set sets the provided bits into the number inside VarInt at the provided index.
146+
// Set sets the provided bits into the integer inside VarInt at the provided index.
147147
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
148148
// In case negative index is provided, ErrorIndexIsNegative is returned.
149149
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -207,7 +207,7 @@ func (vint VarInt) Set(i int, bits Bits) error {
207207
return nil
208208
}
209209

210-
// GetSet swaps the provided bits with the number inside VarInt at the provided index.
210+
// GetSet swaps the provided bits with the integer inside VarInt at the provided index.
211211
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
212212
// In case negative index is provided, ErrorIndexIsNegative is returned.
213213
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -277,7 +277,7 @@ func (vint VarInt) GetSet(i int, bits Bits) error {
277277
return nil
278278
}
279279

280-
// Add adds the provided bits to the number inside VarInt at the provided index.
280+
// Add adds the provided bits to the integer inside VarInt at the provided index.
281281
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
282282
// In case negative index is provided, ErrorIndexIsNegative is returned.
283283
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -373,12 +373,12 @@ func (vint VarInt) Add(i int, bits Bits) error {
373373
return nil
374374
}
375375

376-
// Sub subtracts the provided bits from the number inside VarInt at the provided index.
376+
// Sub subtracts the provided bits from the integer inside VarInt at the provided index.
377377
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
378378
// In case negative index is provided, ErrorIndexIsNegative is returned.
379379
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
380380
// In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.
381-
// In case the subtraction result underflows the number, the regular unsigned semantic applies and
381+
// In case the subtraction result underflows the integer, the regular unsigned semantic applies and
382382
// extra ErrorSubtractionUnderflow warning is returned.
383383
func (vint VarInt) Sub(i int, bits Bits) error {
384384
// Check explicitly for invalid number.
@@ -460,12 +460,12 @@ func (vint VarInt) Sub(i int, bits Bits) error {
460460
return nil
461461
}
462462

463-
// Mul multiplies the provided bits with the number inside VarInt at the provided index.
463+
// Mul multiplies the provided bits with the integer inside VarInt at the provided index.
464464
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
465465
// In case negative index is provided, ErrorIndexIsNegative is returned.
466466
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
467467
// In case the provided bits has different bit len, ErrorUnequalBitLengthCardinality is returned.
468-
// In case the multiplication result overflows the bit len, the number is trucated and
468+
// In case the multiplication result overflows the bit len, the integer is trucated and
469469
// extra ErrorMultiplicationOverflow warning is returned.
470470
func (vint VarInt) Mul(i int, bits Bits) error {
471471
// Check explicitly for invalid number.
@@ -550,15 +550,15 @@ func (vint VarInt) Mul(i int, bits Bits) error {
550550
}
551551
}
552552
// After multiplication is done set bits var
553-
// back to i-th number and check for any error.
553+
// back to i-th integer and check for any error.
554554
_ = vint.Set(i, bvar)
555555
if overflow {
556556
return ErrorMultiplicationOverflow
557557
}
558558
return nil
559559
}
560560

561-
// Div divides the provided bits with the number inside VarInt at the provided index.
561+
// Div divides the provided bits with the integer inside VarInt at the provided index.
562562
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
563563
// In case negative index is provided, ErrorIndexIsNegative is returned.
564564
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -622,7 +622,7 @@ func (vint VarInt) Div(i int, bits Bits) error {
622622
return nil
623623
}
624624

625-
// Mod applies modulo operation to the provided bits and the number inside VarInt at the provided index.
625+
// Mod applies modulo operation to the provided bits and the integer inside VarInt at the provided index.
626626
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
627627
// In case negative index is provided, ErrorIndexIsNegative is returned.
628628
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -637,12 +637,12 @@ func (vint VarInt) Mod(i int, bits Bits) error {
637637
return err
638638
}
639639
// Get tmp bits variable with reminder inise,
640-
// don't clear the previous and swap it with vint number.
640+
// don't clear the previous and swap it with vint.
641641
_ = vint.GetSet(i, bvar(vint, false))
642642
return nil
643643
}
644644

645-
// Not applies bitwise negation ^ operation to the number inside VarInt at the provided index.
645+
// Not applies bitwise negation ^ operation to the integer inside VarInt at the provided index.
646646
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
647647
// In case negative index is provided, ErrorIndexIsNegative is returned.
648648
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -700,7 +700,7 @@ func (vint VarInt) Not(i int) error {
700700
return nil
701701
}
702702

703-
// And applies bitwise and & operation to the provided bits and the number inside VarInt at the provided index.
703+
// And applies bitwise and & operation to the provided bits and the integer inside VarInt at the provided index.
704704
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
705705
// In case negative index is provided, ErrorIndexIsNegative is returned.
706706
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -764,7 +764,7 @@ func (vint VarInt) And(i int, bits Bits) error {
764764
return nil
765765
}
766766

767-
// Or applies bitwise and | operation to the provided bits and the number inside VarInt at the provided index.
767+
// Or applies bitwise and | operation to the provided bits and the integer inside VarInt at the provided index.
768768
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
769769
// In case negative index is provided, ErrorIndexIsNegative is returned.
770770
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -828,7 +828,7 @@ func (vint VarInt) Or(i int, bits Bits) error {
828828
return nil
829829
}
830830

831-
// Xor applies bitwise and ^ operation to the provided bits and the number inside VarInt at the provided index.
831+
// Xor applies bitwise and ^ operation to the provided bits and the integer inside VarInt at the provided index.
832832
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
833833
// In case negative index is provided, ErrorIndexIsNegative is returned.
834834
// In case the provided index is greater than len of VarInt, ErrorIndexIsOutOfRange is returned.
@@ -892,7 +892,7 @@ func (vint VarInt) Xor(i int, bits Bits) error {
892892
return nil
893893
}
894894

895-
// Rsh applies right shift >> operation to the number inside VarInt at the provided index.
895+
// Rsh applies right shift >> operation to the integer inside VarInt at the provided index.
896896
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
897897
// In case negative shift is provided, ErrorShiftIsNegative is returned.
898898
// In case negative index is provided, ErrorIndexIsNegative is returned.
@@ -976,7 +976,7 @@ loop:
976976
return nil
977977
}
978978

979-
// Lsh applies left shift << operation to the number inside VarInt at the provided index.
979+
// Lsh applies left shift << operation to the integer inside VarInt at the provided index.
980980
// In case the operation is used on invalid nil VarInt, ErrorVarIntIsInvalid is returned.
981981
// In case negative shift is provided, ErrorShiftIsNegative is returned.
982982
// In case negative index is provided, ErrorIndexIsNegative is returned.

varint_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ func FuzzVarIntLsh(f *testing.F) {
511511

512512
func BenchmarkVarIntOperations(b *testing.B) {
513513
bench("Benchmark Arithmetic", b, func(b *testing.B) {
514-
bench("Tiny Numbers", b, func(b *testing.B) {
514+
bench("Tiny Integers", b, func(b *testing.B) {
515515
const len, blen = 100000000, 4
516516
bench("VarInt Operations", b, func(b *testing.B) {
517517
vint, _ := NewVarInt(blen, len)
@@ -543,7 +543,7 @@ func BenchmarkVarIntOperations(b *testing.B) {
543543
}
544544
})
545545
})
546-
bench("Word 64 Bits Numbers", b, func(b *testing.B) {
546+
bench("Word 64 Bits Integers", b, func(b *testing.B) {
547547
const len, blen = 10000000, 64
548548
bench("VarInt Operations", b, func(b *testing.B) {
549549
vint, _ := NewVarInt(blen, len)
@@ -575,7 +575,7 @@ func BenchmarkVarIntOperations(b *testing.B) {
575575
}
576576
})
577577
})
578-
bench("Medium Large Numbers", b, func(b *testing.B) {
578+
bench("Medium Large Integers", b, func(b *testing.B) {
579579
const len, blen = 10000000, 100
580580
bench("VarInt Operations", b, func(b *testing.B) {
581581
vint, _ := NewVarInt(blen, len)
@@ -611,7 +611,7 @@ func BenchmarkVarIntOperations(b *testing.B) {
611611
}
612612
})
613613
})
614-
bench("Big Large Numbers", b, func(b *testing.B) {
614+
bench("Big Large Integers", b, func(b *testing.B) {
615615
const len, blen = 100000, 10000
616616
bench("VarInt Operations", b, func(b *testing.B) {
617617
vint, _ := NewVarInt(blen, len)
@@ -649,7 +649,7 @@ func BenchmarkVarIntOperations(b *testing.B) {
649649
})
650650
})
651651
bench("Benchmark Binary Operations", b, func(b *testing.B) {
652-
bench("Tiny Numbers", b, func(b *testing.B) {
652+
bench("Tiny Integers", b, func(b *testing.B) {
653653
const len, blen = 100000000, 4
654654
bench("VarInt Operations", b, func(b *testing.B) {
655655
vint, _ := NewVarInt(blen, len)
@@ -677,7 +677,7 @@ func BenchmarkVarIntOperations(b *testing.B) {
677677
}
678678
})
679679
})
680-
bench("Word 64 Bits Numbers", b, func(b *testing.B) {
680+
bench("Word 64 Bits Integers", b, func(b *testing.B) {
681681
const len, blen = 10000000, 64
682682
bench("VarInt Operations", b, func(b *testing.B) {
683683
vint, _ := NewVarInt(blen, len)
@@ -705,7 +705,7 @@ func BenchmarkVarIntOperations(b *testing.B) {
705705
}
706706
})
707707
})
708-
bench("Medium Large Numbers", b, func(b *testing.B) {
708+
bench("Medium Large Integers", b, func(b *testing.B) {
709709
const len, blen = 10000000, 100
710710
bench("VarInt Operations", b, func(b *testing.B) {
711711
vint, _ := NewVarInt(blen, len)
@@ -737,7 +737,7 @@ func BenchmarkVarIntOperations(b *testing.B) {
737737
}
738738
})
739739
})
740-
bench("Big Large Numbers", b, func(b *testing.B) {
740+
bench("Big Large Integers", b, func(b *testing.B) {
741741
const len, blen = 100000, 10000
742742
bench("VarInt Operations", b, func(b *testing.B) {
743743
vint, _ := NewVarInt(blen, len)

0 commit comments

Comments
 (0)