SortedSet is a set of elements that can be sorted with a provided sorter function.
Items added will not cause the set to be resorted. This must be done as an extra step.
import "github.com/nlatham1999/sortedset"
sorting:
type coord struct {
x, y int
}
ss := NewSortedSet(coord{1, 2}, coord{2, 3}, coord{3, 4})
ss.SortDesc(func(e interface{}) interface{} {
c := e.(coord)
return c.x
})
inserting:
ss := NewSortedSet(1, 3)
ss.InsertBefore(2, 3)
using next:
ss := NewSortedSet(1, 2, 3)
next, _ := ss.Next(1)
nested loops:
ss := NewSortedSet(1, 2, 3)
result := 0
ss.Ask(func(value1 interface{}) {
ss.Ask(func(value2 interface{}) {
ss.Ask(func(value3 interface{}) {
result += (value1.(int) * value2.(int) * value3.(int))
})
})
})
When looping through the set it is fine doing something like this:
v := ss.First()
for v != nil {
// operation
v, _ = ss.Next()
}
But when you are using nested loops you must use the Ask function. This is because the pointer pointing to the next value will be shared by every nested loop causing it to exit early/infinite loop.
//instead of this:
for v := ss.First(); v != nil; v, _ = ss.Next() {
for v2 := ss.First(); v2 != nil; v2, _ = ss.Next() {
// operation
}
}
// do this:
ss.Ask(func(value1 interface{}) {
ss.Ask(func(value2 interface{}) {
//operation
})
})
- Variables
- type SortedSet
- func NewSortedSet(values ...interface{}) *SortedSet
- func (ss *SortedSet) Add(value interface{}) error
- func (ss *SortedSet) After(value interface{}) (interface{}, error)
- func (ss *SortedSet) All(operation func(value interface{}) bool) bool
- func (ss *SortedSet) Any(operation func(value interface{}) bool) bool
- func (ss *SortedSet) Ask(operation func(value interface{}))
- func (ss *SortedSet) Before(value interface{}) (interface{}, error)
- func (ss *SortedSet) Contains(value interface{}) bool
- func (ss *SortedSet) Current() interface{}
- func (ss *SortedSet) Difference(set *SortedSet) *SortedSet
- func (ss *SortedSet) Empty() bool
- func (ss *SortedSet) First() interface{}
- func (ss *SortedSet) InsertAfter(value, after interface{}) error
- func (ss *SortedSet) InsertBefore(value, before interface{}) error
- func (ss *SortedSet) Intersection(set *SortedSet) *SortedSet
- func (ss *SortedSet) Last() interface{}
- func (ss *SortedSet) Len() int
- func (ss *SortedSet) List() []interface{}
- func (ss *SortedSet) Next() (interface{}, error)
- func (ss *SortedSet) Previous() (interface{}, error)
- func (ss *SortedSet) Remove(value interface{}) error
- func (ss *SortedSet) SortAsc(f func(e interface{}) interface{})
- func (ss *SortedSet) SortDesc(f func(e interface{}) interface{})
- func (ss *SortedSet) SymmetricDifference(set *SortedSet) *SortedSet
- func (ss *SortedSet) Union(set *SortedSet) *SortedSet
var (
ErrItemExists = errors.New("item exists")
ErrItemDoesntExist = errors.New("item doesn't exist")
)
type SortedSet struct {
// contains filtered or unexported fields
}
func NewSortedSet(values ...interface{}) *SortedSet
create a new sorted set with the given values
func (ss *SortedSet) Add(value interface{}) error
adds a value to the end of the set
func (ss *SortedSet) After(value interface{}) (interface{}, error)
returns the next value in the set after the given value
func (ss *SortedSet) All(operation func(value interface{}) bool) bool
takes a function that returns a boolean and returns true if all values in the set return true
func (ss *SortedSet) Any(operation func(value interface{}) bool) bool
takes a function that returns a boolean and returns true if any value in the set returns true
func (ss *SortedSet) Ask(operation func(value interface{}))
takes a function and applies it to all values in the set
func (ss *SortedSet) Before(value interface{}) (interface{}, error)
returns the value before the given value
func (ss *SortedSet) Contains(value interface{}) bool
returns if the value exists in the set
func (ss *SortedSet) Current() interface{}
returns the current value of the pointer
func (ss *SortedSet) Difference(set *SortedSet) *SortedSet
returns the difference of this set and the given set as a new set (this - set)
func (ss *SortedSet) Empty() bool
returns if the set is empty
func (ss *SortedSet) First() interface{}
returns the first value in the set and sets the pointer to it
func (ss *SortedSet) InsertAfter(value, after interface{}) error
inserts a value after another value
func (ss *SortedSet) InsertBefore(value, before interface{}) error
inserts a value before another value
func (ss *SortedSet) Intersection(set *SortedSet) *SortedSet
returns the intersection of this set and the given set as a new set
func (ss *SortedSet) Last() interface{}
returns the last value in the set and sets the pointer to it
func (ss *SortedSet) Len() int
returns the length of the set
func (ss *SortedSet) List() []interface{}
returns the set as a slice
func (ss *SortedSet) Next() (interface{}, error)
moves the pointer to the next value in the set and returns it
func (ss *SortedSet) Previous() (interface{}, error)
moves the pointer to the previous value in the set and returns it
func (ss *SortedSet) Remove(value interface{}) error
removes a value from the set
func (ss *SortedSet) SortAsc(f func(e interface{}) interface{})
sorts the set in ascending order
func (ss *SortedSet) SortDesc(f func(e interface{}) interface{})
sorts the set in descending order
func (ss *SortedSet) SymmetricDifference(set *SortedSet) *SortedSet
returns the symmetric difference of this set and the given set as a new set (this XOR set)
func (ss *SortedSet) Union(set *SortedSet) *SortedSet
returns the union of this set and the given set as a new set
Generated by gomarkdoc