Skip to content

Commit

Permalink
Lots of changes for AU press formatting and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
patmorin committed Jun 3, 2013
1 parent 9230cd1 commit 637219d
Show file tree
Hide file tree
Showing 131 changed files with 4,596 additions and 815 deletions.
3 changes: 2 additions & 1 deletion java/junk/GeomVector.java
Expand Up @@ -21,7 +21,8 @@ public int hashCode() {
long s = 0;
long zi = 1;
for (int i = 0; i < x.length; i++) {
long xi = (x[i].hashCode() * z2) >>> 1; // reduce to 31 bits
// reduce to 31 bits
long xi = (x[i].hashCode() * z2) >>> 1;
s = (s + zi * xi) % p;
zi = (zi * z) % p;
}
Expand Down
13 changes: 8 additions & 5 deletions java/junk/Point3D.java
Expand Up @@ -9,14 +9,17 @@ public boolean equals(Object o) {
}

public int hashCode() {
long[] z = {0x2058cc50L, 0xcb19137eL, 0x2cb6b6fdL}; // random
long zz = 0xbea0107e5067d19dL; // random

long h0 = x0.hashCode() & ((1L<<32)-1); // unsigned int to long
// random numbers from rand.org
long[] z = {0x2058cc50L, 0xcb19137eL, 0x2cb6b6fdL};
long zz = 0xbea0107e5067d19dL;

// convert (unsigned) hashcodes to long
long h0 = x0.hashCode() & ((1L<<32)-1);
long h1 = x1.hashCode() & ((1L<<32)-1);
long h2 = x2.hashCode() & ((1L<<32)-1);

return (int)(((z[0]*h0 + z[1]*h1 + z[2]*h2)*zz) >>> 32);
return (int)(((z[0]*h0 + z[1]*h1 + z[2]*h2)*zz)
>>> 32);
}

public static void main(String[] args) {
Expand Down
8 changes: 4 additions & 4 deletions java/ods/ArrayDeque.java
Expand Up @@ -75,11 +75,11 @@ public T set(int i, T x) {
public void add(int i, T x) {
if (i < 0 || i > n) throw new IndexOutOfBoundsException();
if (n+1 > a.length) resize();
if (i < n/2) { // shift a[0],..,a[i-1] left one position
j = (j == 0) ? a.length - 1 : j - 1; // (j-1) mod a.length
if (i < n/2) { // shift a[0],..,a[i-1] left one position
j = (j == 0) ? a.length - 1 : j - 1; //(j-1)mod a.length
for (int k = 0; k <= i-1; k++)
a[(j+k)%a.length] = a[(j+k+1)%a.length];
} else { // shift a[i],..,a[n-1] right one position
} else { // shift a[i],..,a[n-1] right one position
for (int k = n; k > i; k--)
a[(j+k)%a.length] = a[(j+k-1)%a.length];
}
Expand All @@ -94,7 +94,7 @@ public T remove(int i) {
for (int k = i; k > 0; k--)
a[(j+k)%a.length] = a[(j+k-1)%a.length];
j = (j + 1) % a.length;
} else { // shift a[i+1],..,a[n-1] left one position
} else { // shift a[i+1],..,a[n-1] left one position
for (int k = i; k < n-1; k++)
a[(j+k)%a.length] = a[(j+k+1)%a.length];
}
Expand Down
4 changes: 2 additions & 2 deletions java/ods/BTree.java
Expand Up @@ -371,7 +371,7 @@ protected void merge(Node u, int i, Node v, Node w) {
protected void checkUnderflowNonZero(Node u, int i) {
Node w = bs.readBlock(u.children[i]); // w is child of u
if (w.size() < B-1) { // underflow at w
Node v = bs.readBlock(u.children[i-1]); // v is left sibling of w
Node v = bs.readBlock(u.children[i-1]); // v left of w
if (v.size() > B) { // w can borrow from v
shiftLR(u, i-1, v, w);
} else { // v will absorb w
Expand Down Expand Up @@ -412,7 +412,7 @@ protected void shiftLR(Node u, int i, Node v, Node w) {
protected void checkUnderflowZero(Node u, int i) {
Node w = bs.readBlock(u.children[i]); // w is child of u
if (w.size() < B-1) { // underflow at w
Node v = bs.readBlock(u.children[i+1]); // v is right sibling of w
Node v = bs.readBlock(u.children[i+1]); // v right of w
if (v.size() > B) { // w can borrow from v
shiftRL(u, i, v, w);
} else { // w will absorb w
Expand Down
4 changes: 2 additions & 2 deletions java/ods/BinaryTrie.java
Expand Up @@ -93,8 +93,8 @@ public boolean add(T x) {
if (u.child[c] == null) break;
u = u.child[c];
}
if (i == w) return false; // trie already contains x - abort
Node pred = (c == right) ? u.jump : u.jump.child[0]; // save for step 3
if (i == w) return false; // already contains x - abort
Node pred = (c == right) ? u.jump : u.jump.child[0];
u.jump = null; // u will have two children shortly
// 2 - add path to ix
for (; i < w; i++) {
Expand Down
10 changes: 5 additions & 5 deletions java/ods/LinearHashTable.java
Expand Up @@ -87,7 +87,7 @@ public boolean addSlow(T x) {
int i = hash(x);
while (t[i] != null) {
if (t[i] != del && x.equals(t[i])) return false;
i = (i == t.length-1) ? 0 : i + 1; // increment i (mod t.length)
i = (i == t.length-1) ? 0 : i + 1; // increment i
}
t[i] = x;
n++; q++;
Expand All @@ -100,7 +100,7 @@ public boolean add(T x) {
if (2*(q+1) > t.length) resize(); // max 50% occupancy
int i = hash(x);
while (t[i] != null && t[i] != del)
i = (i == t.length-1) ? 0 : i + 1; // increment i (mod t.length)
i = (i == t.length-1) ? 0 : i + 1; // increment i
if (t[i] == null) q++;
n++;
t[i] = x;
Expand All @@ -118,7 +118,7 @@ public boolean add2(T x) {
while (t[i] != null) {
if (t[i] == del && j == -1) j = i;
if (t[i] != del && t[i].equals(x)) return false;
i = (i == t.length-1) ? 0 : i + 1; // increment i (mod t.length)
i = (i == t.length-1) ? 0 : i + 1; // increment i
}
t[j == -1 ? i : j] = x;
n++; q++;
Expand All @@ -131,7 +131,7 @@ public T find(T x) {
int i = hash(x);
while (t[i] != null) {
if (t[i] != del && x.equals(t[i])) return t[i];
i = (i == t.length-1) ? 0 : i + 1; // increment i (mod t.length)
i = (i == t.length-1) ? 0 : i + 1; // increment i
}
return null;
}
Expand All @@ -146,7 +146,7 @@ public T remove(T x) {
if (8*n < t.length) resize(); // min 12.5% occupancy
return y;
}
i = (i == t.length-1) ? 0 : i + 1; // increment i (mod t.length)
i = (i == t.length-1) ? 0 : i + 1; // increment i
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion java/ods/RedBlackTree.java
Expand Up @@ -146,7 +146,7 @@ protected void removeFixup(Node<T> u) {
u = removeFixupCase3(u);
}
}
if (u != r) { // restore left-leaning property, if necessary
if (u != r) { // restore left-leaning property if needed
Node<T> w = u.parent;
if (w.right.color == red && w.left.color == black) {
flipLeft(w);
Expand Down
8 changes: 4 additions & 4 deletions java/ods/SEList.java
Expand Up @@ -176,14 +176,14 @@ public void add(int i, T x) {
u = u.next;
r++;
}
if (r == b) { // found b blocks each with b+1 elements
if (r == b) { // b blocks each with b+1 elements
spread(l.u);
u = l.u;
}
if (u == dummy) { // ran off the end of the list - add new node
if (u == dummy) { // ran off the end - add new node
u = addBefore(u);
}
while (u != l.u) { // work backwards, shifting an element at each step
while (u != l.u) { // work backwards, shifting elements
u.d.add(0, u.prev.d.remove(u.prev.d.size()-1));
u = u.prev;
}
Expand All @@ -201,7 +201,7 @@ public T remove(int i) {
u = u.next;
r++;
}
if (r == b) { // found b blocks each with b-1 elements
if (r == b) { // b blocks each with b-1 elements
gather(l.u);
}
u = l.u;
Expand Down
4 changes: 2 additions & 2 deletions java/ods/SkiplistList.java
Expand Up @@ -71,7 +71,7 @@ public SkiplistList() {
protected Node findPred(int i) {
Node u = sentinel;
int r = h;
int j = -1; // the index of the current node in list 0
int j = -1; // index of the current node in list 0
while (r >= 0) {
while (u.next[r] != null && j + u.length[r] < i) {
j += u.length[r];
Expand Down Expand Up @@ -111,7 +111,7 @@ protected Node add(int i, Node w) {
j += u.length[r];
u = u.next[r];
}
u.length[r]++; // to account for new node in list 0
u.length[r]++; // accounts for new node in list 0
if (r <= k) {
w.next[r] = u.next[r];
u.next[r] = w;
Expand Down
10 changes: 6 additions & 4 deletions java/ods/SkiplistSSet.java
Expand Up @@ -153,14 +153,15 @@ public boolean remove(T x) {
int r = h;
int comp = 0;
while (r >= 0) {
while (u.next[r] != null && (comp = c.compare(u.next[r].x, x)) < 0) {
while (u.next[r] != null
&& (comp = c.compare(u.next[r].x, x)) < 0) {
u = u.next[r];
}
if (u.next[r] != null && comp == 0) {
removed = true;
u.next[r] = u.next[r].next[r];
if (u == sentinel && u.next[r] == null)
h--; // skiplist height has gone down
h--; // height has gone down
}
r--;
}
Expand Down Expand Up @@ -241,14 +242,15 @@ public boolean add(T x) {
int r = h;
int comp = 0;
while (r >= 0) {
while (u.next[r] != null && (comp = c.compare(u.next[r].x,x)) < 0)
while (u.next[r] != null
&& (comp = c.compare(u.next[r].x,x)) < 0)
u = u.next[r];
if (u.next[r] != null && comp == 0) return false;
stack[r--] = u; // going down, store u
}
Node<T> w = new Node<T>(x, pickHeight());
while (h < w.height())
stack[++h] = sentinel; // increasing height of skiplist
stack[++h] = sentinel; // height increased
for (int i = 0; i < w.next.length; i++) {
w.next[i] = stack[i].next[i];
stack[i].next[i] = w;
Expand Down
6 changes: 4 additions & 2 deletions java/ods/XFastTrie.java
Expand Up @@ -129,8 +129,10 @@ public T find(T x) {
}
}
if (l == w) return u.x;
Node pred = (((ix >>> w-l-1) & 1) == 1) ? u.jump : u.jump.child[0];
return (pred.child[next] == dummy) ? null : pred.child[next].x;
Node pred = (((ix >>> w-l-1) & 1) == 1)
? u.jump : u.jump.child[0];
return (pred.child[next] == dummy)
? null : pred.child[next].x;
}

public static void main(String[] args) {
Expand Down

0 comments on commit 637219d

Please sign in to comment.