set

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2022 License: BSD-3-Clause Imports: 11 Imported by: 8

Documentation

Overview

Example (Serialize)
package main

import (
	"log"
)

import (
	"github.com/timtadh/data-structures/list"
	"github.com/timtadh/data-structures/set"
	"github.com/timtadh/data-structures/types"
)

func makeSet() *set.SortedSet {
	return set.FromSlice([]types.Hashable{types.Int(1), types.Int(-1), types.Int(3)})
}

func serialize(s *set.SortedSet) ([]byte, error) {
	marshal, unmarshal := types.IntMarshals()
	m := set.NewMSortedSet(s, marshal, unmarshal)
	return m.MarshalBinary()
}

func deserialize(bytes []byte) (*set.SortedSet, error) {
	marshal, unmarshal := types.IntMarshals()
	m := &set.MSortedSet{MSorted: list.MSorted{MList: list.MList{MarshalItem: marshal, UnmarshalItem: unmarshal}}}
	err := m.UnmarshalBinary(bytes)
	if err != nil {
		return nil, err
	}
	return m.SortedSet(), nil
}

func main() {
	a := makeSet()
	b := makeSet()
	if !a.Equals(b) {
		log.Panic("a was not equal to b")
	}
	bytes, err := serialize(a)
	if err != nil {
		log.Panic(err)
	}
	log.Println(bytes)
	c, err := deserialize(bytes)
	if err != nil {
		log.Panic(err)
	}
	if !c.Equals(b) {
		log.Panic("c was not equal to b")
	}
	log.Println("success")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Intersect

func Intersect(a, b types.Set) (types.Set, error)

func ProperSubset

func ProperSubset(a, b types.Set) bool

func ProperSuperset

func ProperSuperset(a, b types.Set) bool

func Subset

func Subset(a, b types.Set) bool

func Subtract

func Subtract(a, b types.Set) (types.Set, error)

Unions s with o and returns a new Sorted Set

func Superset

func Superset(a, b types.Set) bool

func Union

func Union(a, b types.Set) (types.Set, error)

Types

type MSortedSet

type MSortedSet struct {
	list.MSorted
}

func NewMSortedSet

func NewMSortedSet(s *SortedSet, marshal types.ItemMarshal, unmarshal types.ItemUnmarshal) *MSortedSet

func (*MSortedSet) SortedSet

func (m *MSortedSet) SortedSet() *SortedSet

type MapSet

type MapSet struct {
	Set types.Set
}

func NewMapSet

func NewMapSet(set types.Set) *MapSet

func (*MapSet) Add

func (m *MapSet) Add(item types.Hashable) (err error)

func (*MapSet) Delete

func (m *MapSet) Delete(item types.Hashable) (err error)

func (*MapSet) Equals

func (m *MapSet) Equals(b types.Equatable) bool

func (*MapSet) Extend

func (m *MapSet) Extend(items types.KIterator) (err error)

func (*MapSet) Get

func (m *MapSet) Get(key types.Hashable) (value interface{}, err error)

func (*MapSet) Has

func (m *MapSet) Has(key types.Hashable) bool

func (*MapSet) Intersect

func (m *MapSet) Intersect(b types.Set) (types.Set, error)

func (*MapSet) Item

func (m *MapSet) Item(key types.Hashable) (me types.Hashable, err error)

func (*MapSet) Items

func (m *MapSet) Items() types.KIterator

func (*MapSet) Iterate

func (m *MapSet) Iterate() (kvit types.KVIterator)

func (*MapSet) Keys

func (m *MapSet) Keys() types.KIterator

func (*MapSet) ProperSubset

func (m *MapSet) ProperSubset(b types.Set) bool

func (*MapSet) ProperSuperset

func (m *MapSet) ProperSuperset(b types.Set) bool

func (*MapSet) Put

func (m *MapSet) Put(key types.Hashable, value interface{}) (err error)

func (*MapSet) Remove

func (m *MapSet) Remove(key types.Hashable) (value interface{}, err error)

func (*MapSet) Size

func (m *MapSet) Size() int

func (*MapSet) String

func (m *MapSet) String() string

func (*MapSet) Subset

func (m *MapSet) Subset(b types.Set) bool

func (*MapSet) Subtract

func (m *MapSet) Subtract(b types.Set) (types.Set, error)

func (*MapSet) Superset

func (m *MapSet) Superset(b types.Set) bool

func (*MapSet) Union

func (m *MapSet) Union(b types.Set) (types.Set, error)

func (*MapSet) Values

func (m *MapSet) Values() types.Iterator

type SetMap

type SetMap struct {
	types.Map
}

func NewSetMap

func NewSetMap(m types.Map) *SetMap

func (*SetMap) Add

func (s *SetMap) Add(item types.Hashable) (err error)

func (*SetMap) Delete

func (s *SetMap) Delete(item types.Hashable) (err error)

func (*SetMap) Equals

func (s *SetMap) Equals(o types.Equatable) bool

unimplemented

func (*SetMap) Extend

func (s *SetMap) Extend(items types.KIterator) (err error)

func (*SetMap) Intersect

func (s *SetMap) Intersect(other types.Set) (types.Set, error)

Unions s with o and returns a new SetMap (with a LinearHash)

func (*SetMap) Item

func (s *SetMap) Item(item types.Hashable) (types.Hashable, error)

unimplemented

func (*SetMap) Items

func (s *SetMap) Items() types.KIterator

func (*SetMap) ProperSubset

func (s *SetMap) ProperSubset(o types.Set) bool

Is s a proper subset of o?

func (*SetMap) ProperSuperset

func (s *SetMap) ProperSuperset(o types.Set) bool

Is s a proper superset of o?

func (*SetMap) String added in v0.5.3

func (s *SetMap) String() string

func (*SetMap) Subset

func (s *SetMap) Subset(o types.Set) bool

Is s a subset of o?

func (*SetMap) Subtract

func (s *SetMap) Subtract(other types.Set) (types.Set, error)

Unions s with o and returns a new SetMap (with a LinearHash)

func (*SetMap) Superset

func (s *SetMap) Superset(o types.Set) bool

Is s a superset of o?

func (*SetMap) Union

func (s *SetMap) Union(other types.Set) (types.Set, error)

Unions s with o and returns a new SetMap (with a LinearHash)

type SortedSet

type SortedSet struct {
	list.Sorted
}

SortedSet is a list.Sorted and therefore has all of the methods that list.Sorted has. So although they do not show up in the generated docs you can just do this:

s := NewSortedSet(10)
s.Add(types.Int(5))
s2 = s.Union(FromSlice([]types.Hashable{types.Int(7)}))
fmt.Println(s2.Has(types.Int(7)))
fmt.Println(s.Has(types.Int(7)))

func FromSlice

func FromSlice(items []types.Hashable) *SortedSet

func NewSortedSet

func NewSortedSet(initialSize int) *SortedSet

func SortedFromSet

func SortedFromSet(s types.Set) *SortedSet

func (*SortedSet) Copy

func (s *SortedSet) Copy() *SortedSet

func (*SortedSet) Intersect

func (s *SortedSet) Intersect(other types.Set) (types.Set, error)

Unions s with o and returns a new Sorted Set

func (*SortedSet) Overlap

func (s *SortedSet) Overlap(o *SortedSet) bool

Are there any overlapping elements?

func (*SortedSet) ProperSubset

func (s *SortedSet) ProperSubset(o types.Set) bool

Is s a proper subset of o?

func (*SortedSet) ProperSuperset

func (s *SortedSet) ProperSuperset(o types.Set) bool

Is s a proper superset of o?

func (*SortedSet) Random

func (s *SortedSet) Random() (item types.Hashable, err error)

func (*SortedSet) Subset

func (s *SortedSet) Subset(o types.Set) bool

Is s a subset of o?

func (*SortedSet) Subtract

func (s *SortedSet) Subtract(other types.Set) (types.Set, error)

Unions s with o and returns a new Sorted Set

func (*SortedSet) Superset

func (s *SortedSet) Superset(o types.Set) bool

Is s a superset of o?

func (*SortedSet) Union

func (s *SortedSet) Union(other types.Set) (types.Set, error)

Unions s with o and returns a new Sorted Set

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL