Skip to content

Commit

Permalink
feat!(update): refactor whole module
Browse files Browse the repository at this point in the history
  • Loading branch information
leaxoy committed Mar 21, 2024
1 parent 0f99d12 commit 5aa5a68
Show file tree
Hide file tree
Showing 40 changed files with 1,560 additions and 882 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ go get -u github.com/go-board/std
```

## Packages Hierarchy
- [algorithm](https://github.com/go-board/std/blob/master/algorithm) common used algorithms
- [dp](https://github.com/go-board/std/blob/master/algorithm/dp) dynamic programming
- [clone](https://github.com/go-board/std/blob/master/clone) clone a object
- [codec](https://github.com/go-board/std/blob/master/codec) encode and decode
- [collections](https://github.com/go-board/std/blob/master/collections) common used collections
- [btree](https://github.com/go-board/std/blob/master/collections/btree) btree based map & set
- [linkedlist](https://github.com/go-board/std/blob/master/collections/linkedlist) linked list
- [queue](https://github.com/go-board/std/blob/master/collections/queue) double ended queue
- [cond](https://github.com/go-board/std/blob/master/cond) conditional operator
- [core](https://github.com/go-board/std/blob/master/core) core types & constraints
- [constraints](https://github.com/go-board/std/blob/master/constraints) core constraints
- [fp](https://github.com/go-board/std/blob/master/fp) functional programing
- [hash](https://github.com/go-board/std/blob/master/hash) hash a object
- [iterator](https://github.com/go-board/std/blob/master/iterator) iterators
- [adapters](https://github.com/go-board/std/blob/master/iterator/adapters) adapter to create iterators & streams
- [stream](https://github.com/go-board/std/blob/master/iterator/stream) stream processing
- [iter](https://github.com/go-board/std/blob/master/iter) iterators
- [collector](https://github.com/go-board/std/blob/master/iterator/collector) consume iter and collect to another type
- [source](https://github.com/go-board/std/blob/master/iterator/source) adapter to create iterators & streams
- [lazy](https://github.com/go-board/std/blob/master/lazy) lazy evaluation & variables
- [optional](https://github.com/go-board/std/blob/master/optional) optional values
- [ptr](https://github.com/go-board/std/blob/master/ptr) convenient pointer operator
Expand Down
19 changes: 0 additions & 19 deletions algorithm/dp/dp.go

This file was deleted.

76 changes: 0 additions & 76 deletions algorithm/dp/dp_test.go

This file was deleted.

59 changes: 59 additions & 0 deletions cmp/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import (

type Order int // -1, 0, 1

func MakeOrder(o int) Order {
switch {
case o < 0:
return OrderLess
case o > 0:
return OrderGreater
default:
return OrderEqual
}
}

func (o Order) IsEq() bool { return o == 0 }
func (o Order) IsNe() bool { return o != 0 }
func (o Order) IsLt() bool { return o < 0 }
Expand Down Expand Up @@ -68,3 +79,51 @@ type Ord[A any] interface {
PartialOrd[A]
Cmp(A) Order
}

type Comparator[A any] interface {
Cmp(lhs A, rhs A) int
Lt(lhs A, rhs A) bool
Le(lhs A, rhs A) bool
Gt(lhs A, rhs A) bool
Ge(lhs A, rhs A) bool
Eq(lhs A, rhs A) bool
Ne(lhs A, rhs A) bool
}

type ComparatorFunc[A any] func(A, A) int

func (f ComparatorFunc[A]) Cmp(lhs A, rhs A) int {
return f(lhs, rhs)
}

func (f ComparatorFunc[A]) Lt(lhs A, rhs A) bool {
return f.Cmp(lhs, rhs) < 0
}

func (f ComparatorFunc[A]) Le(lhs A, rhs A) bool {
return f.Cmp(lhs, rhs) <= 0
}

func (f ComparatorFunc[A]) Gt(lhs A, rhs A) bool {
return f.Cmp(lhs, rhs) > 0
}

func (f ComparatorFunc[A]) Ge(lhs A, rhs A) bool {
return f.Cmp(lhs, rhs) >= 0
}

func (f ComparatorFunc[A]) Eq(lhs A, rhs A) bool {
return f.Cmp(lhs, rhs) == 0
}

func (f ComparatorFunc[A]) Ne(lhs A, rhs A) bool {
return f.Cmp(lhs, rhs) != 0
}

func MakeComparator[A Ordered]() Comparator[A] {
return ComparatorFunc[A](Compare[A])
}

func MakeComparatorFunc[A any](f func(A, A) int) Comparator[A] {
return ComparatorFunc[A](f)
}
4 changes: 2 additions & 2 deletions cmp/cmp_1_20.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package cmp

import (
"github.com/go-board/std/core"
"github.com/go-board/std/constraints"
)

// isNaN reports whether x is a NaN without requiring the math package.
Expand All @@ -12,7 +12,7 @@ func isNaN[T Ordered](x T) bool {
return x != x
}

type Ordered = core.Ordered
type Ordered = constraints.Ordered

func Compare[T Ordered](lhs T, rhs T) int {
xNaN := isNaN(lhs)
Expand Down
24 changes: 20 additions & 4 deletions cmp/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ func MaxBy[A any, F ~func(A, A) Order](cmp F, lhs, rhs A) A {
return ternary(cmp(lhs, rhs).IsGt(), lhs, rhs)
}

func MaxFunc[A any](cmp func(A, A) int, lhs, rhs A) A {
return ternary(cmp(lhs, rhs) > 0, lhs, rhs)
}

// MaxByKey calculates the maximum value of two values by a key function.
func MaxByKey[A any, F ~func(A) K, K Ordered](key F, lhs, rhs A) A {
keyLhs, keyRhs := key(lhs), key(rhs)
return ternary(keyLhs > keyRhs, lhs, rhs)
return ternary(key(lhs) > key(rhs), lhs, rhs)
}

// MaxOrdered calculates the maximum value of two ordered values.
Expand All @@ -28,13 +31,26 @@ func MinBy[A any, F ~func(A, A) Order](cmp F, lhs, rhs A) A {
return ternary(cmp(lhs, rhs).IsLt(), lhs, rhs)
}

func MinFunc[A any](cmp func(A, A) int, lhs, rhs A) A {
return ternary(cmp(lhs, rhs) < 0, lhs, rhs)
}

// MinByKey calculates the minimum value of two values by a key function.
func MinByKey[A any, F ~func(A) K, K Ordered](key F, lhs, rhs A) A {
keyLhs, keyRhs := key(lhs), key(rhs)
return ternary(keyLhs < keyRhs, lhs, rhs)
return ternary(key(lhs) < key(rhs), lhs, rhs)
}

// MinOrdered calculates the minimum value of two ordered values.
func MinOrdered[A Ordered](lhs, rhs A) A {
return ternary(lhs < rhs, lhs, rhs)
}

func Or[E comparable](elems ...E) E {
var empty E
for _, x := range elems {
if empty != x {
return x
}
}
return empty
}
13 changes: 7 additions & 6 deletions collections/ordered/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ func invert[T any](cmp func(T, T) int) func(T, T) int {
// MapEntry is a tuple of key and value.
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] {
// MakeMapEntry creates a new MapEntry.
func MakeMapEntry[K, V any](key K, value V) MapEntry[K, V] {
return MapEntry[K, V]{inner: tuple.MakePair(key, value)}
}

// Key returns the key of the MapEntry.
func (self MapEntry[K, V]) Key() K { return self.inner.First }
func (self MapEntry[K, V]) Key() K { return self.inner.First() }

// Value returns the value of the MapEntry.
func (self MapEntry[K, V]) Value() V { return self.inner.Second }
func (self MapEntry[K, V]) Value() V { return self.inner.Second() }

// Map is an ordered map based on a B-Tree.
type Map[K, V any] struct {
Expand All @@ -49,12 +49,13 @@ func NewOrderedMap[K cmp.Ordered, V any]() *Map[K, V] {
}

func (self *Map[K, V]) keyEntry(k K) MapEntry[K, V] {
return MapEntry[K, V]{inner: tuple.Pair[K, V]{First: k}}
var v V
return MapEntry[K, V]{inner: tuple.MakePair(k, v)}
}

// Insert inserts a new MapEntry.
func (self *Map[K, V]) Insert(key K, value V) optional.Optional[V] {
e, ok := self.inner.Set(MapEntryOf(key, value))
e, ok := self.inner.Set(MakeMapEntry(key, value))
return optional.Map(optional.FromPair(e, ok), MapEntry[K, V].Value)
}

Expand Down
5 changes: 1 addition & 4 deletions core/constraint.go → constraints/constraints.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package constraints

type Primitive interface{ Numeric | ~string | ~bool }

Expand All @@ -21,6 +21,3 @@ type Integer interface{ Signed | Unsigned }
type Numeric interface{ Integer | Float | Complex }

type Ordered interface{ Integer | Float | ~string }

// Deprecated: use [Numeric] instead
type Number = Numeric
61 changes: 0 additions & 61 deletions core/core.go

This file was deleted.

23 changes: 0 additions & 23 deletions core/core_test.go

This file was deleted.

Loading

0 comments on commit 5aa5a68

Please sign in to comment.