erigon-pulse/common/tuples.go
Evgeny Danilenko 992e34745c
Replace red black trees (#184)
* use map instead of rb tree

* GetModifiedAccounts

* introduce tuples

* linters

* linters

* init puts

* init maps

* remove GetSortedKeys

* fix string bucket case

* use append in tuples

* fix tuples

* fix tests

* all tests are green

* fmt

* fmt

* rename tuple to tuples
2019-11-21 18:38:00 +00:00

51 lines
939 B
Go

package common
import (
"bytes"
"errors"
)
// Tuples, eg [(index, bucket, key, value)]
type Tuples struct {
Values [][]byte
SortBy int
Length int
arity int
}
func NewTuples(size, arity, sortBy int) *Tuples {
return &Tuples{
Values: make([][]byte, 0, size*arity),
Length: 0,
SortBy: sortBy,
arity: arity,
}
}
func (t *Tuples) Append(values ...[]byte) error {
if len(values) != t.arity {
return errors.New("got an incorrect number of values")
}
t.Length++
for _, value := range values {
t.Values = append(t.Values, value)
}
return nil
}
func (t Tuples) Len() int {
return t.Length
}
func (t Tuples) Less(i, j int) bool {
return bytes.Compare(t.Values[i*t.arity+t.SortBy], t.Values[j*t.arity+t.SortBy]) == -1
}
func (t Tuples) Swap(i, j int) {
for index := 0; index < t.arity; index++ {
t.Values[i*t.arity+index], t.Values[j*t.arity+index] = t.Values[j*t.arity+index], t.Values[i*t.arity+index]
}
}