75 lines
1.8 KiB
Java
75 lines
1.8 KiB
Java
package Models.Program;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
import Exceptions.BaseException;
|
|
import Models.Values.IValue;
|
|
|
|
public class Heap implements IHeap{
|
|
private Integer freeAddress;
|
|
private Hashtable<Integer, IValue> heap;
|
|
|
|
public Heap() {
|
|
this.freeAddress = 1;
|
|
this.heap = new Hashtable<Integer, IValue>();
|
|
}
|
|
|
|
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<Integer, IValue> getContent() {
|
|
return this.heap;
|
|
}
|
|
|
|
public void setContent(Hashtable<Integer, IValue> content) {
|
|
this.heap = content;
|
|
}
|
|
|
|
}
|