Code Fragment: NodeSequence1



template <typename Object>
class NodeSequence : public NodeList<Object> {
protected: 						// utilities
  void checkRank(int rank) const 			// check for valid rank
      throw(BoundaryViolationException) {
    if (rank < 0 || rank >= sz)
      throw BoundaryViolationException("Invalid rank");
  }
public:
  typedef NodeList<Object>::Position Position;
  Position atRank(int rank) const 			// position of rank
      throw(BoundaryViolationException);
  int rankOf(const Position& p) const 			// get rank of element
      throw(InvalidPositionException);
  Object elemAtRank (int rank) const 			// element at this rank
      throw(BoundaryViolationException) {
    checkRank(rank);
    return atRank(rank).element();
  }
  void insertAtRank (int rank, const Object& element)	// insert at given rank
      throw(BoundaryViolationException) {
    if (rank == size())					// no checkRank if last
      insertLast(element);
    else {
      checkRank(rank);
      insertBefore( atRank(rank), element );
    }
  }
  void removeAtRank (int rank)				// remove from rank
      throw(BoundaryViolationException) {
    checkRank(rank);
    Position p = atRank(rank);				// position to remove
    remove(p);
  }
  void replaceAtRank (int rank, const Object& element)	// replace at rank
      throw(BoundaryViolationException) {
    checkRank(rank);
    replaceElement( atRank(rank), element );
  }
};