/* 
   NSConcreteArray.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <stdio.h>

#include <Foundation/common.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSString.h>
#include <Foundation/NSException.h>
#include <Foundation/exceptions/GeneralExceptions.h>

#include <extensions/objc-runtime.h>

#include "NSConcreteArray.h"

/*
 * NSConcreteArray class
 */

@implementation NSConcreteArray

- (id)init
{
    items = NULL;
    itemsCount = 0;
    return self;
}

- (id)initWithObjects:(id *)objects count:(unsigned int)count
{
    int i;

    items = NSZoneCalloc([self zone], sizeof(id), count);
    itemsCount = count;
    for (i=0; i < count; i++)
	if (!(items[i] = [objects[i] retain]))
	    THROW([[InvalidArgumentException alloc] 
		    initWithReason:@"Nil object to be added in array"]);
    return self;
}

- (id)initWithArray:(NSArray *)anotherArray
{
    int i, count = [anotherArray count];

    items = NSZoneCalloc([self zone], sizeof(id), count);
    itemsCount = count;
    for (i = 0; i < itemsCount; i++)
	items[i] = [[anotherArray objectAtIndex:i] retain];
    return self;
}

- (void)dealloc
{
    unsigned int index;
    for (index = 0; index < itemsCount; index++)
	[items[index] release];
    Free(items);
    [super dealloc];
}

/* Querying the Array */

- (id)objectAtIndex:(unsigned int)index
{
    if (index >= itemsCount)
	THROW([[RangeException alloc] 
		initWithReason:@"objectAtIndex: in NSArray" 
		size:itemsCount index:index]);
    return items[index];
}

- (unsigned int)count
{
    return itemsCount;
}

- (unsigned int)indexOfObjectIdenticalTo:(id)anObject
{
    int i;
    for (i=0; i < itemsCount; i++)
	if (items[i] == anObject)
		return i;
    return NSNotFound;
}

@end

/*
 * NSConcreteMutableArray class
 */

@implementation NSConcreteMutableArray

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized) {
	initialized = YES;
	class_add_behavior(self, [NSConcreteArray class]);
    }
}

- (id)init
{
    items = NSZoneCalloc([self zone], 1, sizeof(id));
    maxItems = 1;
    itemsCount = 0;
    return self;
}

- (id)initWithCapacity:(unsigned int)aNumItems
{
    items = NSZoneCalloc([self zone], sizeof(id), aNumItems);
    maxItems = aNumItems;
    itemsCount = 0;
    return self;
}

- (id)initWithObjects:(id *)objects count:(unsigned int)count
{
    int i;

    items = NSZoneCalloc([self zone], sizeof(id), count);
    maxItems = itemsCount = count;

    for (i=0; i < count; i++)
	if (!(items[i] = [objects[i] retain]))
	    THROW([[InvalidArgumentException alloc] 
		    initWithReason:@"Nil object to be added in array"]);
    return self;
}

- (id)initWithArray:(NSArray *)anotherArray
{
    int i, count = [anotherArray count];

    items = NSZoneCalloc([self zone], sizeof(id), count);
    maxItems = itemsCount = count;
    for (i = 0; i < itemsCount; i++)
	items[i] = [[anotherArray objectAtIndex:i] retain];
    return self;
}

- (void)dealloc
{
    unsigned int index;
    for (index = 0; index < itemsCount; index++)
	[items[index] release];
    Free(items);
    [super dealloc];
}

/* Altering the Array */

- (void)insertObject:(id)anObject atIndex:(unsigned int)index
{
    unsigned int i;
    if (!anObject)
	THROW([[InvalidArgumentException alloc] 
		initWithReason:@"Nil object to be added in array"]);
    if (index > itemsCount)
	THROW([[RangeException alloc] 
		initWithReason:@"__insertObject:atIndex: in NSMutableArray" 
		size:itemsCount index:index]);
    if (itemsCount == maxItems) {
	if (maxItems) {
	    maxItems += (maxItems >> 1) ? (maxItems >>1) : 1;
	}
	else {
	    maxItems = 1;
	}
	items = (id*)Realloc(items, sizeof(id) * maxItems);
    }
    for(i = itemsCount; i > index; i--)
	items[i] = items[i - 1];
    items[index] = [anObject retain];
    itemsCount++;
}

- (void)replaceObjectAtIndex:(unsigned int)index  withObject:(id)anObject
{
    if (!anObject)
	THROW([[InvalidArgumentException alloc] 
		initWithReason:@"Nil object to be added in array"]);
    if (index >= itemsCount)
	THROW([[RangeException alloc] 
		initWithReason:@"NSConcreteMutableArray replaceObjectAtIndex" 
		size:itemsCount index:index]);
    [anObject retain];
    [items[index] release];
    items[index] = anObject;
}

- (void)removeObjectsFrom:(unsigned int)index
  count:(unsigned int)count
{
    int i;
    if (index + count > itemsCount)
	THROW([[RangeException alloc]
		initWithReason:@"removeObjectsFrom:count in NSMutableArray"
		size:itemsCount index:index]);
    if (!count)
	return;
    for (i = index; i < index + count; i++)
	[items[i] release];

    for (i = index + count; i < itemsCount; i++, index++)
	items[index] = items[i];
    for (; index < itemsCount; index++)
	items[index] = (id)0x3;

    itemsCount -= count;
}

- (void)removeObjectsInRange:(NSRange)aRange
{
    [self removeObjectsFrom:aRange.location count:aRange.length];
}

- (void)removeAllObjects
{
    [self removeObjectsFrom:0 count:itemsCount];
}

- (void)removeLastObject
{
    if (itemsCount)
	[self removeObjectsFrom:(itemsCount - 1) count:1];
}

- (void)removeObjectAtIndex:(unsigned int)index
{
    [self removeObjectsFrom:index count:1];
}

@end
