X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Futil%2FBalancedTree.java;h=175ae33a3b10be5fd352427b9f098da11678b37b;hb=4ebc8fdd36b23d350924cc54b08fc74276249d0d;hp=52ac71be0b2089969c1af77fc517708df5d8f21a;hpb=eb8d7e6a8fa6091bc2e4999c21faee5cfccbfbf2;p=org.ibex.core.git diff --git a/src/org/xwt/util/BalancedTree.java b/src/org/xwt/util/BalancedTree.java index 52ac71b..175ae33 100644 --- a/src/org/xwt/util/BalancedTree.java +++ b/src/org/xwt/util/BalancedTree.java @@ -1,4 +1,10 @@ -// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright (C) 2003 Adam Megacz all rights reserved. +// +// You may modify, copy, and redistribute this code under the terms of +// the GNU Library Public License version 2.1, with the exception of +// the portion of clause 6a after the semicolon (aka the "obnoxious +// relink clause") + package org.xwt.util; // FEATURE: private void intersection() { } @@ -24,7 +30,7 @@ public class BalancedTree { public final int treeSize() { return root == 0 ? 0 : size[root]; } /** clamps index to [0..treeSize()] and inserts object o *before* the specified index */ - public final void insertNode(int index, Object o) { + public final synchronized void insertNode(int index, Object o) { cached_slot = cached_index = -1; if (index < 0) index = 0; if (index > treeSize()) index = treeSize(); @@ -40,7 +46,7 @@ public class BalancedTree { } /** clamps index to [0..treeSize()-1] and replaces the object at that index with object o */ - public final void replaceNode(int index, Object o) { + public final synchronized void replaceNode(int index, Object o) { cached_slot = cached_index = -1; if (index < 0) index = 0; if (index > treeSize()) index = treeSize() - 1; @@ -52,7 +58,7 @@ public class BalancedTree { } /** returns the index of o; runs in O((log n)^2) time unless cache hit */ - public final int indexNode(Object o) { + public final synchronized int indexNode(Object o) { if (cached_slot != -1 && objects[cached_slot] == o) return cached_index; int slot = getSlot(o, o.hashCode() ^ this.hashCode()); @@ -60,11 +66,11 @@ public class BalancedTree { if (parent == 0) return size(left[slot]); // we are on the far left edge // all nodes after parent and before us are in our left subtree - return size(left[slot]) + indexNode(objects[parent]) + 1; + return size(left[slot]) + (parent <= 0 ? 0 : indexNode(objects[parent])) + 1; } /** returns the object at index; runs in O(log n) time unless cache hit */ - public final Object getNode(int index) { + public final synchronized Object getNode(int index) { if (index == cached_index) return objects[cached_slot]; if (cached_index != -1) { @@ -87,7 +93,7 @@ public class BalancedTree { } /** deletes the object at index, returning the deleted object */ - public final Object deleteNode(int index) { + public final synchronized Object deleteNode(int index) { cached_slot = cached_index = -1; int del = delete(index, root, 0); left[del] = right[del] = size[del] = 0; @@ -304,7 +310,7 @@ public class BalancedTree { } else { int left_childs_rightmost = delete(size(left[slot]) - 1, left[slot], slot); left[left_childs_rightmost] = left[slot]; - left[left_childs_rightmost] = right[slot]; + right[left_childs_rightmost] = right[slot]; if (parent == 0) root = left_childs_rightmost; else (left[parent] == slot ? left : right)[parent] = left_childs_rightmost; // fix parent's pointer balance(left_childs_rightmost, parent);