
/*
	Copyright 1993  Jeremy Slade.

	You are free to use all or any parts of the Locus project
	however you wish, just give credit where credit is due.
	The author (Jeremy Slade) shall not be held responsible
	for any damages that result out of use or misuse of any
	part of this project.

*/

/*
	Project: Locus

	File: AtomList.m

	Description: See AtomList.h

	Original Author: Jeremy Slade

	Revision History:
		Created
			V.101	JGS	Fri Mar 12 23:51:47 GMT-0700 1993

*/


#import "AtomList.h"
#import <appkit/Text.h> // For NXOrderStrings()


@implementation AtomList


// -------------------------------------------------------------------------
//   Creating, Initializing
// -------------------------------------------------------------------------


+ initialize
{
	[self setVersion:AtomList_VERSION];
	return ( self );
}



- initCount:(unsigned)num
{
	[super initCount:num];
	return ( self );
}



- free
{
	return ( [super free] );
}



// -------------------------------------------------------------------------
//   Manipulating Atoms by index
// -------------------------------------------------------------------------


- insertAtom:(NXAtom)a at:(unsigned)index
{
	return ( [super insertObject:(id)a at:index] );
}



- addAtom:(NXAtom)a
{
	return ( [super addObject:(id)a] );
}



- addAtomAlphabetically:(NXAtom)a
{
	unsigned int i = 0;
	BOOL added = NO;
	
	// Find place to insert in list
	for ( i=0; i<numElements; i++ ) {
		if ( NXOrderStrings ( a, [self atomAt:i], NO, -1, NULL ) == -1 ) {
			[self insertAtom:a at:i];
			added = YES;
			break;
		}
	}
	
	// Didn't come before any existing Atoms, add it to the end
	if ( !added ) [self addAtom:a];
	
	return ( self );
}



- removeAtomAt:(unsigned)index
{
	return ( [super removeObjectAt:index] );
}



- removeLastAtom
{
	return ( [super removeLastObject] );
}



- replaceAtomAt:(unsigned)index with:(NXAtom)newA
{
	return ( [super replaceObjectAt:index with:(id)newA] );
}



- (NXAtom)atomAt:(unsigned)index
{
	return ( (NXAtom)[super objectAt:index] );
}



- (NXAtom)lastAtom
{
	return ( (NXAtom)[super lastObject] );
}



- (unsigned)count
{
	return ( [super count] );
}



// -------------------------------------------------------------------------
//   Manipulating Atoms by pointer
// -------------------------------------------------------------------------


- addAtomIfAbsent:(NXAtom)a
{
	return ( [super addObjectIfAbsent:(id)a] );
}



- removeAtom:(NXAtom)a
{
	return ( [super removeObject:(id)a] );
}



- (unsigned)indexOfAtom:(NXAtom)a
{
	return ( [super indexOf:(id)a] );
}



// -------------------------------------------------------------------------
//   Archiving
// -------------------------------------------------------------------------


- awake
{
	[super awake];
	return ( self );
}



- read:(NXTypedStream *)stream
{
	unsigned int count, i;
	NXAtom *array;
	
	// First, let super do it's thing...
	[super read:stream];
	
	// Then, read the number of Atoms in the list and set the capacity of
	// the array
	NXReadType ( stream, "i", &count );
	[self setAvailableCapacity:count];
	numElements = count;
	
	// Then read all of the Atoms
	array = (NXAtom *)dataPtr;
	for ( i=0; i<count; i++ ) NXReadType ( stream, "%", array++ );
	
	return ( self );
}



- write:(NXTypedStream *)stream
{
	unsigned int count, i;
	NXAtom *array;
	
	// First, set count to zero to make [super write:] think there is
	// nothing to write...
	count = numElements;
	numElements = 0;
	
	// Now let super do it's thing...
	[super write:stream];
	
	// Now we'll archive the Atoms ourselves
	numElements = count;
	NXWriteType ( stream, "i", &count );
	array  = (NXAtom *)dataPtr;
	for ( i=0; i<count; i++ ) NXWriteType ( stream, "%", array++ );
	
	return ( self );
}



@end

