Skip to content

Commit

Permalink
Remove cmp.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Oct 3, 2024
1 parent 6a346f6 commit 99f439a
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 184 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ The format is based on [Keep a Changelog], and this project adheres to
- The results of `Clone()`, `Union()` and `Project()` on any set type are now
guaranteed to be non-nil.

### Removed

- **[BC]** Removed `cmp` parameter from `maps.NewOrderedByComparator()` and its
variants. The comparator logic must now be totally encapsulated by the
comparator type alone.

## [0.15.1] - 2024-10-02

### Removed
Expand Down
8 changes: 1 addition & 7 deletions collections/maps/ordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Ordered[K cmp.Ordered, V any] struct {

// NewOrdered returns an [Ordered] containing the given key/value pairs.
func NewOrdered[K cmp.Ordered, V any](pairs ...Pair[K, V]) *Ordered[K, V] {
return orderedFromPairs[K, V, *Ordered[K, V]](pairs)
return orderedFromUnsortedPairs[K, V, *Ordered[K, V]](pairs)
}

// NewOrderedFromSeq returns an [Ordered] containing the key/value pairs yielded
Expand Down Expand Up @@ -116,12 +116,6 @@ func (m *Ordered[K, V]) ReverseValues() iter.Seq[V] {
return orderedReverseValues[K, V](m)
}

func (m *Ordered[K, V]) new(pairs []Pair[K, V]) *Ordered[K, V] {
return &Ordered[K, V]{
pairs: pairs,
}
}

func (m *Ordered[K, V]) ptr() *[]Pair[K, V] {
return &m.pairs
}
Expand Down
57 changes: 11 additions & 46 deletions collections/maps/orderedbycomparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,21 @@ import (
)

// OrderedByComparator is an an ordered map of keys of type K to values of type
// V with ordering defined by a separate comparitor.
// V with ordering defined by a separate comparitor type.
type OrderedByComparator[K, V any, C constraints.Comparator[K]] struct {
Comparator C

pairs []Pair[K, V]
}

// NewOrderedByComparator returns an [OrderedByComparator] containing the
// given key/value pairs.
func NewOrderedByComparator[K, V any, C constraints.Comparator[K]](
cmp C,
pairs ...Pair[K, V],
) *OrderedByComparator[K, V, C] {
n := OrderedByComparator[K, V, C]{
Comparator: cmp,
}

for _, p := range pairs {
n.Set(p.Key, p.Value)
}

return &n
// NewOrderedByComparator returns an [OrderedByComparator] containing the given key/value
// pairs.
func NewOrderedByComparator[K, V any, C constraints.Comparator[K]](pairs ...Pair[K, V]) *OrderedByComparator[K, V, C] {
return orderedFromUnsortedPairs[K, V, *OrderedByComparator[K, V, C]](pairs)
}

// NewOrderedByComparatorFromSeq returns an [OrderedByComparator] containing
// the key/value pairs yielded by the given sequence.
func NewOrderedByComparatorFromSeq[K, V any, C constraints.Comparator[K]](
cmp C,
seq iter.Seq2[K, V],
) *OrderedByComparator[K, V, C] {
n := OrderedByComparator[K, V, C]{
Comparator: cmp,
}

for k, v := range seq {
n.Set(k, v)
}

return &n
// NewOrderedByComparatorFromSeq returns an [OrderedByComparator] containing the key/value
// pairs yielded by the given sequence.
func NewOrderedByComparatorFromSeq[K, V any, C constraints.Comparator[K]](seq iter.Seq2[K, V]) *OrderedByComparator[K, V, C] {
return orderedFromSeq[K, V, *OrderedByComparator[K, V, C]](seq)
}

// Set sets the value associated with the given key.
Expand Down Expand Up @@ -143,22 +119,11 @@ func (m *OrderedByComparator[K, V, C]) ReverseValues() iter.Seq[V] {
return orderedReverseValues[K, V](m)
}

func (m *OrderedByComparator[K, V, C]) new(pairs []Pair[K, V]) *OrderedByComparator[K, V, C] {
var cmp C
if m != nil {
cmp = m.Comparator
}

return &OrderedByComparator[K, V, C]{
Comparator: cmp,
pairs: pairs,
}
}

func (m *OrderedByComparator[K, V, C]) ptr() *[]Pair[K, V] {
return &m.pairs
}

func (m *OrderedByComparator[K, V, C]) cmp(x, y K) int {
return m.Comparator.Compare(x, y)
var c C
return c.Compare(x, y)
}
11 changes: 3 additions & 8 deletions collections/maps/orderedbycomparator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package maps_test

import (
"cmp"
"iter"
"testing"

. "github.com/dogmatiq/enginekit/collections/maps"
Expand All @@ -11,7 +10,7 @@ import (

type reverseStringComparator struct{}

func (c *reverseStringComparator) Compare(x, y string) int {
func (reverseStringComparator) Compare(x, y string) int {
return -cmp.Compare(x, y)
}

Expand All @@ -20,12 +19,8 @@ func TestOrderedByComparator(t *testing.T) {

testOrderedMap(
t,
func(pairs ...Pair[string, int]) *OrderedByComparator[string, int, *reverseStringComparator] {
return NewOrderedByComparator(cmp, pairs...)
},
func(pairs iter.Seq2[string, int]) *OrderedByComparator[string, int, *reverseStringComparator] {
return NewOrderedByComparatorFromSeq(cmp, pairs)
},
NewOrderedByComparator[string, int, reverseStringComparator],
NewOrderedByComparatorFromSeq,
cmp.Compare,
rapid.String(),
)
Expand Down
6 changes: 1 addition & 5 deletions collections/maps/orderedbykey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type OrderedByKey[K constraints.Ordered[K], V any] struct {
// NewOrderedByKey returns an [OrderedByKey] containing the given key/value
// pairs.
func NewOrderedByKey[K constraints.Ordered[K], V any](pairs ...Pair[K, V]) *OrderedByKey[K, V] {
return orderedFromPairs[K, V, *OrderedByKey[K, V]](pairs)
return orderedFromUnsortedPairs[K, V, *OrderedByKey[K, V]](pairs)
}

// NewOrderedByKeyFromSeq returns an [OrderedByKey] containing the key/value
Expand Down Expand Up @@ -119,10 +119,6 @@ func (m *OrderedByKey[K, V]) ReverseValues() iter.Seq[V] {
return orderedReverseValues[K, V](m)
}

func (m *OrderedByKey[K, V]) new(pairs []Pair[K, V]) *OrderedByKey[K, V] {
return &OrderedByKey[K, V]{pairs: pairs}
}

func (m *OrderedByKey[K, V]) ptr() *[]Pair[K, V] {
return &m.pairs
}
Expand Down
22 changes: 13 additions & 9 deletions collections/maps/orderedimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import (
type ordered[K, V, I any] interface {
*I

// new returns a new map with the given key/value pairs, which are already
// in order.
new([]Pair[K, V]) *I

// ptr returns a pointer to the map's key/value pairs.
ptr() *[]Pair[K, V]

// cmp compares two keys.
cmp(K, K) int
}

func orderedFromPairs[K, V any, M ordered[K, V, I], I any](
func orderedFromUnsortedPairs[K, V any, M ordered[K, V, I], I any](
pairs []Pair[K, V],
) M {
var m M = new(I)
Expand All @@ -31,6 +27,14 @@ func orderedFromPairs[K, V any, M ordered[K, V, I], I any](
return m
}

func orderedFromSortedPairs[K, V any, M ordered[K, V, I], I any](
pairs []Pair[K, V],
) M {
var m M = new(I)
*m.ptr() = pairs
return m
}

func orderedFromSeq[K, V any, M ordered[K, V, I], I any](
pairs iter.Seq2[K, V],
) M {
Expand Down Expand Up @@ -164,7 +168,7 @@ func orderedClone[K, V any, M ordered[K, V, I], I any](
pairs = slices.Clone(*m.ptr())
}

return m.new(pairs)
return orderedFromSortedPairs[K, V, M](pairs)
}

func orderedMerge[K, V any, M ordered[K, V, I], I any](
Expand Down Expand Up @@ -214,7 +218,7 @@ func orderedMerge[K, V any, M ordered[K, V, I], I any](
}
}

return x.new(pairs)
return orderedFromSortedPairs[K, V, M](pairs)
}

func orderedSelect[K, V any, M ordered[K, V, I], I any](
Expand All @@ -231,14 +235,14 @@ func orderedSelect[K, V any, M ordered[K, V, I], I any](
}
}

return m.new(pairs)
return orderedFromSortedPairs[K, V, M](pairs)
}

func orderedProject[K, V any, M ordered[K, V, I], I any](
m M,
transform func(K, V) (K, V, bool),
) M {
var out M = m.new(nil)
var out M = new(I)

if m != nil {
for _, pair := range *m.ptr() {
Expand Down
8 changes: 1 addition & 7 deletions collections/sets/ordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Ordered[T cmp.Ordered] struct {

// NewOrdered returns an [Ordered] containing the given members.
func NewOrdered[T cmp.Ordered](members ...T) *Ordered[T] {
return orderedFromMembers[T, *Ordered[T]](members)
return orderedFromUnsortedMembers[T, *Ordered[T]](members)
}

// NewOrderedFromSeq returns an [Ordered] containing the values yielded by the
Expand Down Expand Up @@ -117,12 +117,6 @@ func (s *Ordered[T]) Reverse() iter.Seq[T] {
return orderedReverse[T](s)
}

func (s *Ordered[T]) new(members []T) *Ordered[T] {
return &Ordered[T]{
members: members,
}
}

func (s *Ordered[T]) ptr() *[]T {
return &s.members
}
Expand Down
89 changes: 17 additions & 72 deletions collections/sets/orderedbycomparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,33 @@ import (
)

// OrderedByComparator is an ordered set of unique T values where the ordering
// is defined by a separate comparitor.
// is defined by a separate comparitor type.
type OrderedByComparator[T any, C constraints.Comparator[T]] struct {
Comparator C

members []T
}

// NewOrderedByComparator returns an [OrderedByComparator] containing the given
// members.
func NewOrderedByComparator[T any, C constraints.Comparator[T]](
cmp C,
members ...T,
) *OrderedByComparator[T, C] {
s := OrderedByComparator[T, C]{
Comparator: cmp,
}

s.Add(members...)

return &s
func NewOrderedByComparator[T any, C constraints.Comparator[T]](members ...T) *OrderedByComparator[T, C] {
return orderedFromUnsortedMembers[T, *OrderedByComparator[T, C]](members)
}

// NewOrderedByComparatorFromSeq returns an [OrderedByComparator] containing the
// values yielded by the given sequence.
func NewOrderedByComparatorFromSeq[T any, C constraints.Comparator[T]](
cmp C,
seq iter.Seq[T],
) *OrderedByComparator[T, C] {
s := OrderedByComparator[T, C]{
Comparator: cmp,
}

for m := range seq {
s.Add(m)
}

return &s
// NewOrderedByComparatorFromSeq returns an [OrderedByComparator] containing the values
// yielded by the given sequence.
func NewOrderedByComparatorFromSeq[T any, C constraints.Comparator[T]](seq iter.Seq[T]) *OrderedByComparator[T, C] {
return orderedFromSeq[T, *OrderedByComparator[T, C]](seq)
}

// NewOrderedByComparatorFromKeys returns an [OrderedByComparator] containing the
// keys yielded by the given sequence.
func NewOrderedByComparatorFromKeys[T any, C constraints.Comparator[T], unused any](
cmp C,
seq iter.Seq2[T, unused],
) *OrderedByComparator[T, C] {
s := OrderedByComparator[T, C]{
Comparator: cmp,
}

for m := range seq {
s.Add(m)
}

return &s
// NewOrderedByComparatorFromKeys returns an [OrderedByComparator] containing the keys
// yielded by the given sequence.
func NewOrderedByComparatorFromKeys[T any, C constraints.Comparator[T], unused any](seq iter.Seq2[T, unused]) *OrderedByComparator[T, C] {
return orderedFromKeys[T, *OrderedByComparator[T, C]](seq)
}

// NewOrderedByComparatorFromValues returns an [OrderedByComparator] containing
// the values yielded by the given sequence.
func NewOrderedByComparatorFromValues[T any, C constraints.Comparator[T], unused any](
cmp C,
seq iter.Seq2[unused, T],
) *OrderedByComparator[T, C] {
s := OrderedByComparator[T, C]{
Comparator: cmp,
}

for _, m := range seq {
s.Add(m)
}

return &s
// NewOrderedByComparatorFromValues returns an [OrderedByComparator] containing the
// values yielded by the given sequence.
func NewOrderedByComparatorFromValues[T any, C constraints.Comparator[T], unused any](seq iter.Seq2[unused, T]) *OrderedByComparator[T, C] {
return orderedFromValues[T, *OrderedByComparator[T, C]](seq)
}

// Add adds the given members to the set.
Expand Down Expand Up @@ -164,22 +120,11 @@ func (s *OrderedByComparator[T, C]) Reverse() iter.Seq[T] {
return orderedReverse[T](s)
}

func (s *OrderedByComparator[T, C]) new(members []T) *OrderedByComparator[T, C] {
var cmp C
if s != nil {
cmp = s.Comparator
}

return &OrderedByComparator[T, C]{
Comparator: cmp,
members: members,
}
}

func (s *OrderedByComparator[T, C]) ptr() *[]T {
return &s.members
}

func (s *OrderedByComparator[T, C]) cmp(x, y T) int {
return s.Comparator.Compare(x, y)
var c C
return c.Compare(x, y)
}
Loading

0 comments on commit 99f439a

Please sign in to comment.