Dart API Referencedart:coreimplHashSetImplementation<E>

HashSetImplementation<E> class

class HashSetImplementation<E > implements HashSet<E> {

  HashSetImplementation() {
    _backingMap = new HashMapImplementation<E, E>();
  }

  factory HashSetImplementation.from(Iterable<E> other) {
    Set<E> set = new HashSetImplementation<E>();
    for (final e in other) {
      set.add(e);
    }
    return set;
  }

  void clear() {
    _backingMap.clear();
  }

  void add(E value) {
    _backingMap[value] = value;
  }

  bool contains(E value) {
    return _backingMap.containsKey(value);
  }

  bool remove(E value) {
    if (!_backingMap.containsKey(value)) return false;
    _backingMap.remove(value);
    return true;
  }

  void addAll(Collection<E> collection) {
    collection.forEach(void _(E value) {
      add(value);
    });
  }

  Set<E> intersection(Collection<E> collection) {
    Set<E> result = new Set<E>();
    collection.forEach(void _(E value) {
      if (contains(value)) result.add(value);
    });
    return result;
  }

  bool isSubsetOf(Collection<E> other) {
    return new Set<E>.from(other).containsAll(this);
  }

  void removeAll(Collection<E> collection) {
    collection.forEach(void _(E value) {
      remove(value);
    });
  }

  bool containsAll(Collection<E> collection) {
    return collection.every(bool _(E value) {
      return contains(value);
    });
  }

  void forEach(void f(E element)) {
    _backingMap.forEach(void _(E key, E value) {
      f(key);
    });
  }

  Set map(f(E element)) {
    Set result = new Set();
    _backingMap.forEach(void _(E key, E value) {
      result.add(f(key));
    });
    return result;
  }

  Dynamic reduce(Dynamic initialValue,
                 Dynamic combine(Dynamic previousValue, E element)) {
    return Collections.reduce(this, initialValue, combine);
  }

  Set<E> filter(bool f(E element)) {
    Set<E> result = new Set<E>();
    _backingMap.forEach(void _(E key, E value) {
      if (f(key)) result.add(key);
    });
    return result;
  }

  bool every(bool f(E element)) {
    Collection<E> keys = _backingMap.getKeys();
    return keys.every(f);
  }

  bool some(bool f(E element)) {
    Collection<E> keys = _backingMap.getKeys();
    return keys.some(f);
  }

  bool isEmpty() {
    return _backingMap.isEmpty();
  }

  int get length {
    return _backingMap.length;
  }

  Iterator<E> iterator() {
    return new HashSetIterator<E>(this);
  }

  String toString() {
    return Collections.collectionToString(this);
  }

  // The map backing this set. The associations in this map are all
  // of the form element -> element. If a value is not in the map,
  // then it is not in the set.
  HashMapImplementation<E, E> _backingMap;
}

Implements

HashSet<E>

Constructors

new HashSetImplementation() #

HashSetImplementation() {
  _backingMap = new HashMapImplementation<E, E>();
}

factory HashSetImplementation.from(Iterable<E> other) #

factory HashSetImplementation.from(Iterable<E> other) {
  Set<E> set = new HashSetImplementation<E>();
  for (final e in other) {
    set.add(e);
  }
  return set;
}

Properties

final int length #

Returns the number of elements in this collection.

docs inherited from Collection<E>
int get length {
  return _backingMap.length;
}

Methods

void add(E value) #

Adds value into the set. The method has no effect if value was already in the set.

docs inherited from Set<E>
void add(E value) {
  _backingMap[value] = value;
}

void addAll(Collection<E> collection) #

Adds all the elements of the given collection to the set.

docs inherited from Set<E>
void addAll(Collection<E> collection) {
  collection.forEach(void _(E value) {
    add(value);
  });
}

void clear() #

Removes all elements in the set.

docs inherited from Set<E>
void clear() {
  _backingMap.clear();
}

bool contains(E value) #

Returns true if value is in the set.

docs inherited from Set<E>
bool contains(E value) {
  return _backingMap.containsKey(value);
}

bool containsAll(Collection<E> collection) #

Returns true if this collection contains all the elements of collection.

docs inherited from Set<E>
bool containsAll(Collection<E> collection) {
  return collection.every(bool _(E value) {
    return contains(value);
  });
}

bool every(bool f(E element)) #

Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.

docs inherited from Collection<E>
bool every(bool f(E element)) {
  Collection<E> keys = _backingMap.getKeys();
  return keys.every(f);
}

Set<E> filter(bool f(E element)) #

Returns a collection with the elements of this collection that satisfy the predicate f.

The returned collection should be of the same type as the collection creating it.

An element satisfies the predicate f if f(element) returns true.

docs inherited from Collection<E>
Set<E> filter(bool f(E element)) {
  Set<E> result = new Set<E>();
  _backingMap.forEach(void _(E key, E value) {
    if (f(key)) result.add(key);
  });
  return result;
}

void forEach(void f(E element)) #

Applies the function f to each element of this collection.

docs inherited from Collection<E>
void forEach(void f(E element)) {
  _backingMap.forEach(void _(E key, E value) {
    f(key);
  });
}

Set<E> intersection(Collection<E> collection) #

Returns a new set which is the intersection between this set and the given collection.

docs inherited from Set<E>
Set<E> intersection(Collection<E> collection) {
  Set<E> result = new Set<E>();
  collection.forEach(void _(E value) {
    if (contains(value)) result.add(value);
  });
  return result;
}

bool isEmpty() #

Returns true if there is no element in this collection.

docs inherited from Collection<E>
bool isEmpty() {
  return _backingMap.isEmpty();
}

bool isSubsetOf(Collection<E> other) #

Returns true if collection contains all the elements of this collection.

docs inherited from Set<E>
bool isSubsetOf(Collection<E> other) {
  return new Set<E>.from(other).containsAll(this);
}

Iterator<E> iterator() #

Returns an Iterator that iterates over this Iterable object.

docs inherited from Iterable<E>
Iterator<E> iterator() {
  return new HashSetIterator<E>(this);
}

Set map(f(E element)) #

Returns a new collection with the elements f(e) for each element e of this collection.

Subclasses of Collection should implement the map method to return a collection of the same general type as themselves. E.g., List.map should return a List.

docs inherited from Collection<E>
Set map(f(E element)) {
  Set result = new Set();
  _backingMap.forEach(void _(E key, E value) {
    result.add(f(key));
  });
  return result;
}

reduce(initialValue, combine(previousValue, E element)) #

Reduce a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function. Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.

Example of calculating the sum of a collection:

collection.reduce(0, (prev, element) => prev + element);

docs inherited from Collection<E>
Dynamic reduce(Dynamic initialValue,
               Dynamic combine(Dynamic previousValue, E element)) {
  return Collections.reduce(this, initialValue, combine);
}

bool remove(E value) #

Removes value from the set. Returns true if value was in the set. Returns false otherwise. The method has no effect if value value was not in the set.

docs inherited from Set<E>
bool remove(E value) {
  if (!_backingMap.containsKey(value)) return false;
  _backingMap.remove(value);
  return true;
}

void removeAll(Collection<E> collection) #

Removes all the elements of the given collection from the set.

docs inherited from Set<E>
void removeAll(Collection<E> collection) {
  collection.forEach(void _(E value) {
    remove(value);
  });
}

bool some(bool f(E element)) #

Returns true if one element of this collection satisfies the predicate f. Returns false otherwise.

docs inherited from Collection<E>
bool some(bool f(E element)) {
  Collection<E> keys = _backingMap.getKeys();
  return keys.some(f);
}

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() {
  return Collections.collectionToString(this);
}