Skip to content

Commit

Permalink
initial building blocks for Rope data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ztellman committed Sep 5, 2017
1 parent 4bc534d commit 87241af
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pom.xml.asc
.idea
benchmarks/data/
.DS_Store
gen-javadoc.sh
gen-javadoc.sh
javadoc
54 changes: 39 additions & 15 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
(defproject io.lacuna/bifurcan "0.1.0-alpha1"
:java-source-paths ["src"]
:dependencies []
:test-selectors {:default #(not
(some #{:benchmark :stress}
(cons (:tag %) (keys %))))
:test-selectors {:default #(not
(some #{:benchmark :stress}
(cons (:tag %) (keys %))))
:benchmark :benchmark
:stress :stress
:all (constantly true)}
:stress :stress
:all (constantly true)}
:profiles {:travis {:jvm-opts ^:replace ["-server" "-Xmx1g"]}
:bench {:jvm-opts ^:replace ["-server" "-Xmx10g" "-XX:+UseParallelGC"]}
:dev {:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/test.check "0.9.0"]
[criterium "0.4.3"]
[potemkin "0.4.3"]
[proteus "0.1.6"]
[byte-streams "0.2.2"]
[eftest "0.1.4"]]}}
:aliases {"partest" ["run" "-m" "bifurcan.run-tests"]
:bench {:jvm-opts ^:replace ["-server" "-Xmx10g" "-XX:+UseParallelGC"]}
:dev {:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/test.check "0.9.0"]
[criterium "0.4.3"]
[potemkin "0.4.3"]
[proteus "0.1.6"]
[byte-streams "0.2.2"]
[eftest "0.1.4"]
[virgil "0.1.7-alpha1"]]}}
:aliases {"partest" ["run" "-m" "bifurcan.run-tests"]
"benchmark" ["with-profile" "bench,dev" "run" "-m" "bifurcan.benchmark-test" "benchmark"]}
:jvm-opts ^:replace ["-server" "-XX:+UseG1GC" "-Xmx10g" "-XX:-OmitStackTraceInFastThrow"])
:jvm-opts ^:replace ["-server" "-XX:+UseG1GC" "-Xmx10g" "-XX:-OmitStackTraceInFastThrow"]

;; deployment
:url "https://github.com/lacuna/bifurcan"
:description "impure functional data structures"
:license {:name "MIT License"}
:javac-options ["-target" "1.8" "-source" "1.8"]
:deploy-repositories {"releases" {:url "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
:creds :gpg}
"snapshots" {:url "https://oss.sonatype.org/content/repositories/snapshots/"
:creds :gpg}}

;; Maven properties for the Maven God
:scm {:url "[email protected]:lacuna/bifurcan.git"}
:pom-addition [:developers [:developer
[:name "Zach Tellman"]
[:url "http://ideolalia.com"]
[:email "[email protected]"]
[:timezone "-8"]]]
:classifiers {:javadoc {:java-source-paths ^:replace []
:source-paths ^:replace []
:resource-paths ^:replace ["javadoc"]}
:sources {:java-source-paths ^:replace ["src"]
:resource-paths ^:replace []}})
14 changes: 12 additions & 2 deletions src/io/lacuna/bifurcan/IMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -178,6 +179,15 @@ default IMap<K, V> put(K key, V value, BinaryOperator<V> merge) {
return new VirtualMap<>(this).put(key, value, merge);
}

/**
* @param update a function which takes the existing value, or {@code null} if none exists, and returns an updated
* value.
* @return an updated map with {@code update(value)} under {@code key}.
*/
default IMap<K, V> update(K key, Function<V, V> update) {
return this.put(key, update.apply(this.get(key, null)));
}

/**
* @return an updated map with {@code value} stored under {@code key}
*/
Expand All @@ -194,12 +204,12 @@ default IMap<K, V> remove(K key) {

@Override
default IMap<K, V> forked() {
return null;
return this;
}

@Override
default IMap<K, V> linear() {
return null;
return new VirtualMap<>(this).linear();
}

@Override
Expand Down
9 changes: 8 additions & 1 deletion src/io/lacuna/bifurcan/LinearList.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ private LinearList(int size, Object[] elements) {
this.elements = elements;
}

public static <V> LinearList<V> from(V... elements) {
LinearList<V> list = new LinearList<V>(elements.length);
for (V e : elements) {
list.addLast(e);
}
return list;
}

/**
* @param collection a {@code java.util.Collection}
* @return a list containing the entries of the collection
Expand Down Expand Up @@ -136,7 +144,6 @@ public LinearList<V> removeFirst() {
if (size == 0) {
return this;
}
elements[offset] = null;
offset = (offset + 1) & mask;
size--;
return this;
Expand Down
14 changes: 14 additions & 0 deletions src/io/lacuna/bifurcan/LinearMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ public V get(K key, V defaultValue) {
}
}

@Override
public IMap<K, V> update(K key, Function<V, V> update) {
int idx = tableIndex(keyHash(key), key);
if (idx >= 0) {
long row = table[idx];
int valIdx = Row.keyIndex(row) + 1;
entries[valIdx] = update.apply((V) entries[valIdx]);
} else {
put(key, update.apply(null));
}

return this;
}

@Override
public IList<IEntry<K, V>> entries() {
return Lists.from(
Expand Down
8 changes: 8 additions & 0 deletions src/io/lacuna/bifurcan/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public List() {
this.suffix = suffix;
}

public static <V> List<V> from(V... elements) {
List<V> list = new List<V>().linear();
for (V e : elements) {
list.addLast(e);
}
return list.forked();
}

public static <V> List<V> from(IList<V> list) {
if (list instanceof List) {
return ((List<V>) list).forked();
Expand Down
1 change: 1 addition & 0 deletions src/io/lacuna/bifurcan/nodes/ListNodes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.lacuna.bifurcan.nodes;

import io.lacuna.bifurcan.LinearList;
import io.lacuna.bifurcan.utils.Bits;

import java.util.Iterator;

Expand Down
2 changes: 1 addition & 1 deletion src/io/lacuna/bifurcan/nodes/MapNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public Node<K, V> put(int shift, Object editor, int hash, K key, V value, BiPred
} else if (n.isNode(mask)) {
INode<K, V> child = n.node(mask);

// since we're not changing anything at this left, just head down
// since we're not changing anything at this level, just head down
if (child instanceof Node && ((Node<K, V>) child).editor == editor) {
n = (Node<K, V>) child;
currShift += SHIFT_INCREMENT;
Expand Down
11 changes: 11 additions & 0 deletions src/io/lacuna/bifurcan/nodes/RopeNodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.lacuna.bifurcan.nodes;


/**
* @author ztellman
*/
public class RopeNodes {



}
Loading

0 comments on commit 87241af

Please sign in to comment.