Skip to content

Instantly share code, notes, and snippets.

@boustrophedon
Created October 8, 2014 21:11
Show Gist options
  • Save boustrophedon/10979993e0a246288a6c to your computer and use it in GitHub Desktop.
Save boustrophedon/10979993e0a246288a6c to your computer and use it in GitHub Desktop.
splay tree map w/ values as comparators
import 'dart:core';
import 'dart:collection';
class Test {
SplayTreeMap m;
Test() {
m = new SplayTreeMap<String, RTest>(_compare);
}
int _compare(String s1, String s2) {
return m[s1].compareTo(m[s2]);
}
void add(String s, RTest t){
m[s] = t;
}
void printAll() {
for (String s in m.keys) {
print(s);
}
}
}
class RTest implements Comparable {
int z;
RTest(int z) {
this.z = z;
}
int compareTo(RTest other) {
return this.z - other.z;
}
}
void main() {
Test t = new Test();
RTest r1 = new RTest(6);
RTest r2 = new RTest(-5);
RTest r3 = new RTest(4);
RTest r4 = new RTest(-20);
t.add("a",r1);
t.add("b",r2);
t.add("c",r3);
t.add("d",r4);
// order should be "d", "b", "c", "a"
t.printAll();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment