Refactor BodyForceTreeMap
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
*/
|
||||
public class BodyForceTreeMap {
|
||||
private int size = 0;
|
||||
private BodyForceTreeMapItem root = null;
|
||||
private Item root = null;
|
||||
|
||||
/**
|
||||
* Adds a new key-value association to this map. If the key already exists in this map,
|
||||
@ -13,30 +13,30 @@ public class BodyForceTreeMap {
|
||||
*/
|
||||
public Vector3 put(Body key, Vector3 value) {
|
||||
if (root == null) {
|
||||
root = new BodyForceTreeMapItem(key, value);
|
||||
root = new Item(key, value);
|
||||
size++;
|
||||
return null;
|
||||
}
|
||||
|
||||
BodyForceTreeMapItem item = root;
|
||||
Item item = root;
|
||||
while (item != null) {
|
||||
if (item.key() == key) {
|
||||
Vector3 old = item.value();
|
||||
item.setValue(value);
|
||||
if (item.key == key) {
|
||||
Vector3 old = item.value;
|
||||
item.value = value;
|
||||
return old;
|
||||
} else if (item.key().mass() > key.mass()) {
|
||||
if (item.left() != null) {
|
||||
item = item.left();
|
||||
} else if (item.key.mass() > key.mass()) {
|
||||
if (item.left != null) {
|
||||
item = item.left;
|
||||
} else {
|
||||
item.setLeft(new BodyForceTreeMapItem(key, value));
|
||||
item.setLeft(new Item(key, value));
|
||||
size++;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (item.right() != null) {
|
||||
item = item.right();
|
||||
if (item.right != null) {
|
||||
item = item.right;
|
||||
} else{
|
||||
item.setRight(new BodyForceTreeMapItem(key, value));
|
||||
item.setRight(new Item(key, value));
|
||||
size++;
|
||||
break;
|
||||
}
|
||||
@ -52,14 +52,14 @@ public class BodyForceTreeMap {
|
||||
* Precondition: key != null.
|
||||
*/
|
||||
public Vector3 get(Body key) {
|
||||
BodyForceTreeMapItem item = root;
|
||||
Item item = root;
|
||||
while (item != null) {
|
||||
if (item.key() == key) {
|
||||
return item.value();
|
||||
} else if (item.key().mass() > key.mass()) {
|
||||
item = item.left();
|
||||
if (item.key == key) {
|
||||
return item.value;
|
||||
} else if (item.key.mass() > key.mass()) {
|
||||
item = item.left;
|
||||
} else {
|
||||
item = item.right();
|
||||
item = item.right;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@ -69,14 +69,14 @@ public class BodyForceTreeMap {
|
||||
* Returns 'true' if this map contains a mapping for the specified key.
|
||||
*/
|
||||
public boolean containsKey(Body key) {
|
||||
BodyForceTreeMapItem item = root;
|
||||
Item item = root;
|
||||
while (item != null) {
|
||||
if (item.key() == key) {
|
||||
if (item.key == key) {
|
||||
return true;
|
||||
} else if (item.key().mass() > key.mass()) {
|
||||
item = item.left();
|
||||
} else if (item.key.mass() > key.mass()) {
|
||||
item = item.left;
|
||||
} else {
|
||||
item = item.right();
|
||||
item = item.right;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@ -86,14 +86,14 @@ public class BodyForceTreeMap {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
private String toString(BodyForceTreeMapItem item) {
|
||||
private String toString(Item item) {
|
||||
String s = "";
|
||||
if (item == null) {
|
||||
return s;
|
||||
}
|
||||
s += this.toString(item.right());
|
||||
s += String.format("{%s: %s}\n", item.key(), item.value());
|
||||
s += this.toString(item.left());
|
||||
s += this.toString(item.right);
|
||||
s += String.format("{%s: %s}\n", item.key, item.value);
|
||||
s += this.toString(item.left);
|
||||
return s;
|
||||
}
|
||||
|
||||
@ -105,51 +105,27 @@ public class BodyForceTreeMap {
|
||||
public String toString() {
|
||||
return toString(root);
|
||||
}
|
||||
}
|
||||
|
||||
class BodyForceTreeMapItem {
|
||||
private class Item {
|
||||
private final Body key;
|
||||
private Vector3 value;
|
||||
private BodyForceTreeMapItem parent;
|
||||
private BodyForceTreeMapItem left;
|
||||
private BodyForceTreeMapItem right;
|
||||
private Item parent;
|
||||
private Item left;
|
||||
private Item right;
|
||||
|
||||
public BodyForceTreeMapItem(Body key, Vector3 value) {
|
||||
public Item(Body key, Vector3 value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Body key() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
public void setValue(Vector3 value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Vector3 value() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public BodyForceTreeMapItem left() {
|
||||
return this.left;
|
||||
}
|
||||
|
||||
public BodyForceTreeMapItem right() {
|
||||
return this.right;
|
||||
}
|
||||
|
||||
public BodyForceTreeMapItem parent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
public void setLeft(BodyForceTreeMapItem left) {
|
||||
public void setLeft(Item left) {
|
||||
this.left = left;
|
||||
if (left != null) left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BodyForceTreeMapItem right) {
|
||||
public void setRight(Item right) {
|
||||
this.right = right;
|
||||
if (right != null) right.parent = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user