stack = stack.plus(s); assertEquals("first element should be most recently added", s, stack.get(0)); assertTrue("stack.plus(x).contains(x) should be true!", stack.contains(s)); stack, reverse(stack2)); PStack<String> stack3 = stack.plusAll(stack); // palindrome stack assertEqualsAndHash("plusAll should reverse order", stack3.subList(0,stack.size()), stack2); assertEqualsAndHash(stack3.minusAll(stack3), ConsPStack.empty()); assertEqualsAndHash(stack3.minusAll(stack), ConsPStack.empty()); stack = stack.minus(s); assertTrue(!stack.contains(s));
private AmortizedPQueue(AmortizedPQueue<E> queue, E e) { /* Guarantee that there is always at least 1 element * in front, which makes peek worst-case O(1). */ if (queue.front.size() == 0) { this.front = queue.front.plus(e); this.back = queue.back; } else { this.front = queue.front; this.back = queue.back.plus(e); } }
public Entry<Integer,V> next() { IntTree<V> node = stack.get(0); final Entry<Integer,V> result = new SimpleImmutableEntry<Integer,V>(key, node.value); // find next node. // we've already done everything smaller, // so try least larger node: if(node.right.size>0) // we can descend to the right gotoMinOf(node.right); else // can't descend to the right -- try ascending to the right while (true) { // find current node's least larger ancestor, if any key -= node.key; // revert to parent's key stack = stack.subList(1); // climb up to parent // if parent was larger than child or there was no parent, we're done: if(node.key<0 || stack.size()==0) break; // otherwise parent was smaller -- try its parent: node = stack.get(0); } return result; }
public AmortizedPQueue<E> minus() { if (size() == 0) { return this; } int fsize = front.size(); if (fsize == 0) { //If there's nothing on front, dump back onto front //(as stacks, this goes in reverse like we want) //and take one off. return new AmortizedPQueue<E>(Empty.<E>stack().plusAll(back), Empty.<E>stack()).minus(); } else if (fsize == 1) { //If there's one element on front, dump back onto front, //but now we've already removed the head. return new AmortizedPQueue<E>(Empty.<E>stack().plusAll(back), Empty.<E>stack()); } else { //If there's more than one on front, we pop one off. return new AmortizedPQueue<E>(front.minus(0), back); } }
private PStack<Function> swapFn(Function fn){ if(functionList.size()==0) return functionList.plus(fn); Function before = functionList.get(functionList.size()-1); PStack<Function> removed = functionList.minus(functionList.size()-1); return removed.plus(removed.size(),fn); } private PStack<Function> swapComposeFn(Function fn){
Random r = new Random(); for(int i=0;i<1000;i++) { if(pstack.size()==0 || r.nextBoolean()) { // add if(r.nextBoolean()) { // append Integer v = r.nextInt(); assertEquals(list.contains(v), pstack.contains(v)); pstack = pstack.plus(v); int k = r.nextInt(pstack.size()+1); Integer v = r.nextInt(); assertEquals(list.contains(v), pstack.contains(v)); if(k<pstack.size()) assertEquals(list.get(k), pstack.get(k)); pstack = pstack.plus(k, v); int k = r.nextInt(pstack.size()); Integer v = r.nextInt(); list.set(k, v); pstack = pstack.with(k, v); int j = r.nextInt(pstack.size()), k=0; for(Integer e : pstack) { assertTrue(pstack.contains(e)); assertEquals(e, pstack.get(k));
public PStack<T> plusAll(Collection<? extends T> list) { return boxed.plusAll(list); } public PStack<T> with(int i, T e) {
/** Make sure the right element is removed */ public void testMinusInt() { // First, let's try a list with distinct elements PStack<String> pstack = ConsPStack.<String>empty().plus("C").plus("B").plus("A"); assertEquals(Arrays.asList("A", "B", "C"), pstack); assertEquals(Arrays.asList("B", "C"), pstack.minus(0)); assertEquals(Arrays.asList("A", "B"), pstack.minus(2)); //Now, let's try duplicates pstack = pstack.plus("B"); assertEquals(Arrays.asList("B", "A", "B", "C"), pstack); assertEquals(Arrays.asList("A", "B", "C"), pstack.minus(0)); assertEquals(Arrays.asList("B", "A", "C"), pstack.minus(2)); }
public E peek() { if(size()==0) return null; return front.get(0); }
private Stream<Case<T,R,X>> sequentialStream(){ return cases.stream(); }
public PStack<T> minus(Object e) { return boxed.minus(e); } public PStack<T> minusAll(Collection<?> list) {
private void gotoMinOf(IntTree<V> node) { while(node.size>0) { stack = stack.plus(node); key += node.key; node = node.left; } } }
public PStack<T> subList(int start, int end) { return boxed.subList(start, end); } public boolean add(T o) {
/** * @return * @see java.util.AbstractCollection#isEmpty() */ public boolean isEmpty() { return stack.isEmpty(); }