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

swim.collections.BTree 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 swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.util.Cursor;
import swim.util.OrderedMap;
import swim.util.OrderedMapCursor;

/**
 * Immutable {@link OrderedMap} backed by a B-tree.
 */
public class BTree extends BTreeContext implements OrderedMap, Debug {
  final BTreePage root;

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

  @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) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void putAll(Map map) {
    throw new UnsupportedOperationException();
  }

  @Override
  public V remove(Object key) {
    throw new UnsupportedOperationException();
  }

  public BTree drop(int lower) {
    if (lower > 0 && this.root.size() > 0) {
      if (lower < this.root.size()) {
        return copy(this.root.drop(lower, this).balanced(this));
      } else {
        return empty();
      }
    }
    return this;
  }

  public BTree take(int upper) {
    if (upper < this.root.size() && this.root.size() > 0) {
      if (upper > 0) {
        return copy(this.root.take(upper, this).balanced(this));
      } else {
        return empty();
      }
    }
    return this;
  }

  @Override
  public void clear() {
    throw new UnsupportedOperationException();
  }

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

  public BTree removed(K key) {
    final BTreePage oldRoot = this.root;
    final BTreePage newRoot = oldRoot.removed(key, this).balanced(this);
    if (oldRoot != newRoot) {
      return copy(newRoot);
    } else {
      return this;
    }
  }

  public BTree cleared() {
    return empty();
  }

  @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();
  }

  protected BTree copy(BTreePage root) {
    return new BTree(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("BTree").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);
  }

  private static BTree empty;

  @SuppressWarnings("unchecked")
  public static  BTree empty() {
    if (empty == null) {
      empty = new BTree(BTreePage.empty());
    }
    return (BTree) (BTree) empty;
  }

  public static  BTree of(K key, V value) {
    BTree tree = empty();
    tree = tree.updated(key, value);
    return tree;
  }

  public static  BTree from(Map map) {
    BTree tree = empty();
    for (Entry entry : map.entrySet()) {
      tree = tree.updated(entry.getKey(), entry.getValue());
    }
    return tree;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy