#include "oop.h"

#include <stdio.h>

oop SequenceableCollection::atAllPut(oop anObject)
{
  // d"Put anObject at every one of the receiver's indices.
  int count= size();
  for (int i= 0;  i < count;  ++i)
    atPut(i, anObject);
  return this;
}

oop SequenceableCollection::replaceFromToWithStartingAt(int from, int to, oop aCollection, int start)
{
  // This destructively replaces elements from start to stop in the
  // receiver starting at index, repStart, in the sequenceable
  // collection, replacementCollection.  Answer the receiver.  No
  // range checks are performed.
  while (from < to)
    atPut(from++, aCollection->at(start++));
  return this;
}

oop SequenceableCollection::copyFromTo(int start, int stop)
{
  // Answer a copy of a subset of the receiver, starting from element at
  // index start until element at index stop.
  int newSize= stop - start;
  return speciesNew(newSize)->replaceFromToWithStartingAt(0, newSize, this, start);
}

oop SequenceableCollection::readStream(void)
{
  return new ReadStream(this);
}

oop SequenceableCollection::writeStream(void)
{
  return new WriteStream(this);
}

oop SequenceableCollection::first(void)
{
  return at(0);
}

oop SequenceableCollection::last(void)
{
  return at(size() - 1);
}

int SequenceableCollection::operator ==(oop anObject)
{
  if (!anObject->isSequenceableCollection()) return 0;
  if (size() != anObject->size()) return 0;
  iterateWith(this, anObject, my, his)
    if (**my != *his)
      return 0;
  return 1;
}

