OSDN Git Service

ccb4998f7fcb995c214d12a4bb34a5a1d5abb90f
[nyartoolkit-and/nyartoolkit-and.git] / src / jp / nyatla / utils / NyObjectStack.java
1 /* \r
2  * PROJECT: NyARToolkit\r
3  * --------------------------------------------------------------------------------\r
4  * This work is based on the original ARToolKit developed by\r
5  *   Hirokazu Kato\r
6  *   Mark Billinghurst\r
7  *   HITLab, University of Washington, Seattle\r
8  * http://www.hitl.washington.edu/artoolkit/\r
9  *\r
10  * The NyARToolkit is Java version ARToolkit class library.\r
11  * Copyright (C)2008 R.Iizuka\r
12  *\r
13  * This program is free software; you can redistribute it and/or\r
14  * modify it under the terms of the GNU General Public License\r
15  * as published by the Free Software Foundation; either version 2\r
16  * of the License, or (at your option) any later version.\r
17  * \r
18  * This program is distributed in the hope that it will be useful,\r
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
21  * GNU General Public License for more details.\r
22  * \r
23  * You should have received a copy of the GNU General Public License\r
24  * along with this framework; if not, write to the Free Software\r
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
26  * \r
27  * For further information please contact.\r
28  *      http://nyatla.jp/nyatoolkit/\r
29  *      <airmail(at)ebony.plala.or.jp>\r
30  * \r
31  */\r
32 package jp.nyatla.utils;\r
33 \r
34 import jp.nyatla.nyartoolkit.NyARException;\r
35 \r
36 /**\r
37  * オンデマンド割り当てをするオブジェクト配列。\r
38  * 配列には実体を格納します。\r
39  */\r
40 public abstract class NyObjectStack\r
41 {\r
42         private final static int ARRAY_APPEND_STEP = 64;\r
43 \r
44         protected final Object[] _items;\r
45 \r
46         private int _allocated_size;\r
47 \r
48         protected int _length;\r
49 \r
50         /**\r
51          * 最大ARRAY_MAX個の動的割り当てバッファを準備する。\r
52          * \r
53          * @param i_array\r
54          */\r
55         public NyObjectStack(Object[] i_array)\r
56         {\r
57                 // ポインタだけははじめに確保しておく\r
58                 this._items = i_array;\r
59                 // アロケート済サイズと、使用中個数をリセット\r
60                 this._allocated_size = 0;\r
61                 this._length = 0;\r
62         }\r
63 \r
64         /**\r
65          * ポインタを1進めて、その要素を予約し、その要素へのポインタを返します。\r
66          * 特定型に依存させるときには、継承したクラスでこの関数をオーバーライドしてください。\r
67          */\r
68         public final Object prePush() throws NyARException\r
69         {\r
70                 // 必要に応じてアロケート\r
71                 if (this._length >= this._allocated_size) {\r
72                         // 要求されたインデクスは範囲外\r
73                         if (this._length >= this._items.length) {\r
74                                 throw new NyARException();\r
75                         }\r
76                         // 追加アロケート範囲を計算\r
77                         int range = this._length + ARRAY_APPEND_STEP;\r
78                         if (range >= this._items.length) {\r
79                                 range = this._items.length;\r
80                         }\r
81                         // アロケート\r
82                         this.onReservRequest(this._allocated_size, range, this._items);\r
83                         this._allocated_size = range;\r
84                 }\r
85                 // 使用領域を+1して、予約した領域を返す。\r
86                 Object ret = this._items[this._length];\r
87                 this._length++;\r
88                 return ret;\r
89         }\r
90         /**\r
91          * 見かけ上の要素数を1減らして、最後尾のアイテムを返します。\r
92          * @return\r
93          */\r
94         public final Object pop() throws NyARException\r
95         {\r
96                 if(this._length<1){\r
97                         throw new NyARException();\r
98                 }\r
99                 this._length--;\r
100                 return this.getItem(this._length);\r
101         }\r
102         /**\r
103          * 0~i_number_of_item-1までの領域を予約します。\r
104          * 予約すると、見かけ上の要素数は0にリセットされます。\r
105          * @param i_number_of_reserv\r
106          */\r
107         final public void reserv(int i_number_of_item) throws NyARException\r
108         {\r
109                 // 必要に応じてアロケート\r
110                 if (i_number_of_item >= this._allocated_size) {\r
111                         // 要求されたインデクスは範囲外\r
112                         if (i_number_of_item >= this._items.length) {\r
113                                 throw new NyARException();\r
114                         }\r
115                         // 追加アロケート範囲を計算\r
116                         int range = i_number_of_item+ARRAY_APPEND_STEP;\r
117                         if (range >= this._items.length) {\r
118                                 range = this._items.length;\r
119                         }\r
120                         // アロケート\r
121                         this.onReservRequest(this._allocated_size, range, this._items);\r
122                         this._allocated_size = range;\r
123                 }\r
124                 //見かけ上の配列サイズを指定\r
125                 this._length=i_number_of_item;\r
126                 return;\r
127         }\r
128 \r
129 \r
130 \r
131         /**\r
132          * この関数を継承先クラスで実装して下さい。\r
133          * i_bufferの配列の、i_start番目からi_end-1番目までの要素に、オブジェクトを割り当てて下さい。\r
134          * \r
135          * @param i_start\r
136          * @param i_end\r
137          * @param i_buffer\r
138          */\r
139         protected abstract void onReservRequest(int i_start, int i_end, Object[] i_buffer);\r
140         /**\r
141          * 配列を返します。\r
142          * \r
143          * @return\r
144          */\r
145         public final Object[] getArray()\r
146         {\r
147                 return this._items;\r
148         }\r
149         public final Object getItem(int i_index)\r
150         {\r
151                 return this._items[i_index];\r
152         }\r
153         /**\r
154          * 配列の見かけ上の要素数を返却します。\r
155          * @return\r
156          */\r
157         public final int getLength()\r
158         {\r
159                 return this._length;\r
160         }\r
161         \r
162         /**\r
163          * 見かけ上の要素数をリセットします。\r
164          */\r
165         public final void clear()\r
166         {\r
167                 this._length = 0;\r
168         }\r
169 }\r