Dart API Referencedart:coreimplHashMapImplementation<K, V>

HashMapImplementation<K extends Hashable, V> Class

Implements

HashMap<K, V>

Constructors

Code new HashMapImplementation() #

HashMapImplementation() {
  _numberOfEntries = 0;
  _numberOfDeleted = 0;
  _loadLimit = _computeLoadLimit(_INITIAL_CAPACITY);
  _keys = new List(_INITIAL_CAPACITY);
  _values = new List<V>(_INITIAL_CAPACITY);
}

Code factory HashMapImplementation.from(Map<K, V> other) #

factory HashMapImplementation.from(Map<K, V> other) {
  Map<K, V> result = new HashMapImplementation<K, V>();
  other.forEach((K key, V value) { result[key] = value; });
  return result;
}

Methods

Code void clear() #

void clear() {
  _numberOfEntries = 0;
  _numberOfDeleted = 0;
  int length = _keys.length;
  for (int i = 0; i < length; i++) {
    _keys[i] = null;
    _values[i] = null;
  }
}

Code bool containsKey(K key) #

bool containsKey(K key) {
  return (_probeForLookup(key) != -1);
}

Code bool containsValue(V value) #

bool containsValue(V value) {
  int length = _values.length;
  for (int i = 0; i < length; i++) {
    var key = _keys[i];
    if ((key !== null) && (key !== _DELETED_KEY)) {
      if (_values[i] == value) return true;
    }
  }
  return false;
}

Code void forEach(void f(K key, V value)) #

void forEach(void f(K key, V value)) {
  int length = _keys.length;
  for (int i = 0; i < length; i++) {
    var key = _keys[i];
    if ((key !== null) && (key !== _DELETED_KEY)) {
      f(key, _values[i]);
    }
  }
}

Code Collection<K> getKeys() #

Collection<K> getKeys() {
  List<K> list = new List<K>(length);
  int i = 0;
  forEach(void _(K key, V value) {
    list[i++] = key;
  });
  return list;
}

Code Collection<V> getValues() #

Collection<V> getValues() {
  List<V> list = new List<V>(length);
  int i = 0;
  forEach(void _(K key, V value) {
    list[i++] = value;
  });
  return list;
}

Code bool isEmpty() #

bool isEmpty() {
  return _numberOfEntries == 0;
}

Code int get length() #

int get length() {
  return _numberOfEntries;
}

Code V operator [](K key) #

V operator [](K key) {
  int index = _probeForLookup(key);
  if (index < 0) return null;
  return _values[index];
}

Code void operator []=(K key, V value) #

void operator []=(K key, V value) {
  _ensureCapacity();
  int index = _probeForAdding(key);
  if ((_keys[index] === null) || (_keys[index] === _DELETED_KEY)) {
    _numberOfEntries++;
  }
  _keys[index] = key;
  _values[index] = value;
}

Code V putIfAbsent(K key, V ifAbsent()) #

V putIfAbsent(K key, V ifAbsent()) {
  int index = _probeForLookup(key);
  if (index >=0) return _values[index];

  V value = ifAbsent();
  this[key] = value;
  return value;
}

Code V remove(K key) #

V remove(K key) {
  int index = _probeForLookup(key);
  if (index >= 0) {
    _numberOfEntries--;
    V value = _values[index];
    _values[index] = null;
    // Set the key to the sentinel to not break the probing chain.
    _keys[index] = _DELETED_KEY;
    _numberOfDeleted++;
    return value;
  }
  return null;
}

Code String toString() #

String toString() {
  return Maps.mapToString(this);
}