ftp.nice.ch/pub/next/developer/resources/libraries/libcoll.930521.s.tar.gz#/libcoll-930521/Array.m

This is Array.m in view mode; [Download] [Up]

/* Implementation for Objective-C Array collection object

   Copyright (C) 1993 R. Andrew McCallum <mccallum@cs.rochester.edu>
   Dept. of Computer Science, U. of Rochester, Rochester, NY  14627

   This file is part of the GNU Objective-C Collection library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ 

#include <coll/Array.h>
#include <coll/ArrayPrivate.h>

@implementation Array

+ initialize
{
    if (self == [Array class])
	[self setVersion:-1];	/* alpha release */
    return self;
}

// MANAGING CAPACITY;

/* Eventually we will want to have better capacity management,
   potentially keep default capacity as a class variable. */

+ (unsigned) defaultCapacity
{
  return DEFAULT_ARRAY_CAPACITY;
}

+ (unsigned) defaultGrowFactor
{
  return DEFAULT_ARRAY_GROW_FACTOR;
}
  
/* This is the designated initializer of this class */
- initDescription: (const char *)type capacity: (unsigned)aCapacity
{
  [super initDescription:type];
  _grow_factor = [[self class] defaultGrowFactor];
  _count = 0;
  /* what if someone passes zero? */
  _capacity = aCapacity;
  OBJC_MALLOC(_contents_array, elt, _capacity);
  return self;
}

- initCapacity: (unsigned)aCapacity
{
  return [self initDescription:"@" capacity:aCapacity];
}

/* Catch designated initializer for IndexedCollection */
- initDescription: (const char *)type
{
  return [self initDescription:type capacity:[[self class] defaultCapacity]];
}

// MANAGING CAPACITY;

/* This is the only method that changes the value of the instance
   variable _capacity, except for "-initDescription:capacity:" */

- setCapacity: (unsigned)newCapacity
{
  if (newCapacity > _count) {
    _capacity = newCapacity;
    OBJC_REALLOC(_contents_array, elt, _capacity);
  }
  return self;
}

- (unsigned) growFactor
{
  return _grow_factor;
}

- setGrowFactor: (unsigned)aNum;
{
  _grow_factor = aNum;
  return self;
}

// ADDING;

- appendElement: (elt)newElement
{
  incrementCount(self);
  _contents_array[_count-1] = newElement;
}

- prependElement: (elt)newElement
{
  incrementCount(self);
  makeHoleAt(self, 0);
  _contents_array[0] = newElement;
  return self;
}

- insertElement: (elt)newElement atIndex: (unsigned)index
{
  if (INDEX_RANGE_WARNING(index, _count+1))
    return nil;
  incrementCount(self);
  makeHoleAt(self, index);
  _contents_array[index] = newElement;
  return self;
}


// REMOVING, REPLACING AND SWAPPING;

- (elt) removeElementAtIndex: (unsigned)index
{
  elt ret;

  if (INDEX_RANGE_WARNING(index, _count))
    return COLL_NO_ELEMENT;
  ret = _contents_array[index];
  fillHoleAt(self, index);
  decrementCount(self);
  return ret;
}
  
/* We could be more efficient if we override these also.
   - (elt) removeFirstElement
   - (elt) removeLastElement; */


- (elt) replaceElementAtIndex: (unsigned)index with: (elt)newElement
{
  elt ret;

  if (INDEX_RANGE_WARNING(index, _count))
    return nil;
  ret = _contents_array[index];
  _contents_array[index] = newElement;
  return ret;
}

- swapAtIndeces: (unsigned)index1 : (unsigned)index2
{
  elt tmp;

  if (INDEX_RANGE_WARNING(index1, _count)
      || INDEX_RANGE_WARNING(index2, _count))
    return nil;
  tmp = _contents_array[index1];
  _contents_array[index1] = _contents_array[index2];
  _contents_array[index2] = tmp;
  return self;
}


// GETTING ELEMENTS BY INDEX;

- (elt) elementAtIndex: (unsigned)index
{
  if (INDEX_RANGE_WARNING(index, _count))
      return COLL_NO_ELEMENT;
  return _contents_array[index];
}


// TESTING;

- (unsigned) count
{
  return _count;
}

// SORTING;

/**  Is this what we want?  Careful with CircularArray.
- sortContentsByCalling: (int(*)(elt,elt))aFunc
{
  int compare(elt* e1, elt* e2)
    {
      return aFunc(*e1, *e2);
    }
  qsort(_contents_array, _count, sizeof(elt), compare);
  return self;
}
**/

/**
#include <objc/error.h>
- testing
{
  EX_DURING
    {
      EX_RAISE(@selector(errorNoIndexInMethod:),0,0);
    }
  EX_HANDLER 
    {
      switch (ex_local_handler.code)
	{
	case @selector(errorNoIndexInMethod:):
	  [self errorNoIndexInMethod:_cmd];
	  break;

	default:
	  EX_RERAISE();
	}
    }
  EX_ENDHANDLER;
  return self;
}
**/


@end


These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.