/****************************************************************************
 * KONOHA COPYRIGHT, LICENSE NOTICE, AND DISCRIMER  
 * 
 * Copyright (c) 2005-2008, Kimio Kuramitsu <kimio at ynu.ac.jp>
 *           (c) 2008-      Konoha Software Foundation  
 * All rights reserved.
 * 
 * You may choose one of the following two licenses when you use konoha. 
 * See www.konohaware.org/license.html for further information.
 * 
 * (1) GNU General Public License 2.0      (with    KONOHA_UNDER_GPL2)
 * (2) Konoha Software Foundation License 1.0
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *  
 ****************************************************************************/

/* ************************************************************************ */

#include"commons.h"

/* ************************************************************************ */

#ifdef __cplusplus 
extern "C" {
#endif

/* ======================================================================== */
/* [define] */

#ifndef KNH_ARRAY_INITSIZE
#define KNH_ARRAY_INITSIZE  4
#endif/*KNH_ARRAY_INITSIZE*/

/* ======================================================================== */
/* [macros] */

#define _knh_Array_n(a,n)      (a)->list[(n)]
#define _knh_Array_size(a)     (a)->size

/* ======================================================================== */
/* [constructors] */

knh_Array_t* new_Array0(Ctx *ctx, size_t capacity)
{
	knh_Array_t *o = (knh_Array_t*)new_PObject(ctx, FLAG_Array, CLASS_Array, CLASS_Array);
	o->list = knh_oarray_malloc(ctx, capacity, KNH_NULL);
	o->size = 0;
	//DBG2_P("%p o->size=%d %d", o, o->size, knh_oarray_capacity(o->list));
	return o;
}

/* ------------------------------------------------------------------------ */

knh_Array_t* new_Array(Ctx *ctx, knh_class_t p1, size_t capacity)
{
	knh_class_t cid = knh_pmzclass(ctx, CLASS_Array, p1, CLASS_Any);
	knh_Array_t *o = (knh_Array_t*)new_PObject(ctx, FLAG_Array, CLASS_Array, cid);
	o->list = knh_oarray_malloc(ctx, capacity, KNH_NULL);
	o->size = 0;
	//DBG2_P("%p o->size=%d %d", o, o->size, knh_oarray_capacity(o->list));
	return o;
}

/* ======================================================================== */
/* [field] */

INLINE
Object** knh_Array_list(Array *o)
{
	return o->list;
}

/* ------------------------------------------------------------------------ */

Object* knh_Array_last(Array *o)
{
	KNH_ASSERT(o->size != 0);
	return o->list[o->size-1];
}

/* ------------------------------------------------------------------------ */

void knh_Array_subclear(Ctx *ctx, Array *o, size_t n)
{
	size_t i;
	for(i = n; i < o->size; i++) {
		KNH_SETv(ctx, o->list[i], KNH_NULL);
	}
	if(n <= o->size) {
		o->size = n;
	}
}

/* ======================================================================== */
/* [utils] */

size_t knh_array_index(Ctx *ctx, knh_int_t n, size_t size)
{
	size_t idx = (n < 0) ? size + n : n;
	if(!(idx < size)) {
		char buf[80];
		knh_snprintf(buf, sizeof(buf), "OutOfIndex!!: %d < %d", (int)n, (int)size);
		KNH_THROWs(ctx, buf);
	}
	return idx;
}

/* ------------------------------------------------------------------------ */

void knh_Array_add(Ctx *ctx, Array *o, Any *value)
{
	if(o->size == knh_oarray_capacity(o->list)) {
		o->list = knh_oarray_grow(ctx, o->list);
	}
	KNH_ASSERT(o->size < knh_oarray_capacity(o->list));
	KNH_SETv(ctx, o->list[o->size], value);
	o->size++;
}

/* ------------------------------------------------------------------------ */

Any* knh_Array_pop(Ctx *ctx, Array *o)
{
	if(o->size > 0) {
		o->size--;
		return o->list[o->size];
	}else {
		return KNH_NULL;
	}
}

/* ------------------------------------------------------------------------ */


#ifdef __cplusplus
}
#endif
