- linkedhashset simplification

pull/91/head
emirpasic 7 years ago
parent bd99060a93
commit 73e1b206f9

@ -20,7 +20,7 @@ type Iterator struct {
// Iterator returns a stateful iterator whose values can be fetched by an index. // Iterator returns a stateful iterator whose values can be fetched by an index.
func (set *Set) Iterator() Iterator { func (set *Set) Iterator() Iterator {
return Iterator{iterator: set.list.Iterator()} return Iterator{iterator: set.ordering.Iterator()}
} }
// Next moves the iterator to the next element and returns true if there was a next element in the container. // Next moves the iterator to the next element and returns true if there was a next element in the container.

@ -26,8 +26,8 @@ func assertSetImplementation() {
// Set holds elements in go's native map // Set holds elements in go's native map
type Set struct { type Set struct {
items map[interface{}]struct{} table map[interface{}]struct{}
list *doublylinkedlist.List ordering *doublylinkedlist.List
} }
var itemExists = struct{}{} var itemExists = struct{}{}
@ -35,8 +35,8 @@ var itemExists = struct{}{}
// New instantiates a new empty set and adds the passed values, if any, to the set // New instantiates a new empty set and adds the passed values, if any, to the set
func New(values ...interface{}) *Set { func New(values ...interface{}) *Set {
set := &Set{ set := &Set{
items: make(map[interface{}]struct{}), table: make(map[interface{}]struct{}),
list: doublylinkedlist.New(), ordering: doublylinkedlist.New(),
} }
if len(values) > 0 { if len(values) > 0 {
set.Add(values...) set.Add(values...)
@ -48,13 +48,10 @@ func New(values ...interface{}) *Set {
// Note that insertion-order is not affected if an element is re-inserted into the set. // Note that insertion-order is not affected if an element is re-inserted into the set.
func (set *Set) Add(items ...interface{}) { func (set *Set) Add(items ...interface{}) {
for _, item := range items { for _, item := range items {
if _, contains := set.table[item]; !contains {
if _, contains := set.items[item]; contains { set.table[item] = itemExists
continue set.ordering.Append(item)
} }
set.items[item] = itemExists
set.list.Append(item)
} }
} }
@ -62,14 +59,11 @@ func (set *Set) Add(items ...interface{}) {
// Slow operation, worst-case O(n^2). // Slow operation, worst-case O(n^2).
func (set *Set) Remove(items ...interface{}) { func (set *Set) Remove(items ...interface{}) {
for _, item := range items { for _, item := range items {
if _, contains := set.table[item]; contains {
if _, contains := set.items[item]; !contains { delete(set.table, item)
continue index := set.ordering.IndexOf(item)
set.ordering.Remove(index)
} }
delete(set.items, item)
index := set.list.IndexOf(item)
set.list.Remove(index)
} }
} }
@ -78,7 +72,7 @@ func (set *Set) Remove(items ...interface{}) {
// Returns true if no arguments are passed at all, i.e. set is always superset of empty set. // Returns true if no arguments are passed at all, i.e. set is always superset of empty set.
func (set *Set) Contains(items ...interface{}) bool { func (set *Set) Contains(items ...interface{}) bool {
for _, item := range items { for _, item := range items {
if _, contains := set.items[item]; !contains { if _, contains := set.table[item]; !contains {
return false return false
} }
} }
@ -92,13 +86,13 @@ func (set *Set) Empty() bool {
// Size returns number of elements within the set. // Size returns number of elements within the set.
func (set *Set) Size() int { func (set *Set) Size() int {
return set.list.Size() return set.ordering.Size()
} }
// Clear clears all values in the set. // Clear clears all values in the set.
func (set *Set) Clear() { func (set *Set) Clear() {
set.items = make(map[interface{}]struct{}) set.table = make(map[interface{}]struct{})
set.list.Clear() set.ordering.Clear()
} }
// Values returns all items in the set. // Values returns all items in the set.

Loading…
Cancel
Save