Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzotinfena committed Apr 23, 2024
1 parent 0951b20 commit a1b7ff5
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 6 deletions.
130 changes: 130 additions & 0 deletions collections/arraydeque.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package collections

type ArrayDeque[T any] struct {
v []T
start int
end int
}

func NewArrayDeque[T any]() *ArrayDeque[T] {
return &ArrayDeque[T]{
[]T{}, -1, -1,
}
}

func (arr *ArrayDeque[T]) doubleSize() {
v1 := make([]T, len(arr.v)*2)
j := 0
for i := arr.start; i != arr.end; i = (i + 1) % len(arr.v) {
v1[j] = arr.v[i]
j++
}
v1[j] = arr.v[arr.end]
arr.start = 0
arr.end = len(arr.v) - 1
arr.v = v1
}

func (arr *ArrayDeque[T]) Len() int {
if arr.start <= arr.end {
return arr.end - arr.start + 1
} else {
return arr.end + len(arr.v) - arr.start + 1
}
}

func (arr *ArrayDeque[T]) Get(index int) T {
return arr.v[(arr.start+index)%len(arr.v)]
}

func (arr *ArrayDeque[T]) Set(index int, element T) {
arr.v[(arr.start+index)%len(arr.v)] = element
}

func (arr *ArrayDeque[T]) InsertFirst(element T) {
if len(arr.v) == 0 {
arr.v = []T{element}
arr.start = 0
arr.end = 0
return
}
if (arr.end+1)%len(arr.v) == arr.start {
arr.doubleSize()
}
if arr.start == 0 {
arr.start = len(arr.v) - 1
} else {
arr.start = (arr.start - 1) % len(arr.v)
}
arr.v[arr.end] = element
}

func (arr *ArrayDeque[T]) InsertLast(element T) {
if len(arr.v) == 0 {
arr.v = []T{element}
arr.start = 0
arr.end = 0
return
}
if (arr.end+1)%len(arr.v) == arr.start {
arr.doubleSize()
}

arr.end = (arr.end + 1) % len(arr.v)
arr.v[arr.end] = element
}

func (arr *ArrayDeque[T]) shrink() {
if 4*arr.Len() <= len(arr.v) {
nextV := make([]T, len(arr.v))
j := 0
for i := arr.start; i <= arr.end; i = (i + 1) % len(arr.v) {
nextV[j] = arr.v[i]
j++
}
arr.v = nextV
}
}

func (arr *ArrayDeque[T]) RemoveFirst() {
if arr.end == 0 {
arr.end = len(arr.v) - 1
} else {
arr.end--
}
arr.shrink()
}

func (arr *ArrayDeque[T]) RemoveLast() {
if arr.start == len(arr.v)-1 {
arr.start = 0
} else {
arr.start++
}
arr.shrink()
}

func (arr *ArrayDeque[T]) Slice() []T {
nextV := make([]T, len(arr.v))
j := 0
for i := arr.start; i <= arr.end; i = (i + 1) % len(arr.v) {
nextV[j] = arr.v[i]
j++
}
return nextV
}

func (arr *ArrayDeque[T]) Insert(element T, index int) {
if index == 0 {
arr.InsertLast(element)
return
}
if (arr.end+1)%len(arr.v) == arr.start {
arr.doubleSize()
}
i := (arr.start + index) % len(arr.v)
for ; i <= arr.end; i = (i + 1) % len(arr.v) {
element, arr.v[i] = arr.v[i], element
}
arr.v[i] = element
}
29 changes: 29 additions & 0 deletions collections/arraydeque_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package collections_test

import (
"testing"

"github.com/lorenzotinfena/goji/collections"
)

func TestArrayDeque(t *testing.T) {
deque := collections.NewArrayDeque[int]()
deque.InsertLast(1)
deque.InsertLast(1)
deque.InsertFirst(1)
deque.InsertFirst(1)
deque.RemoveFirst()
deque.RemoveFirst()
deque.RemoveFirst()
deque.RemoveFirst()
deque.InsertFirst(1)
deque.InsertLast(1)
deque.InsertFirst(1)
deque.InsertLast(1)
deque.InsertLast(1)
deque.RemoveLast()
deque.RemoveLast()
deque.RemoveLast()
deque.RemoveLast()
deque.RemoveLast()
}
12 changes: 6 additions & 6 deletions collections/trie/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func (t *HAMT[K, V]) Set(key K, value V) {
break
}
data := (element << bitsPrefixSum[i]) >> (64 - 6)
if n.bitmap&(1<<data) != 0 {
n = n.next[bits.OnesCount64(uint64(n.bitmap<<(64-data)))]
if n.bitmap&((1<<63)>>data) != 0 {
n = n.next[bits.OnesCount64(uint64(n.bitmap>>(64-data)))]
} else {
keyValues := ll.NewSinglyLinkedList[coll.Pair[K, V]](func(a, b coll.Pair[K, V]) bool { return a.First == b.First })
keyValues.InsertLast(coll.MakePair(key, value))
next := &node[K, V]{bitmap: 0, hash: element, keyValues: *keyValues, next: make([]*node[K, V], 0)}
pos := bits.OnesCount64(uint64(n.bitmap << (63 - data)))
pos := bits.OnesCount64(uint64(n.bitmap >> (64 - data)))
n.next = slices.Insert(n.next, pos, next)
n.bitmap |= (1 << data)
n.bitmap |= ((1 << 63) >> data)
t.length++
return
}
Expand All @@ -86,8 +86,8 @@ func (t *HAMT[K, V]) Get(key K) (V, bool) {
break
}
data := (element << bitsPrefixSum[i]) >> (64 - 6)
if n.bitmap&(1<<data) != 0 {
n = n.next[bits.OnesCount(n.bitmap<<(64-data))]
if n.bitmap&((1<<63)>>data) != 0 {
n = n.next[bits.OnesCount(n.bitmap>>(64-data))]
} else {
var foo V
return foo, false
Expand Down

0 comments on commit a1b7ff5

Please sign in to comment.