Code Fragment: NodeSequence2



template <typename Object>
NodeSequence<Object>::Position NodeSequence<Object>::
atRank(int rank) const 					// position of rank
    throw(BoundaryViolationException) {
  NodePtr v;
  checkRank(rank);
  if (rank <= size()/2) {				// scan forward from head
    v = header->next;
    for (int i = 0; i < rank; i++)
      v = v->next;
  }
  else {						// scan back from tail
    v = trailer->prev;
    for (int i = 1; i < size()-rank; i++)
      v = v->prev;
  }
  return Position(v);
}

template <typename Object>
int NodeSequence<Object>::
rankOf(const Position &p) const 			// get rank of position
    throw(InvalidPositionException) {
  NodePtr v = first();
  int i = 0;
  while (v != trailer) {				// search for p.node
    if (p.node == v) return i;				// found it here
    v = v->next;					// else advance
    i++;
  }							// did not find it?
  throw InvalidPositionException("Position not found");
}