All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.collections.BTreeMap Maven / Gradle / Ivy

Go to download

Immutable, structure sharing collections, including hash array mapped tries, finger tries, B-trees, and S-trees (sequence trees)

There is a newer version: 4.3.15
Show newest version
// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swim.collections;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.util.CombinerFunction;
import swim.util.Cursor;
import swim.util.OrderedMapCursor;
import swim.util.ReducedMap;

/**
 * Mutable, thread-safe {@link Map} backed by a B-tree.
 */
public class BTreeMap extends BTreeContext implements ReducedMap, Cloneable, Debug {
  volatile BTreePage root;

  protected BTreeMap(BTreePage root) {
    this.root = root;
  }

  public BTreeMap() {
    this(BTreePage.empty());
  }

  @Override
  public boolean isEmpty() {
    return this.root.isEmpty();
  }

  @Override
  public int size() {
    return this.root.size();
  }

  @Override
  public boolean containsKey(Object key) {
    return this.root.containsKey(key, this);
  }

  @Override
  public boolean containsValue(Object value) {
    return this.root.containsValue(value);
  }

  @Override
  public int indexOf(Object key) {
    return this.root.indexOf(key, this);
  }

  @Override
  public V get(Object key) {
    return this.root.get(key, this);
  }

  @Override
  public Entry getEntry(Object key) {
    return this.root.getEntry(key, this);
  }

  @Override
  public Entry getIndex(int index) {
    return this.root.getIndex(index);
  }

  @Override
  public Entry firstEntry() {
    return this.root.firstEntry();
  }

  @Override
  public K firstKey() {
    final Entry entry = this.root.firstEntry();
    if (entry != null) {
      return entry.getKey();
    } else {
      return null;
    }
  }

  @Override
  public V firstValue() {
    final Entry entry = this.root.firstEntry();
    if (entry != null) {
      return entry.getValue();
    } else {
      return null;
    }
  }

  @Override
  public Entry lastEntry() {
    return this.root.lastEntry();
  }

  @Override
  public K lastKey() {
    final Entry entry = this.root.lastEntry();
    if (entry != null) {
      return entry.getKey();
    } else {
      return null;
    }
  }

  @Override
  public V lastValue() {
    final Entry entry = this.root.lastEntry();
    if (entry != null) {
      return entry.getValue();
    } else {
      return null;
    }
  }

  @Override
  public Entry nextEntry(K key) {
    return this.root.nextEntry(key, this);
  }

  @Override
  public K nextKey(K key) {
    final Entry entry = this.root.nextEntry(key, this);
    if (entry != null) {
      return entry.getKey();
    } else {
      return null;
    }
  }

  @Override
  public V nextValue(K key) {
    final Entry entry = this.root.nextEntry(key, this);
    if (entry != null) {
      return entry.getValue();
    } else {
      return null;
    }
  }

  @Override
  public Entry previousEntry(K key) {
    return this.root.previousEntry(key, this);
  }

  @Override
  public K previousKey(K key) {
    final Entry entry = this.root.previousEntry(key, this);
    if (entry != null) {
      return entry.getKey();
    } else {
      return null;
    }
  }

  @Override
  public V previousValue(K key) {
    final Entry entry = this.root.previousEntry(key, this);
    if (entry != null) {
      return entry.getValue();
    } else {
      return null;
    }
  }

  @Override
  public V put(K key, V newValue) {
    BTreePage oldRoot;
    do {
      oldRoot = this.root;
      BTreePage newRoot = oldRoot.updated(key, newValue, this);
      if (oldRoot != newRoot) {
        if (newRoot.size() > oldRoot.size()) {
          newRoot = newRoot.balanced(this);
        }
        if (ROOT.compareAndSet(this, oldRoot, newRoot)) {
          break;
        }
      } else {
        break;
      }
    } while (true);
    return oldRoot.get(key, this);
  }

  @Override
  public void putAll(Map map) {
    for (Entry entry : map.entrySet()) {
      put(entry.getKey(), entry.getValue());
    }
  }

  @Override
  public V remove(Object key) {
    do {
      final BTreePage oldRoot = this.root;
      final BTreePage newRoot = oldRoot.removed(key, this).balanced(this);
      if (oldRoot != newRoot) {
        if (ROOT.compareAndSet(this, oldRoot, newRoot)) {
          return oldRoot.get(key, this);
        }
      } else {
        return null;
      }
    } while (true);
  }

  public BTreeMap drop(int lower) {
    do {
      final BTreePage oldRoot = this.root;
      if (lower > 0 && oldRoot.size() > 0) {
        final BTreePage newRoot;
        if (lower < oldRoot.size()) {
          newRoot = oldRoot.drop(lower, this).balanced(this);
        } else {
          newRoot = BTreePage.empty();
        }
        if (ROOT.compareAndSet(this, oldRoot, newRoot)) {
          break;
        }
      } else {
        break;
      }
    } while (true);
    return this;
  }

