package Models.Program; import java.util.Hashtable; import Exceptions.BaseException; import Models.Values.IValue; public class Heap implements IHeap{ private Integer freeAddress; private Hashtable heap; public Heap() { this.freeAddress = 1; this.heap = new Hashtable(); } public Integer getFreeAddress() { return this.freeAddress; } public Boolean contains(Integer address) { return this.heap.containsKey(address); } public Integer add(IValue value) { this.heap.put(this.freeAddress, value); this.freeAddress += 1; return this.freeAddress - 1; } public void update(Integer address, IValue value) throws BaseException { if (!this.heap.containsKey(address)) { throw new BaseException("Address does not exist in heap."); } this.heap.put(address, value); } public IValue get(Integer address) throws BaseException { if (!this.heap.containsKey(address)) { throw new BaseException("Address does not exist in heap."); } return this.heap.get(address); } public void remove(Integer address) throws BaseException { if (!this.heap.containsKey(address)) { throw new BaseException("Address does not exist in heap."); } this.heap.remove(address); } public String toString() { var sb = new StringBuilder(); for (var key : this.heap.keySet()) { sb.append(key); sb.append(" --> "); sb.append(this.heap.get(key)); sb.append("\n"); } return sb.toString(); } public Hashtable getContent() { return this.heap; } public void setContent(Hashtable content) { this.heap = content; } }