Skip to content

Commit

Permalink
feat(collection): update collections
Browse files Browse the repository at this point in the history
  • Loading branch information
leaxoy committed Jan 3, 2024
1 parent 5cd5113 commit 8ae7c71
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
9 changes: 5 additions & 4 deletions collections/ordered/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type MapEntry[K, V any] struct{ inner tuple.Pair[K, V] }

// MapEntryOf creates a new MapEntry.
func MapEntryOf[K, V any](key K, value V) MapEntry[K, V] {
return MapEntry[K, V]{inner: tuple.NewPair(key, value)}
return MapEntry[K, V]{inner: tuple.MakePair(key, value)}
}

// Key returns the key of the MapEntry.
Expand Down Expand Up @@ -202,9 +202,10 @@ type KeyItem[K any, V any] struct {
m *Map[K, V]
}

func (k *KeyItem[K, V]) Key() K { return k.key }
func (k *KeyItem[K, V]) Set(v V) { k.m.Insert(k.key, v) }
func (k *KeyItem[K, V]) Remove() { k.m.Remove(k.key) }
func (k *KeyItem[K, V]) Key() K { return k.key }
func (k *KeyItem[K, V]) Value() V { return k.m.Get(k.key).ValueOrZero() }
func (k *KeyItem[K, V]) Set(v V) { k.m.Insert(k.key, v) }
func (k *KeyItem[K, V]) Remove() { k.m.Remove(k.key) }

type ValueItem[K any, V any] struct {
val V
Expand Down
43 changes: 40 additions & 3 deletions collections/queue/slice.go → collections/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@ import (
"github.com/go-board/std/optional"
)

// ArrayQueue is a double ended queue backed by array.
type ArrayQueue[E any] struct{ inner []E }

func (q *ArrayQueue[E]) Iter() iter.Seq[E] {
// New create an empty Queue.
func New[E any]() *ArrayQueue[E] {
return &ArrayQueue[E]{}
}

// FromSlice creates an [ArrayQueue] from slice.
func FromSlice[E any](elems ...E) *ArrayQueue[E] {
return &ArrayQueue[E]{inner: elems}
}

// FromIter creates an [ArrayQueue] from [iter.Seq].
func FromIter[E any](it iter.Seq[E]) *ArrayQueue[E] {
q := new(ArrayQueue[E])
q.PushBackIter(it)
return q
}

// Forward creates an [iter.Seq] in forward order.
func (q *ArrayQueue[E]) Forward() iter.Seq[E] {
return func(yield func(E) bool) {
for _, x := range q.inner {
if !yield(x) {
Expand All @@ -17,18 +36,36 @@ func (q *ArrayQueue[E]) Iter() iter.Seq[E] {
}
}

func (q *ArrayQueue[E]) Size() int {
return len(q.inner)
// Backward creates an [iter.Seq] in backward order.
func (q *ArrayQueue[E]) Backward() iter.Seq[E] {
return func(yield func(E) bool) {
for i := len(q.inner) - 1; i >= 0; i-- {
if !yield(q.inner[i]) {
break
}
}
}
}

// Size returns size of [ArrayQueue].
func (q *ArrayQueue[E]) Size() int { return len(q.inner) }

func (q *ArrayQueue[E]) PushFront(element E) {
q.inner = append([]E{element}, q.inner...)
}

func (q *ArrayQueue[E]) PushFrontIter(it iter.Seq[E]) {
iter.ForEach(it, q.PushFront)
}

func (q *ArrayQueue[E]) PushBack(element E) {
q.inner = append(q.inner, element)
}

func (q *ArrayQueue[E]) PushBackIter(it iter.Seq[E]) {
iter.ForEach(it, q.PushBack)
}

func (q *ArrayQueue[E]) PopFront() optional.Optional[E] {
if len(q.inner) == 0 {
return optional.None[E]()
Expand Down
13 changes: 13 additions & 0 deletions collections/queue/queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package queue_test

import (
"testing"

qt "github.com/frankban/quicktest"
"github.com/go-board/std/collections/queue"
)

func TestArrayQueue_Size(t *testing.T) {
q := queue.FromSlice(1, 2)
qt.Assert(t, q.Size(), qt.Equals, 2)
}

0 comments on commit 8ae7c71

Please sign in to comment.