initial pointer list

This commit is contained in:
Annika Hannig 2022-11-15 11:07:43 +01:00
parent 8c21b29bfd
commit 9622f5745b
2 changed files with 60 additions and 0 deletions

View 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{}
}

View File

@ -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))
}