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