  public BTreeMap take(int upper) {
    do {
      final BTreePage oldRoot = this.root;
      if (upper < oldRoot.size() && oldRoot.size() > 0) {
        final BTreePage newRoot;
        if (upper > 0) {
          newRoot = oldRoot.take(upper, this).balanced(this);
        } else {
          newRoot = BTreePage.empty();
        }
        if (ROOT.compareAndSet(this, oldRoot, newRoot)) {
          break;
        }
      } else {
        break;
      }
    } while (true);
    return this;
  }

  @Override
  public void clear() {
    do {
      final BTreePage oldRoot = this.root;
      final BTreePage newRoot = BTreePage.empty();
      if (oldRoot != newRoot) {
        if (ROOT.compareAndSet(this, oldRoot, newRoot)) {
          break;
        }
      } else {
        break;
      }
    } while (true);
  }

  public BTreeMap updated(K key, V newValue) {
    final BTreePage oldRoot = this.root;
    BTreePage newRoot = oldRoot.updated(key, newValue, this);
    if (newRoot.size() > oldRoot.size()) {
      newRoot = newRoot.balanced(this);
    }
    return copy(newRoot);
  }

  public BTreeMap removed(K key) {
    return copy(this.root.removed(key, this).balanced(this));
  }

  public BTreeMap cleared() {
    return copy(BTreePage.empty());
  }

  @Override
  public U reduced(U identity, CombinerFunction accumulator, CombinerFunction combiner) {
    BTreePage newRoot;
    do {
      final BTreePage oldRoot = this.root;
      newRoot = oldRoot.reduced(identity, accumulator, combiner);
      if (oldRoot != newRoot) {
        if (ROOT.compareAndSet(this, oldRoot, newRoot)) {
          break;
        }
      } else {
        break;
      }
    } while (true);
    return newRoot.fold();
  }

  /**
   * An immutable copy of this {@code BTreeMap}'s data.
   */
  public BTree snapshot() {
    return new BTree(this.root);
  }

  @Override
  public OrderedMapCursor iterator() {
    return this.root.iterator();
  }

  public Cursor keyIterator() {
    return this.root.keyIterator();
  }

  public Cursor valueIterator() {
    return this.root.valueIterator();
  }

  public OrderedMapCursor lastIterator() {
    return this.root.lastIterator();
  }

  public Cursor lastKeyIterator() {
    return this.root.lastKeyIterator();
  }

  public Cursor lastValueIterator() {
    return this.root.lastValueIterator();
  }

  @Override
  public BTreeMap clone() {
    return copy(this.root);
  }

  protected BTreeMap copy(BTreePage root) {
    return new BTreeMap(root);
  }

  @Override
  public Comparator comparator() {
    return null;
  }

  @SuppressWarnings("unchecked")
  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    } else if (other instanceof Map) {
      final Map that = (Map) other;
      if (size() == that.size()) {
        final Iterator> those = that.entrySet().iterator();
        while (those.hasNext()) {
          final Entry entry = those.next();
          final V value = get(entry.getKey());
          final V v = entry.getValue();
          if (value == null ? v != null : !value.equals(v)) {
            return false;
          }
        }
        return true;
      }
    }
    return false;
  }

  @Override
  public int hashCode() {
    int code = 0;
    final Cursor> these = iterator();
    while (these.hasNext()) {
      code += these.next().hashCode();
    }
    return code;
  }

  @Override
  public void debug(Output output) {
    output = output.write("BTreeMap").write('.');
    final Cursor> these = iterator();
    if (these.hasNext()) {
      Entry entry = these.next();
      output = output.write("of").write('(')
          .debug(entry.getKey()).write(", ").debug(entry.getValue());
      while (these.hasNext()) {
        entry = these.next();
        output = output.write(')').write('.').write("updated").write('(')
            .debug(entry.getKey()).write(", ").debug(entry.getValue());
      }
    } else {
      output = output.write("empty").write('(');
    }
    output = output.write(')');
  }

  @Override
  public String toString() {
    return Format.debug(this);
  }

  public static  BTreeMap empty() {
    return new BTreeMap();
  }

  public static  BTreeMap of(K key, V value) {
    final BTreeMap tree = new BTreeMap();
    tree.put(key, value);
    return tree;
  }

  public static  BTreeMap from(Map map) {
    final BTreeMap tree = new BTreeMap();
    for (Entry entry : map.entrySet()) {
      tree.put(entry.getKey(), entry.getValue());
    }
    return tree;
  }

  @SuppressWarnings("rawtypes")
  static final AtomicReferenceFieldUpdater ROOT =
      AtomicReferenceFieldUpdater.newUpdater(BTreeMap.class, BTreePage.class, "root");
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy