| 25 | import wycc.lang.Syntactic.Item; |
| 26 | |
| 27 | public abstract class AbstractHeap implements Syntactic.Heap { |
| 28 | /** |
| 29 | * The list of syntactic items contained in this heap. |
| 30 | */ |
| 31 | protected final ArrayList<Item> syntacticItems = new ArrayList<>(); |
| 32 | |
| 33 | /** |
| 34 | * The root item for this heap. |
| 35 | */ |
| 36 | protected int root; |
| 37 | |
| 38 | public AbstractHeap() { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | public AbstractHeap(Syntactic.Heap heap) { |
| 43 | // Copy over the root |
| 44 | this.root = heap.getRootItem().getIndex(); |
| 45 | // Now, clone items from heap in here |
| 46 | Allocator allocator = new Allocator(this); |
| 47 | // |
| 48 | for (int i = 0; i != heap.size(); ++i) { |
| 49 | Item oitem = heap.getSyntacticItem(i); |
| 50 | Item item = clone(oitem, allocator.map); |
| 51 | allocator.allocate(item); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public int size() { |
| 57 | return syntacticItems.size(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Item getRootItem() { |
| 62 | return getSyntacticItem(root); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void setRootItem(Item item) { |
| 67 | this.root = allocate(item).getIndex(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Item getSyntacticItem(int index) { |
| 72 | return syntacticItems.get(index); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int getIndexOf(Item item) { |
| 77 | for (int i = 0; i != syntacticItems.size(); ++i) { |
| 78 | if (syntacticItems.get(i) == item) { |
| 79 | return i; |
| 80 | } |
| 81 | } |
| 82 | throw new IllegalArgumentException("invalid syntactic item"); |
| 83 | } |
| 84 |
nothing calls this directly
no outgoing calls
no test coverage detected