]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/gnu/trove/TIntStack.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / gnu / trove / TIntStack.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 ///////////////////////////////////////////////////////////////////////////////
18
19
20 package gnu.trove;
21
22 /**
23  * A stack of int primitives, backed by a TIntArrayList.
24  *
25  * @author Eric D. Friedman, Rob Eden
26  * @version $Id: PStack.template,v 1.2 2007/02/28 23:03:57 robeden Exp $
27  */
28
29 public class TIntStack {
30
31     /** the list used to hold the stack values. */
32     protected TIntArrayList _list;
33
34     public static final int DEFAULT_CAPACITY = TIntArrayList.DEFAULT_CAPACITY;
35
36     /**
37      * Creates a new <code>TIntStack</code> instance with the default
38      * capacity.
39      */
40     public TIntStack() {
41         this(DEFAULT_CAPACITY);
42     }
43
44     /**
45      * Creates a new <code>TIntStack</code> instance with the
46      * specified capacity.
47      *
48      * @param capacity the initial depth of the stack
49      */
50     public TIntStack(int capacity) {
51         _list = new TIntArrayList(capacity);
52     }
53
54     /**
55      * Pushes the value onto the top of the stack.
56      *
57      * @param val an <code>int</code> value
58      */
59     public void push(int val) {
60         _list.add(val);
61     }
62
63     /**
64      * Removes and returns the value at the top of the stack.
65      *
66      * @return an <code>int</code> value
67      */
68     public int pop() {
69         return _list.remove(_list.size() - 1);
70     }
71
72     /**
73      * Returns the value at the top of the stack.
74      *
75      * @return an <code>int</code> value
76      */
77     public int peek() {
78         return _list.get(_list.size() - 1);
79     }
80
81     /**
82      * Returns the current depth of the stack.
83      */
84     public int size() {
85         return _list.size();
86     }
87
88     /**
89      * Clears the stack, reseting its capacity to the default.
90      */
91     public void clear() {
92         _list.clear(DEFAULT_CAPACITY);
93     }
94
95     /**
96      * Clears the stack without releasing its internal capacity allocation.
97      */
98     public void reset() {
99         _list.reset();
100     }
101
102     /**
103      * Copies the contents of the stack into a native array. Note that this will NOT
104      * pop them out of the stack.
105      *
106      * @return an <code>int[]</code> value
107      */
108     public int[] toNativeArray() {
109         return _list.toNativeArray();
110     }
111
112     /**
113      * Copies a slice of the list into a native array. Note that this will NOT
114      * pop them out of the stack.
115      *
116      * @param dest the array to copy into.
117      */
118     public void toNativeArray(int[] dest) {
119         _list.toNativeArray(dest, 0, size());
120     }
121 } // TIntStack