Skip to content

nlatham1999/sortedset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

sortedset

import "github.com/nlatham1999/sortedset"

Example Usage

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))
			})
		})
	})

Tips

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
		})
	})

Index

Variables

var (
    ErrItemExists      = errors.New("item exists")
    ErrItemDoesntExist = errors.New("item doesn't exist")
)

type SortedSet

type SortedSet struct {
    // contains filtered or unexported fields
}

func NewSortedSet

func NewSortedSet(values ...interface{}) *SortedSet

create a new sorted set with the given values

func (*SortedSet) Add

func (ss *SortedSet) Add(value interface{}) error

adds a value to the end of the set

func (*SortedSet) After

func (ss *SortedSet) After(value interface{}) (interface{}, error)

returns the next value in the set after the given value

func (*SortedSet) All

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 (*SortedSet) Any

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 (*SortedSet) Ask

func (ss *SortedSet) Ask(operation func(value interface{}))

takes a function and applies it to all values in the set

func (*SortedSet) Before

func (ss *SortedSet) Before(value interface{}) (interface{}, error)

returns the value before the given value

func (*SortedSet) Contains

func (ss *SortedSet) Contains(value interface{}) bool

returns if the value exists in the set

func (*SortedSet) Current

func (ss *SortedSet) Current() interface{}

returns the current value of the pointer

func (*SortedSet) Difference

func (ss *SortedSet) Difference(set *SortedSet) *SortedSet

returns the difference of this set and the given set as a new set (this - set)

func (*SortedSet) Empty

func (ss *SortedSet) Empty() bool

returns if the set is empty

func (*SortedSet) First

func (ss *SortedSet) First() interface{}

returns the first value in the set and sets the pointer to it

func (*SortedSet) InsertAfter

func (ss *SortedSet) InsertAfter(value, after interface{}) error

inserts a value after another value

func (*SortedSet) InsertBefore

func (ss *SortedSet) InsertBefore(value, before interface{}) error

inserts a value before another value

func (*SortedSet) Intersection

func (ss *SortedSet) Intersection(set *SortedSet) *SortedSet

returns the intersection of this set and the given set as a new set

func (*SortedSet) Last

func (ss *SortedSet) Last() interface{}

returns the last value in the set and sets the pointer to it

func (*SortedSet) Len

func (ss *SortedSet) Len() int

returns the length of the set

func (*SortedSet) List

func (ss *SortedSet) List() []interface{}

returns the set as a slice

func (*SortedSet) Next

func (ss *SortedSet) Next() (interface{}, error)

moves the pointer to the next value in the set and returns it

func (*SortedSet) Previous

func (ss *SortedSet) Previous() (interface{}, error)

moves the pointer to the previous value in the set and returns it

func (*SortedSet) Remove

func (ss *SortedSet) Remove(value interface{}) error

removes a value from the set

func (*SortedSet) SortAsc

func (ss *SortedSet) SortAsc(f func(e interface{}) interface{})

sorts the set in ascending order

func (*SortedSet) SortDesc

func (ss *SortedSet) SortDesc(f func(e interface{}) interface{})

sorts the set in descending order

func (*SortedSet) SymmetricDifference

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 (*SortedSet) Union

func (ss *SortedSet) Union(set *SortedSet) *SortedSet

returns the union of this set and the given set as a new set

Generated by gomarkdoc

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages