Skip to content

Commit

Permalink
feat: refactor iter module to match official proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
leaxoy committed Dec 2, 2023
1 parent f61c5b8 commit acc5904
Show file tree
Hide file tree
Showing 24 changed files with 864 additions and 1,491 deletions.
4 changes: 2 additions & 2 deletions algorithm/dp/dp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dp

func LcsBy[T any](left []T, right []T, eq func(T, T) bool) []T {
func LcsBy[T any, S ~[]T](left S, right S, eq func(T, T) bool) S {
if len(left) == 0 || len(right) == 0 {
return nil
}
Expand All @@ -14,6 +14,6 @@ func LcsBy[T any](left []T, right []T, eq func(T, T) bool) []T {
return lcs2
}

func Lcs[T comparable](left []T, right []T) []T {
func Lcs[T comparable, S ~[]T](left S, right S) S {
return LcsBy(left, right, func(a, b T) bool { return a == b })
}
67 changes: 0 additions & 67 deletions collections/linked/list.go

This file was deleted.

69 changes: 17 additions & 52 deletions collections/ordered/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ type Map[K, V any] struct {

var dummyMap *Map[any, any]

var (
_ clone.Cloneable[*Map[any, any]] = dummyMap
_ iter.Iterable[MapEntry[any, any]] = dummyMap
)
var _ clone.Cloneable[*Map[any, any]] = dummyMap

// NewMap creates a new Map.
func NewMap[K, V any](cmp func(K, K) int) *Map[K, V] {
Expand All @@ -61,6 +58,12 @@ func (self *Map[K, V]) Insert(key K, value V) optional.Optional[V] {
return optional.Map(optional.FromPair(e, ok), MapEntry[K, V].Value)
}

func (self *Map[K, V]) InsertIter(it iter.Seq[MapEntry[K, V]]) {
iter.ForEach(it, func(me MapEntry[K, V]) {
self.insertEntry(me)
})
}

func (self *Map[K, V]) insertEntry(entry MapEntry[K, V]) {
self.inner.Set(entry)
}
Expand Down Expand Up @@ -88,7 +91,7 @@ func (self *Map[K, V]) Clone() *Map[K, V] {
// Reverse returns a reversed copy of the Map.
func (self *Map[K, V]) Reverse() *Map[K, V] {
newTree := NewMap[K, V](invert(self.cmp))
iter.ForEach(self.Iter(), newTree.insertEntry)
iter.ForEach(self.EntryIter(), newTree.insertEntry)
return newTree
}

Expand Down Expand Up @@ -123,31 +126,19 @@ func (self *Map[K, V]) PopLast() optional.Optional[MapEntry[K, V]] {
return optional.FromPair(self.inner.PopMax())
}

func (self *Map[K, V]) Range(start, end K) *Map[K, V] {
subMap := NewMap[K, V](self.cmp)
insert := func(m *Map[K, V], v MapEntry[K, V]) bool {
m.insertEntry(v)
return true
}
self.inner.Ascend(self.keyEntry(start), func(item MapEntry[K, V]) bool {
return self.inner.Less(item, self.keyEntry(end)) && insert(subMap, item)
})
return subMap
}

// IterKey returns an iter over the keys in the map.
func (self *Map[K, V]) IterKey() iter.Iter[K] {
return &mapKeyIter[K, V]{inner: &mapIter[K, V]{inner: self.inner.Iter()}}
// KeyIter returns an iterator over the keys in the map.
func (self *Map[K, V]) KeyIter() iter.Seq[K] {
return iter.Map(self.inner.Scan, func(e MapEntry[K, V]) K { return e.Key() })
}

// IterValue returns an iter over the values in the map.
func (self *Map[K, V]) IterValue() iter.Iter[V] {
return &mapValueIter[K, V]{inner: &mapIter[K, V]{inner: self.inner.Iter()}}
// ValueIter returns an iterator over the keys in the map.
func (self *Map[K, V]) ValueIter() iter.Seq[V] {
return iter.Map(self.inner.Scan, func(e MapEntry[K, V]) V { return e.Value() })
}

// Iter returns an iter over the entries in the map.
func (self *Map[K, V]) Iter() iter.Iter[MapEntry[K, V]] {
return &mapIter[K, V]{inner: self.inner.Iter()}
// EntryIter returns an iterator over the keys in the map.
func (self *Map[K, V]) EntryIter() iter.Seq[MapEntry[K, V]] {
return self.inner.Scan
}

// Len returns the number of entries in the map.
Expand All @@ -161,29 +152,3 @@ func (self *Map[K, V]) Merge(o *Map[K, V]) {
return true
})
}

type mapIter[TKey, TValue any] struct {
inner btree.IterG[MapEntry[TKey, TValue]]
}

var _ iter.Iter[MapEntry[any, any]] = (*mapIter[any, any])(nil)

func (self *mapIter[TKey, TValue]) Next() optional.Optional[MapEntry[TKey, TValue]] {
if self.inner.Next() {
return optional.Some(self.inner.Item())
}
self.inner.Release()
return optional.None[MapEntry[TKey, TValue]]()
}

type mapKeyIter[K, V any] struct{ inner *mapIter[K, V] }

func (self *mapKeyIter[K, V]) Next() optional.Optional[K] {
return optional.Map(self.inner.Next(), MapEntry[K, V].Key)
}

type mapValueIter[K, V any] struct{ inner *mapIter[K, V] }

func (self *mapValueIter[K, V]) Next() optional.Optional[V] {
return optional.Map(self.inner.Next(), MapEntry[K, V].Value)
}
58 changes: 21 additions & 37 deletions collections/ordered/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ type Set[T any] struct {
inner *btree.BTreeG[T]
}

var (
_ clone.Cloneable[*Set[any]] = (*Set[any])(nil)
_ iter.Iterable[any] = (*Set[any])(nil)
)
var _ clone.Cloneable[*Set[any]] = (*Set[any])(nil)

// NewSet creates a new Set.
func NewSet[T any](cmp func(T, T) int) *Set[T] {
Expand All @@ -40,21 +37,29 @@ func (self *Set[T]) InsertMany(elements ...T) {
}
}

func (self *Set[T]) InsertIter(it iter.Seq[T]) {
iter.ForEach(it, func(t T) { self.inner.Set(t) })
}

// Remove removes elements from the set.
func (self *Set[T]) Remove(elements ...T) {
for _, element := range elements {
self.inner.Delete(element)
}
}

func (self *Set[T]) RemoveIter(it iter.Seq[T]) {
iter.ForEach(it, func(t T) { self.inner.Delete(t) })
}

func (self *Set[T]) Clear() {
self.inner.Clear()
}

// Reverse returns a reversed view of the set.
func (self *Set[T]) Reverse() *Set[T] {
newSet := NewSet(invert(self.cmp))
iter.ForEach(self.Iter(), newSet.Insert)
iter.ForEach(self.AscendIter(), newSet.Insert)
return newSet
}

Expand All @@ -64,19 +69,6 @@ func (self *Set[T]) Contains(element T) bool {
return ok
}

// Range returns an iter over the set.
func (self *Set[T]) Range(start, end T) *Set[T] {
newSet := NewSet(self.cmp)
insert := func(s *Set[T], v T) bool {
s.Insert(v)
return true
}
self.inner.Ascend(start, func(item T) bool {
return self.inner.Less(item, end) && insert(newSet, item)
})
return newSet
}

// First returns the first element of the set.
func (self *Set[T]) First() optional.Optional[T] {
return optional.FromPair(self.inner.Min())
Expand Down Expand Up @@ -105,7 +97,7 @@ func (self *Set[T]) Clone() *Set[T] {
// Intersection returns the intersection of the two sets.
func (self *Set[T]) Intersection(o *Set[T]) *Set[T] {
newSet := NewSet(self.cmp)
iter.ForEach(self.Iter(), func(t T) {
iter.ForEach(self.AscendIter(), func(t T) {
if o.Contains(t) {
newSet.Insert(t)
}
Expand All @@ -116,7 +108,7 @@ func (self *Set[T]) Intersection(o *Set[T]) *Set[T] {
// Difference returns the difference of the two sets.
func (self *Set[T]) Difference(o *Set[T]) *Set[T] {
cloned := self.Clone()
iter.ForEach(self.Iter(), func(t T) {
iter.ForEach(self.AscendIter(), func(t T) {
if o.Contains(t) {
cloned.Remove(t)
}
Expand All @@ -127,39 +119,31 @@ func (self *Set[T]) Difference(o *Set[T]) *Set[T] {
// Union returns the union of the two sets.
func (self *Set[T]) Union(o *Set[T]) *Set[T] {
cloned := self.Clone()
iter.ForEach(o.Iter(), cloned.Insert)
iter.ForEach(o.AscendIter(), cloned.Insert)
return cloned
}

// SubsetOf returns true if the set is a subset of the other set.
func (self *Set[T]) SubsetOf(o *Set[T]) bool {
return iter.AllOf(o.Iter(), o.Contains)
return iter.All(o.AscendIter(), o.Contains)
}

// SupersetOf returns true if the set is a superset of the other set.
func (self *Set[T]) SupersetOf(o *Set[T]) bool {
return iter.AllOf(o.Iter(), self.Contains)
return iter.All(o.AscendIter(), self.Contains)
}

// Len returns the number of elements in the set.
func (self *Set[T]) Len() int {
return self.inner.Len()
}

// Iter returns an iter over the set.
func (self *Set[T]) Iter() iter.Iter[T] {
return &setIter[T]{iter: self.inner.Iter()}
// AscendIter returns an iter over the set in ascend order.
func (self *Set[T]) AscendIter() iter.Seq[T] {
return self.inner.Scan
}

type setIter[T any] struct {
iter btree.IterG[T]
}

var _ iter.Iter[any] = (*setIter[any])(nil)

func (self *setIter[T]) Next() optional.Optional[T] {
if self.iter.Next() {
return optional.Some(self.iter.Item())
}
return optional.None[T]()
// DescendIter returns an iter over the set in descend order.
func (self *Set[T]) DescendIter() iter.Seq[T] {
return self.inner.Reverse
}
53 changes: 53 additions & 0 deletions collections/stack/stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package stack

import (
"github.com/go-board/std/iter"
"github.com/go-board/std/slices"
)

type Stack[E any] struct {
elements []E
}

func FromIter[E any](it iter.Seq[E]) *Stack[E] {
return &Stack[E]{elements: slices.Collect(it)}
}

func FromSlice[E any](elems ...E) *Stack[E] {
return &Stack[E]{elements: elems}
}

func (s *Stack[E]) Push(elem E) {
s.elements = append(s.elements, elem)
}

func (s *Stack[E]) Pop() (e E, ok bool) {
if len(s.elements) == 0 {
return
}
e = s.elements[len(s.elements)-1]
s.elements = s.elements[:len(s.elements)-1]
ok = true
return
}

func (s *Stack[E]) Peek() (e E, ok bool) {
if len(s.elements) == 0 {
return
}
e = s.elements[len(s.elements)-1]
ok = true
return
}

func (s *Stack[E]) IsEmpty() bool {
return len(s.elements) == 0
}

func (s *Stack[E]) Size() int {
return len(s.elements)
}

func (s *Stack[E]) Iter() iter.Seq[E] {
return slices.BackwardSeq(s.elements)
}
4 changes: 3 additions & 1 deletion core/core_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package core
package core_test

import (
"reflect"
"testing"

. "github.com/go-board/std/core"
)

func TestSlice(t *testing.T) {
Expand Down
Loading

0 comments on commit acc5904

Please sign in to comment.