📄 objecttree.java
字号:
if (n == null) rightUsed = false; inserted = true; } if (size() == 0) return null; else if (inserted) return this; /* nodes are full, therefore must change to different node type */ Node replacementNode = new NarrowNode(); SimpleByteArray dummy = new SimpleByteArray(1); dummy.setByte(0, leftIndex); replacementNode = replacementNode.setNode(leftNode, dummy, 0, 0); dummy.setByte(0, rightIndex); replacementNode = replacementNode.setNode(rightNode, dummy, 0, 0); replacementNode = replacementNode.setNode(n, mem, offset, maxOffset); return replacementNode; } public Node getNode(ByteArray mem, int offset) { byte index = mem.getByte(offset); if ((leftIndex == index) && (leftUsed)) return leftNode; else if ((rightIndex == index) && (rightUsed)) return rightNode; else return null; } public boolean visitNodes(ObjectTreeVisitor visitor) { if (!super.visitNodes(visitor)) return false; if (rightNode != null) { if (!rightNode.visitNodes(visitor)) return false; } if (leftNode != null) { if (!leftNode.visitNodes(visitor)) return false; } return true; } public void print(String indent) { System.out.println(indent+"Binary["+leftIndex+" "+rightIndex+"]"); if (leftNode != null) leftNode.print(indent+" "); if (rightNode != null) rightNode.print(indent+" "); } } public static class SingularNode extends Node { private Node childNode; private byte childIndex; private boolean childUsed; /** Create a new single node */ SingularNode() { childUsed = false; } public int size() { if (childUsed) return 1; return 0; } public Node setNode(Node n, ByteArray mem, int offset, int maxOffset) { byte index = mem.getByte(offset); if (!childUsed || (childUsed && (childIndex == index))) { // if only child node is to be null, no point in having this node! if (n == null) return null; childIndex = index; childNode = n; childUsed = true; return this; } /* nodes are full, therefore must change to different node type */ Node replacementNode = new BinaryNode(); SimpleByteArray dummy = new SimpleByteArray(1); dummy.setByte(0, childIndex); replacementNode = replacementNode.setNode(childNode, dummy, 0, 0); replacementNode = replacementNode.setNode(n, mem, offset, maxOffset); return replacementNode; } public Node getNode(ByteArray mem, int offset) { if (childIndex == mem.getByte(offset)) return childNode; else return null; } public boolean visitNodes(ObjectTreeVisitor visitor) { if (!super.visitNodes(visitor)) return false; if (childNode != null) { if (!childNode.visitNodes(visitor)) return false; } return true; } public void print(String indent) { System.out.println(indent+"SINGLE!!!!"); } } public static class SequenceNode extends Node { byte[] sequence; Node child; public SequenceNode(Node child, ByteArray arr, int offset, int maxOffset) { this.child = child; sequence = new byte[maxOffset - offset + 1]; arr.copyContentsInto(offset, sequence, 0, sequence.length); } public int size() { return 1; } public Node setNode(Node n, ByteArray mem, int offset, int maxOffset) { for (int i=0; i<sequence.length; i++) { if (mem.getByte(offset + i) == sequence[i]) continue; BinaryNode branch = new BinaryNode(); SimpleByteArray dummy = new SimpleByteArray(sequence); Node left = null; if (i+1 == sequence.length) left = child; else left = new SequenceNode(child, dummy, i+1, sequence.length-1); Node right = null; if (offset+i == maxOffset) right = n; else right = new SequenceNode(n, mem, offset+i+1, maxOffset); branch.setNode(left, dummy, i, i); branch.setNode(right, mem, offset+i, offset+i); if (i == 0) return branch; return new SequenceNode(branch, dummy, 0, i-1); } if (n == null) return null; child = n; return this; } public Node getNode(ByteArray mem, int offset) { for (int i=0; i<sequence.length; i++) { if (mem.getByte(offset + i) == sequence[i]) continue; return null; } return child; } public Object getObject(ByteArray mem, int offset) { Node child = getNode(mem, offset); if (child == null) return null; return child.getObject(mem, offset+sequence.length); } public Node setObject(Object obj, ByteArray mem, int offset, int maxOffset) { Node child = getNode(mem, offset); if (child == null) return setNode(new LeafNode(obj), mem, offset, maxOffset); Node result = child.setObject(obj, mem, offset+sequence.length, maxOffset); if (result == child) return this; return setNode(result, mem, offset, maxOffset); } public void print(String indent) { String arrayString = ""; if (sequence == null) arrayString = "[null]"; else { for (int i = 0; i < sequence.length; i++) arrayString += String.valueOf(sequence[i]) + ","; arrayString += "]"; } System.out.println(indent+"SEQ"+arrayString); child.print(indent+" "); } } public static class LeafNode extends Node { private int useageCount; private Object obj; /** Create a new leaf node */ LeafNode(Object obj) { useageCount = 1; this.obj = obj; } public Node setNode(Node n, ByteArray mem, int offset, int maxOffset) { throw new IllegalStateException("Setting node on a leaf"); } public Node getNode(ByteArray mem, int offset) { throw new IllegalStateException("Getting a node from a leaf"); } public Object getObject(ByteArray mem, int offset) { useageCount++; return obj; } public Node setObject(Object obj, ByteArray mem, int offset, int maxOffset) { useageCount = 1; this.obj = obj; return this; } public int size() { return 0; } public int getUsageCount() { return useageCount; } public Object peekObject() { return obj; } public void print(String indent) { System.out.println(indent+"Leaf["+obj+"]"); } } /** * Clear the entire tree. */ public void clearAllObjects() { root = new WideNode(); } /** * Get the Object that represents a x86 byte sequence (if it exists). * * @param mem memory holding a sequence of x86 bytes to search for * @param offset offset into memory of current x86 byte in sequence * @return a pointer to the code obj that represents the x86 sequnce or null if it doesn't exist */ public Object getObject(ByteArray mem, int offset) { return root.getObject(mem, offset); } /** * Sets the Object that represents a x86 byte sequence. * (overwrites if already exists) * * @param obj code obj representing the sequence of x86 bytes * @param mem memory holding a sequence of x86 bytes to search for * @param offset offset into memory of current x86 byte in sequence * @param length x86 length of obj */ public void setObject(ByteArray mem, int offset, Object obj, int length) { root = root.setObject(obj, mem, offset, offset + length - 1); } /** * Visits all nodes in the tree with a visitor * * @param visitor visitor for the nodes * @see ObjectTreeVisitor */ public void visitNodes(ObjectTreeVisitor visitor) { if (root != null) root.visitNodes(visitor); } public void printTree() { root.print(""); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -