Sequence



next up previous contents index
Next: Vector Up: No Title Previous: Array

Sequence

A "sequence" is an immutable indexed collection. It is a parameterized type; the actual parameter of an instantiation determines the type of the "sequence" elements. The low bound of a "sequence" is always 1. The size of a "sequence" must be represented as an int and is therefore [tex2html_wrap2975] "int_max".

The sequence routine "sequence_create" allows the new sequence to be created using the varying arguments form. (6.1). For example

        s: sequence[int] := sequence_create[int](.. 6, 9, 17)
creates a new sequence containing the elements 6, 9, and 17.

Methods for type "sequence[T]"

  empty ( ) returns (bool)
      % effects   returns true if the sequence is empty, else returns false.

  length ( ) returns (int)
      % effects   returns the length of the sequence (a count of the number of elements it contains).

  fetch (i: int) returns (T) signals (bounds)
      % effects   if i is not a legal index in self, signals bounds.  Otherwise returns the
      %           ith element.

  replace (i: int, v: T) returns (sequence[T]) signals (bounds)
      % effects   if i is not a legal index in self, signals bounds.  Otherwise, returns a new
      %           sequence containing the elements of self, except that the ith element is v.

  append (x: T) returns (sequence[T]) 
      % effects   returns a new sequence containing the elements of self extended by x on the
      %           high end;  signals failure if the size of the new sequence would be too large.

  extract (at: int, count: int ) returns (sequence[T]) signals (bounds, negative_size)
      % effects   if "at" is not a legal index, signal bounds, else if "count"
      %           is negative, signal negative_size. Otherwise,
      %           return a new sequence containing the elements
      %		  self[at], ..., self[min(at + count - 1, self.length())]

  concat (s: sequence[T]) returns (sequence[T]) 
      % effects   returns a new sequence containing the elements of self followed by the elements
      %           of s; signals failure if the size of the new sequence would be too large.

  indexes ( ) yields (int)
      % effects   yields the legal indexes of self

  elements ( ) yields (T) 
      % effects   yields the elements of self in order from low bound to high bound.

  equal (s: sequence[T]) returns (bool)
        where T has equal (T) returns (bool)
      % effects   returns true if self and s are indistinguishable, i.e., they are
      %           the same size and their corresponding elements are equal.

  similar (s: sequence[T]) returns (bool)
        where T has similar (T) returns (bool)
      % effects   returns true if self and s are the same size and their corresponding
      %           elements are similar, using T similar to do the test.

  copy ( ) returns (sequence[T])
        where T has copy ( ) returns (T)
      % effects   returns a new sequence with the same size as self and containing a copy
      %           of each element of self (using T copy) in the corresponding positions.

  unparse ( ) returns (string)
        where T has unparse ( ) returns (string)
      % effects   produces a string representation of the contents of self using the
      %           T unparse method to produce string images of the elements.
      %           The resulting string has the form vector[\tex{$e_1$},...\tex{$e_n$}],
      %           where \tex{$e_i$} is obtained by calling the T unparse method for that element.
Routines for type "sequence[T]"
  sequence_create[T] (els: sequence[T]) returns (sequence[T])
      % effects   returns a new sequence containing the
      %           elements of els in order.  

  sequence_generate[T] (n: int, els: iter () yields (T)) returns (sequence[T])
      signals (negative_size, not_enough)
      % effects   If n < 0, signals negative_size.  If the iterator yields less than n elements,
      %           signals not_enough.  Otherwise, returns a new sequence containing the
      %           first n elements yielded by the iterator in order.



theta-questions@lcs.mit.edu