initial pointer list
This commit is contained in:
parent
8c21b29bfd
commit
9622f5745b
41
pkg/store/pools/int_list_list.go
Normal file
41
pkg/store/pools/int_list_list.go
Normal file
@ -0,0 +1,41 @@
|
||||
package pools
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A IntListList pool can be used to deduplicate
|
||||
// lists of lists of integers. Like a list of large
|
||||
// BGP communities.
|
||||
//
|
||||
// A Tree datastructure is used.
|
||||
type IntListList struct {
|
||||
lists *IntTree
|
||||
ptrs *IntTree
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// NewIntListList creates a new int list pool
|
||||
func NewIntListList() *IntListList {
|
||||
return &IntListList{
|
||||
lists: NewIntTree(),
|
||||
ptrs: NewIntTree(),
|
||||
}
|
||||
}
|
||||
|
||||
// Acquire int list from pool
|
||||
func (p *IntListList) Acquire(list [][]int) [][]int {
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
|
||||
// Convert list of list to list of ptrs
|
||||
ptrList := make([]int, len(list))
|
||||
for i, v := range list {
|
||||
ptr := p.ptrs.Acquire(v)
|
||||
ptrV := int(uintptr(reflect.ValueOf(ptr).UnsafePointer()))
|
||||
ptrList[i] = ptrV
|
||||
}
|
||||
|
||||
return []int{}
|
||||
}
|
@ -2,7 +2,9 @@ package pools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func TestAcquireIntList(t *testing.T) {
|
||||
@ -24,3 +26,20 @@ func TestAcquireIntList(t *testing.T) {
|
||||
}
|
||||
t.Log(fmt.Sprintf("Ptr: %p %p => %p %p", a, b, r1, r2))
|
||||
}
|
||||
|
||||
func TestPtrIntList(t *testing.T) {
|
||||
a := []int{23, 42, 1337, 65535, 1}
|
||||
b := []int{23, 42, 1337, 65535, 1}
|
||||
|
||||
pa := unsafe.Pointer(&a[0])
|
||||
pb := unsafe.Pointer(&b[0])
|
||||
|
||||
t.Log(fmt.Sprintf("Ptr: %p %p", a, b))
|
||||
t.Log(fmt.Sprintf("P: %p %p", pa, pb))
|
||||
|
||||
ra := reflect.ValueOf(a).UnsafePointer()
|
||||
rb := reflect.ValueOf(b).UnsafePointer()
|
||||
t.Log(fmt.Sprintf("P: %x %x %v", ra, rb, ra == rb))
|
||||
|
||||
t.Log(fmt.Sprintf("P: %x %x %v", int(uintptr(ra)), rb, ra == rb))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user