1package set
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/timtadh/data-structures/errors"
8	"github.com/timtadh/data-structures/types"
9)
10
11type SetMap struct {
12	types.Map
13}
14
15func NewSetMap(m types.Map) *SetMap {
16	return &SetMap{m}
17}
18
19func (s *SetMap) String() string {
20	if s.Size() <= 0 {
21		return "{}"
22	}
23	items := make([]string, 0, s.Size())
24	for item, next := s.Items()(); next != nil; item, next = next() {
25		items = append(items, fmt.Sprintf("%v", item))
26	}
27	return "{" + strings.Join(items, ", ") + "}"
28}
29
30func (s *SetMap) Items() types.KIterator {
31	return s.Keys()
32}
33
34// unimplemented
35func (s *SetMap) Item(item types.Hashable) (types.Hashable, error) {
36	return nil, errors.Errorf("un-implemented")
37}
38
39// unimplemented
40func (s *SetMap) Equals(o types.Equatable) bool {
41	panic(errors.Errorf("un-implemented"))
42}
43
44func (s *SetMap) Add(item types.Hashable) (err error) {
45	return s.Put(item, nil)
46}
47
48func (s *SetMap) Delete(item types.Hashable) (err error) {
49	_, err = s.Remove(item)
50	return err
51}
52
53func (s *SetMap) Extend(items types.KIterator) (err error) {
54	for item, next := items(); next != nil; item, next = next() {
55		err := s.Add(item)
56		if err != nil {
57			return err
58		}
59	}
60	return nil
61}
62
63// Unions s with o and returns a new SetMap (with a LinearHash)
64func (s *SetMap) Union(other types.Set) (types.Set, error) {
65	return Union(s, other)
66}
67
68// Unions s with o and returns a new SetMap (with a LinearHash)
69func (s *SetMap) Intersect(other types.Set) (types.Set, error) {
70	return Intersect(s, other)
71}
72
73// Unions s with o and returns a new SetMap (with a LinearHash)
74func (s *SetMap) Subtract(other types.Set) (types.Set, error) {
75	return Subtract(s, other)
76}
77
78// Is s a subset of o?
79func (s *SetMap) Subset(o types.Set) bool {
80	return Subset(s, o)
81}
82
83// Is s a proper subset of o?
84func (s *SetMap) ProperSubset(o types.Set) bool {
85	return ProperSubset(s, o)
86}
87
88// Is s a superset of o?
89func (s *SetMap) Superset(o types.Set) bool {
90	return Superset(s, o)
91}
92
93// Is s a proper superset of o?
94func (s *SetMap) ProperSuperset(o types.Set) bool {
95	return ProperSuperset(s, o)
96}
97