/* * PROJECT: FLARToolKit * -------------------------------------------------------------------------------- * This work is based on the NyARToolKit developed by * R.Iizuka (nyatla) * http://nyatla.jp/nyatoolkit/ * * The FLARToolKit is ActionScript 3.0 version ARToolkit class library. * Copyright (C)2008 Saqoosha * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this framework; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For further information please contact. * http://www.libspark.org/wiki/saqoosha/FLARToolKit * <saq(at)saqoosha.net> * */ package org.libspark.flartoolkit.utils { import org.libspark.flartoolkit.FLARException; /** * オンデマンド割り当ã¦ã‚’ã™ã‚‹ã‚ªãƒ–ジェクトé…列。 * é…列ã«ã¯å®Ÿä½“ã‚’æ ¼ç´ã—ã¾ã™ã€‚ */ public class NyObjectStack { private static const ARRAY_APPEND_STEP:int = 64; private var _dataClass:Class; protected var _items:Array; private var _allocated_size:int; protected var _length:int; /** * 最大ARRAY_MAX個ã®å‹•çš„割り当ã¦ãƒãƒƒãƒ•ã‚¡ã‚’準備ã™ã‚‹ã€‚ * * @param i_array */ public function NyObjectStack(klass:Class, i_max_array_size:int) { this._dataClass = klass; // ãƒã‚¤ãƒ³ã‚¿ã ã‘ã¯ã¯ã˜ã‚ã«ç¢ºä¿ã—ã¦ãŠã this._items = new Array(i_max_array_size) var n:int = i_max_array_size; while (n--) { this._items[n] = new this._dataClass(); } // ã‚¢ãƒã‚±ãƒ¼ãƒˆæ¸ˆã‚µã‚¤ã‚ºã¨ã€ä½¿ç”¨ä¸å€‹æ•°ã‚’リセット this._allocated_size = 0; this._length = 0; } /** * ãƒã‚¤ãƒ³ã‚¿ã‚’1進ã‚ã¦ã€ãã®è¦ç´ を予約ã—ã€ãã®è¦ç´ ã¸ã®ãƒã‚¤ãƒ³ã‚¿ã‚’è¿”ã—ã¾ã™ã€‚ * 特定型ã«ä¾å˜ã•ã›ã‚‹ã¨ãã«ã¯ã€ç¶™æ‰¿ã—ãŸã‚¯ãƒ©ã‚¹ã§ã“ã®é–¢æ•°ã‚’オーãƒãƒ¼ãƒ©ã‚¤ãƒ‰ã—ã¦ãã ã•ã„。 */ public function prePush():Object { // å¿…è¦ã«å¿œã˜ã¦ã‚¢ãƒã‚±ãƒ¼ãƒˆ if (this._length >= this._allocated_size) { // è¦æ±‚ã•ã‚ŒãŸã‚¤ãƒ³ãƒ‡ã‚¯ã‚¹ã¯ç¯„囲外 if (this._length >= this._items.length) { throw new FLARException(); } // è¿½åŠ ã‚¢ãƒã‚±ãƒ¼ãƒˆç¯„囲を計算 var range:int = this._length + ARRAY_APPEND_STEP; if (range >= this._items.length) { range = this._items.length; } // ã‚¢ãƒã‚±ãƒ¼ãƒˆ this.onReservRequest(this._allocated_size, range, this._items); this._allocated_size = range; } // ä½¿ç”¨é ˜åŸŸã‚’+1ã—ã¦ã€äºˆç´„ã—ãŸé ˜åŸŸã‚’è¿”ã™ã€‚ var ret:Object = this._items[this._length]; this._length++; return ret; } /** * 見ã‹ã‘上ã®è¦ç´ æ•°ã‚’1減らã—ã¦ã€æœ€å¾Œå°¾ã®ã‚¢ã‚¤ãƒ†ãƒ ã‚’è¿”ã—ã¾ã™ã€‚ * @return */ public function pop():Object { if(this._length < 1) { throw new FLARException(); } this._length--; return this.getItem(this._length); } /** * 0~i_number_of_item-1ã¾ã§ã®é ˜åŸŸã‚’予約ã—ã¾ã™ã€‚ * 予約ã™ã‚‹ã¨ã€è¦‹ã‹ã‘上ã®è¦ç´ æ•°ã¯0ã«ãƒªã‚»ãƒƒãƒˆã•ã‚Œã¾ã™ã€‚ * @param i_number_of_reserv */ public function reserv(i_number_of_item:int):void { // å¿…è¦ã«å¿œã˜ã¦ã‚¢ãƒã‚±ãƒ¼ãƒˆ if (i_number_of_item >= this._allocated_size) { // è¦æ±‚ã•ã‚ŒãŸã‚¤ãƒ³ãƒ‡ã‚¯ã‚¹ã¯ç¯„囲外 if (i_number_of_item >= this._items.length) { throw new FLARException(); } // è¿½åŠ ã‚¢ãƒã‚±ãƒ¼ãƒˆç¯„囲を計算 var range:int = i_number_of_item + ARRAY_APPEND_STEP; if (range >= this._items.length) { range = this._items.length; } // ã‚¢ãƒã‚±ãƒ¼ãƒˆ this.onReservRequest(this._allocated_size, range, this._items); this._allocated_size = range; } //見ã‹ã‘上ã®é…列サイズを指定 this._length = i_number_of_item; return; } /** * ã“ã®é–¢æ•°ã‚’継承先クラスã§å®Ÿè£…ã—ã¦ä¸‹ã•ã„。 * i_bufferã®é…列ã®ã€i_start番目ã‹ã‚‰i_end-1番目ã¾ã§ã®è¦ç´ ã«ã€ã‚ªãƒ–ジェクトを割り当ã¦ã¦ä¸‹ã•ã„。 * * @param i_start * @param i_end * @param i_buffer */ protected virtual function onReservRequest(i_start:int, i_end:int, i_buffer:Array):void { for (var i:int = i_start; i < i_end; i++) { i_buffer[i] = new this._dataClass(); } } /** * é…列を返ã—ã¾ã™ã€‚ * * @return */ public function getArray():Array { return this._items; } public function getItem(i_index:int):Object { return this._items[i_index]; } /** * é…列ã®è¦‹ã‹ã‘上ã®è¦ç´ æ•°ã‚’è¿”å´ã—ã¾ã™ã€‚ * @return */ public function getLength():int { return this._length; } /** * 見ã‹ã‘上ã®è¦ç´ 数をリセットã—ã¾ã™ã€‚ */ public function clear():void { this._length = 0; } } }