GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
IntegerStack.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.common.util;
29
30import java.nio.BufferOverflowException;
31import java.nio.BufferUnderflowException;
32import java.nio.IntBuffer;
33
34/**
35 * Simple primitive-type stack.
36 * <p>
37 * Implemented operations:
38 * <ul>
39 * <li>FILO - First In, Last Out</li>
40 * </ul>
41 * </p>
42 */
43public class /*name*/IntegerStack/*name*/ implements PrimitiveStack {
44 private int position;
45 private int[] buffer;
46 private int growSize;
47
48 /**
49 * @param initialSize initial size, must be &gt; zero
50 * @param growSize grow size if {@link #position()} is reached, maybe <code>0</code>
51 * in which case an {@link IndexOutOfBoundsException} is thrown.
52 */
53 public /*name*/IntegerStack/*name*/(final int initialSize, final int growSize) {
54 this.position = 0;
55 this.growSize = growSize;
56 this.buffer = new int[initialSize];
57 }
58
59 @Override
60 public final int capacity() { return buffer.length; }
61
62 @Override
63 public final int position() { return position; }
64
65 @Override
66 public final void position(final int newPosition) throws IndexOutOfBoundsException {
67 if( 0 > position || position >= buffer.length ) {
68 throw new IndexOutOfBoundsException("Invalid new position "+newPosition+", "+this.toString());
69 }
70 position = newPosition;
71 }
72
73 @Override
74 public final int remaining() { return buffer.length - position; }
75
76 @Override
77 public final int getGrowSize() { return growSize; }
78
79 @Override
80 public final void setGrowSize(final int newGrowSize) { growSize = newGrowSize; }
81
82 @Override
83 public final String toString() {
84 return "IntegerStack[0..(pos "+position+").."+buffer.length+", remaining "+remaining()+"]";
85 }
86
87 public final int[] buffer() { return buffer; }
88
89 private final void growIfNecessary(final int length) throws IndexOutOfBoundsException {
90 if( position + length > buffer.length ) {
91 if( 0 >= growSize ) {
92 throw new IndexOutOfBoundsException("Out of fixed stack size: "+this);
93 }
94 final int[] newBuffer =
95 new int[buffer.length + growSize];
96 System.arraycopy(buffer, 0, newBuffer, 0, position);
97 buffer = newBuffer;
98 }
99 }
100
101 /**
102 * FILO put operation
103 *
104 * @param src source buffer
105 * @param srcOffset offset of src
106 * @param length number of float elements to put from <code>src</code> on-top this stack
107 * @return the src float[]
108 * @throws IndexOutOfBoundsException if stack cannot grow due to zero grow-size or offset+length exceeds src.
109 */
110 public final int[]
111 putOnTop(final int[] src, final int srcOffset, final int length) throws IndexOutOfBoundsException {
112 growIfNecessary(length);
113 System.arraycopy(src, srcOffset, buffer, position, length);
114 position += length;
115 return src;
116 }
117
118 /**
119 * FILO put operation
120 *
121 * @param src source buffer, it's position is incremented by <code>length</code>
122 * @param length number of float elements to put from <code>src</code> on-top this stack
123 * @return the src FloatBuffer
124 * @throws IndexOutOfBoundsException if stack cannot grow due to zero grow-size
125 * @throws BufferUnderflowException if <code>src</code> FloatBuffer has less remaining elements than <code>length</code>.
126 */
127 public final IntBuffer
128 putOnTop(final IntBuffer src, final int length) throws IndexOutOfBoundsException, BufferUnderflowException {
129 growIfNecessary(length);
130 src.get(buffer, position, length);
131 position += length;
132 return src;
133 }
134
135 /**
136 * FILO get operation
137 *
138 * @param dest destination buffer
139 * @param destOffset offset of dest
140 * @param length number of float elements to get from-top this stack to <code>dest</code>.
141 * @return the dest float[]
142 * @throws IndexOutOfBoundsException if stack or <code>dest</code> has less elements than <code>length</code>.
143 */
144 public final int[]
145 getFromTop(final int[] dest, final int destOffset, final int length) throws IndexOutOfBoundsException {
146 System.arraycopy(buffer, position-length, dest, destOffset, length);
147 position -= length;
148 return dest;
149 }
150
151 /**
152 * FILO get operation
153 *
154 * @param dest destination buffer, it's position is incremented by <code>length</code>.
155 * @param length number of float elements to get from-top this stack to <code>dest</code>.
156 * @return the dest FloatBuffer
157 * @throws IndexOutOfBoundsException if stack has less elements than length
158 * @throws BufferOverflowException if <code>src</code> FloatBuffer has less remaining elements than <code>length</code>.
159 */
160 public final IntBuffer
161 getFromTop(final IntBuffer dest, final int length) throws IndexOutOfBoundsException, BufferOverflowException {
162 dest.put(buffer, position-length, length);
163 position -= length;
164 return dest;
165 }
166}
Simple primitive-type stack.
final int[] putOnTop(final int[] src, final int srcOffset, final int length)
FILO put operation.
final IntBuffer putOnTop(final IntBuffer src, final int length)
FILO put operation.
final IntBuffer getFromTop(final IntBuffer dest, final int length)
FILO get operation.
final int capacity()
Returns this stack's current capacity.
final int position()
Returns the current position of this stack.
final int remaining()
Returns the remaining elements left before stack will grow about getGrowSize().
final int getGrowSize()
Returns the grow size.
final int[] getFromTop(final int[] dest, final int destOffset, final int length)
FILO get operation.
final void position(final int newPosition)
Sets the position of this stack.
final void setGrowSize(final int newGrowSize)
Set new growSize().
Simple primitive-type stack.