View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.example.stack;
28  
29  import java.util.LinkedList;
30  
31  /**
32   * <h1>FastLongStack</h1>
33   * <p>
34   * Unsynchronized stack optimized for long primitive type. Is significantly faster than a normal Stack.
35   * </p>
36   * 
37   * @author Marc Kramis, University of Konstanz / Seabix
38   */
39  public final class FastIntStack {
40  
41      /** Internal linked list to store stack elements. */
42      private transient LinkedList<Integer> stack;
43  
44      /**
45       * Constructor.
46       */
47      public FastIntStack() {
48          stack = new LinkedList<Integer>();
49      }
50  
51      /**
52       * Place new element on top of stack.
53       * 
54       * @param element
55       *            Element to push.
56       */
57      public void push(final int element) {
58          stack.addLast(element);
59      }
60  
61      /**
62       * Get the element on top of the stack. The internal array performs boundary
63       * checks.
64       * 
65       * @return Topmost stack element.
66       */
67      public int peek() {
68          return stack.getLast().intValue();
69      }
70  
71      /**
72       * Get element at given position in stack. The internal array performs
73       * boundary checks.
74       * 
75       * @param position
76       *            Position in stack from where to get the element.
77       * @return Stack element at given position.
78       */
79      public int get(final int position) {
80          return stack.get(position);
81      }
82  
83      /**
84       * Remove topmost element from stack.
85       * 
86       * @return Removed topmost element of stack.
87       */
88      public int pop() {
89          int last = stack.getLast();
90          stack.removeLast();
91          return last;
92      }
93  
94      /**
95       * Reset the stack.
96       */
97      public void clear() {
98          stack.clear();
99      }
100 
101     /**
102      * Get the current stackSize of the stack.
103      * 
104      * @return stackSize of stack.
105      */
106     public int size() {
107         return stack.size();
108     }
109 
110 }