// Copyright 1997-1999 Omni Development, Inc.  All rights reserved.
//
// This software may only be used and reproduced according to the
// terms in the file OmniSourceLicense.html, which should be
// distributed with this project and can also be found at
// http://www.omnigroup.com/DeveloperResources/OmniSourceLicense.html.

#import "OFSparseArray.h"

#import <Foundation/Foundation.h>
#import <OmniBase/OmniBase.h>

#import "OFNull.h"

RCS_ID("$Header: /Network/Developer/Source/CVS/OmniGroup/OmniFoundation/DataStructures.subproj/OFSparseArray.m,v 1.3 1999/03/30 21:25:40 kc Exp $")

@implementation OFSparseArray

static OFNull            *nullValue = nil;

+ (void) initialize;
{
    static BOOL                 initialized = NO;

    [super initialize];

    if (initialized)
	return;

    initialized = YES;
    nullValue = (id)[[OFNull nullStringObject] retain];
}

- initWithCapacity:(unsigned int)aCapacity;
{
    if (![super init])
	return nil;

    values = [[NSMutableArray alloc] initWithCapacity:aCapacity];
    valuesLength = 0;

    return self;
}

- init;
{
    return [self initWithCapacity:0];
}

- (void)dealloc;
{
    [values release];
    [defaultValue release];
    [super dealloc];
}

- (unsigned int)count;
{
    return valuesLength;
}

- (id)objectAtIndex:(unsigned int)anIndex;
{
    id                          value;
    if (anIndex >= valuesLength)
	return defaultValue;
    value = [values objectAtIndex:anIndex];
    if (value == nullValue)
	return defaultValue;
    return value;
}

static inline void setValuesLength(OFSparseArray *self, unsigned int anIndex)
{
    while (self->valuesLength < anIndex) {
	[self->values addObject:nullValue];
	self->valuesLength++;
    }
}

- (void)setObject:(id)anObject atIndex:(unsigned int)anIndex;
{
    if (!anObject || anObject == defaultValue)
	anObject = nullValue;
    if (anIndex < self->valuesLength) {
	[self->values replaceObjectAtIndex:anIndex
	 withObject:anObject];
    } else if (anObject != nullValue) {
	setValuesLength(self, anIndex);
	[values addObject:anObject];
	valuesLength++;
    }
}

- (void)setDefaultValue:(id)aDefaultValue;
{
    if (defaultValue != aDefaultValue) {
	[defaultValue release];
        defaultValue = [aDefaultValue retain];
    }
}

- (NSArray *)valuesArray;
{
    return values;
}

- (NSMutableDictionary *)debugDictionary;
{
    return (id)values;
}

@end
