public boolean add(T o) { boolean added = true; elements.add(o); while (size() > capacity()) { Object dumped = elements.extractMin(); if (dumped.equals(o)) { added = false; } } return added; }
public List<T> asSortedList() { LinkedList<T> list = new LinkedList<T>(); for (Iterator<T> i = elements.iterator(); i.hasNext(); ) { list.addFirst(i.next()); } return list; }
public static void main(String[] args) { try { JSObject js1 = new JSObject(); JSObject js2 = new JSObject(); JSObject js3 = new JSObject(); js1.put("x", new ObjectValue("Y")); js2.put(new String("a"), new SecurityType(SecurityType.high, null)); js2.put(new String("u"), null); js3.put(new String("b"), null); // Testing the alloc function Heap h = new Heap(); Location l0 = h.alloc(Location.generateLocString(), js1); Location l1 = h.alloc("0", js1); Location l2 = h.alloc("10", js2); System.out.println("loc 1: " + l1); System.out.println("loc 2: " + l2); System.out.println("Heap : " + h); Heap h0 = h.deepClone(); Heap h1 = h.clone(); // Location l3 = h.alloc("5", js3); System.out.println("Heap comparison: " + h.equals(h0)); System.out.println("Heap comparison: " + h.equals(h1)); } catch (Exception e) { e.printStackTrace(); } }
public boolean isomorphic(Heap other) { if (this == other) return true; if (other == null) return false; if (getClass() != other.getClass()) return false; if (heap == null) { if (other.heap != null) return false; } else { if (heap.size() != other.getSize()) { // if the size of both the maps is not the same, then return false return false; } else { // keyset sizes match // less optimization // return checkHeapObjectEquality(other); // max optimization return checkContentEquality(other, this.getGlobal(), other.getGlobal()); } } return true; }
/** * @param other * @return */ private boolean checkHeapObjectEquality(Heap other) { Set<Location> this_keyset = this.getKeySet(); Set<Location> other_keyset = other.getKeySet(); if (this_keyset != null && other_keyset != null) { if (this_keyset.containsAll(other_keyset) && other_keyset.containsAll( this_keyset)) { // check if both the keysets have the same keys // If true, compare value sets boolean retval = true; for (Location key : this_keyset) { JSObject this_value = this.get(key); JSObject other_value = other.get(key); retval = retval && (this_value.isomorphic(other_value)); } return retval; } else { // If false, the maps don't match return false; } } else { if (this_keyset == null && other_keyset == null) return true; else return false; } }
public static void main(String[] args) { Heap<String> h = new Heap<String>(); h.insert("p"); h.insert("r"); h.insert("i"); h.insert("o"); System.out.println(h); h.deleteMin(); System.out.println(h); Heap<Integer> tmp = new Heap<Integer>(); Integer[] a = {4, 7, 7, 7, 5, 0, 2, 3, 5, 1}; tmp.heapSort(a); System.out.println(Arrays.toString(a)); }
/** * Create a deep clone of the heap and return it * * @return */ public Heap deepClone() { Heap hp = new Heap(); if (this.heap == null) { hp.setHeap(null); return hp; } // set the clone global element hp.setGlobal(this.getGlobal()); Set<Location> keys = this.getKeySet(); for (Location key : keys) { JSObject old_ov = this.get(key); // create a deep clone of the old object value JSObject new_ov = old_ov.deepClone(); hp.put(key, new_ov); } hp.setStrValueMap((HashMap<String, Location>) this.strValueMap.clone()); hp.setDummyObjectMap((HashMap<String, Location>) this.dummyObjectMap.clone()); return hp; }
public int size() { return elements.size(); }
/** * Recursive function to check if the JSObjects in both the heaps match * * @param other * @param this_global * @param other_global * @return */ private boolean checkContentEquality(Heap other, Location this_obj, Location other_obj) { JSObject curr_obj = this.get(this_obj); JSObject test_obj = other.get(other_obj); if (curr_obj == null && test_obj == null) return true; if ((curr_obj != null && test_obj == null) || (curr_obj == null && test_obj != null)) return false; SecurityType curr_taint = (SecurityType) curr_obj.get("at_Taint"); SecurityType test_taint = (SecurityType) test_obj.get("at_Taint"); // Proceed further only if the JSObject taints match if (curr_taint.equals(test_taint)) { // If the location is a dummy object, it suffices to just check the taints if (other.isDummy(other_obj) && this.isDummy(this_obj)) { return true; } Set<String> this_keyset = curr_obj.getKeySet(); Set<String> other_keyset = test_obj.getKeySet(); // Proceed further only if the keys in both the JSObjects match if (this_keyset.containsAll(other_keyset) && other_keyset.containsAll(this_keyset)) { // For each key compare the object values boolean retval = true; for (String key : this_keyset) { // we don't want to go into libraryProperties as they will lead to the parents // creating a circular check and infinite recursion if (ProgramAnalyzerC.libraryProperties.contains(key)) continue; if (key.equals("at_Taint")) // we already compared taints continue; ObjectValue this_val = curr_obj.get(key); ObjectValue other_val = test_obj.get(key); if (this_val == null && other_val == null) { continue; } if ((this_val == null && other_val != null) || (this_val != null && other_val == null)) { retval = false; return retval; } if (this_val.getClass() != other_val.getClass()) { retval = false; // If the class types don't match return retval; } if (this_val instanceof Location) retval = retval && checkContentEquality(other, (Location) this_val, (Location) other_val); if (this_val instanceof FunctionValue) retval = retval && ((FunctionValue) this_val).equals((FunctionValue) other_val); if (this_val instanceof ObjectValue) retval = retval && this_val.equals(other_val); if (retval == false) return false; } return retval; } } return false; }
/** * This main program is started like this: * * <p>java HeapStuff X V0 V1 V2 V3 ... * * <p>It puts the strings Vi into a heap, and then prints out all values that are >= X (String * comparison). */ public static void main(String[] args) { Heap<String> H = new Heap<String>(); for (int i = 0; i < args.length; i += 1) H.add(args[i]); H.printLarger(args[0]); }