2 * PROJECT: NyARToolkit
\r
3 * --------------------------------------------------------------------------------
\r
4 * The NyARToolkit is Java version ARToolkit class library.
\r
5 * Copyright (C)2008 R.Iizuka
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
12 * This program is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 * GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this framework; if not, write to the Free Software
\r
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
21 * For further information please contact.
\r
22 * http://nyatla.jp/nyatoolkit/
\r
23 * <airmail(at)ebony.plala.or.jp>
\r
26 package jp.nyatla.utils;
\r
27 import java.lang.reflect.*;
\r
28 import jp.nyatla.nyartoolkit.NyARException;
\r
34 * オンデマンド割り当てをするオブジェクト配列。
\r
37 public abstract class NyObjectStack<T>
\r
39 private final static int ARRAY_APPEND_STEP = 64;
\r
41 protected final T[] _items;
\r
43 private int _allocated_size;
\r
45 protected int _length;
\r
48 * 最大ARRAY_MAX個の動的割り当てバッファを準備する。
\r
51 * @param i_element_type
\r
52 * JavaのGenedicsの制限突破
\r
54 @SuppressWarnings("unchecked")
\r
55 protected NyObjectStack(int i_length,Class<T> i_element_type)
\r
57 // ポインタだけははじめに確保しておく
\r
58 this._items = (T[])Array.newInstance(i_element_type, i_length);
\r
59 // アロケート済サイズと、使用中個数をリセット
\r
60 this._allocated_size = 0;
\r
64 protected abstract T createElement();
\r
66 * ポインタを1進めて、その要素を予約し、その要素へのポインタを返します。
\r
67 * 特定型に依存させるときには、継承したクラスでこの関数をオーバーライドしてください。
\r
69 public final T prePush() throws NyARException
\r
72 if (this._length >= this._allocated_size) {
\r
74 if (this._length >= this._items.length) {
\r
75 throw new NyARException();
\r
78 int range = this._length + ARRAY_APPEND_STEP;
\r
79 if (range >= this._items.length) {
\r
80 range = this._items.length;
\r
83 this.onReservRequest(this._allocated_size, range, this._items);
\r
84 this._allocated_size = range;
\r
86 // 使用領域を+1して、予約した領域を返す。
\r
87 T ret = this._items[this._length];
\r
92 * 見かけ上の要素数を1減らして、最後尾のアイテムを返します。
\r
95 public final T pop() throws NyARException
\r
98 throw new NyARException();
\r
101 return this.getItem(this._length);
\r
104 * 0~i_number_of_item-1までの領域を予約します。
\r
105 * 予約すると、見かけ上の要素数は0にリセットされます。
\r
106 * @param i_number_of_reserv
\r
108 final public void reserv(int i_number_of_item) throws NyARException
\r
111 if (i_number_of_item >= this._allocated_size) {
\r
113 if (i_number_of_item >= this._items.length) {
\r
114 throw new NyARException();
\r
117 int range = i_number_of_item+ARRAY_APPEND_STEP;
\r
118 if (range >= this._items.length) {
\r
119 range = this._items.length;
\r
122 this.onReservRequest(this._allocated_size, range, this._items);
\r
123 this._allocated_size = range;
\r
126 this._length=i_number_of_item;
\r
130 * 必要に応じて、この関数を継承先クラスで実装して下さい。
\r
131 * i_bufferの配列の、i_start番目からi_end-1番目までの要素に、オブジェクトを割り当てて下さい。
\r
136 final protected void onReservRequest(int i_start, int i_end, Object[] i_buffer)
\r
139 for (int i = i_start; i < i_end; i++){
\r
140 i_buffer[i] =createElement();
\r
142 } catch(Exception e) {
\r
143 e.printStackTrace();
\r
154 public final T[] getArray()
\r
156 return this._items;
\r
158 public final T getItem(int i_index)
\r
160 return this._items[i_index];
\r
163 * 配列の見かけ上の要素数を返却します。
\r
166 public final int getLength()
\r
168 return this._length;
\r
172 * 見かけ上の要素数をリセットします。
\r
174 public final void clear()
\r