Vector



next up previous contents index
Next: Record Up: No Title Previous: Sequence

Vector

A "vector" is a fixed-size, mutable, homogeneous collection with a low bound of 1. The elements of a "vector[T]" are all initialized with some element of type "T". The size of a "vector" must be representable by an "int" and therefore must be less than or equal to "int_max".

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

        v: vector[int] := vector_create[int](.. 6, 9, 17)
creates a new vector containing the elements 6, 9, and 17.

Methods for type "vector[T]"

  length ( ) returns (int)
      % effects   returns the size of the vector (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 element self[i].

  store (i: int, v: T) signals (bounds)
      % modifies  self
      % effects   If i is not a legal index in self, signals bounds.
      %           Otherwise, sets the element at self[i] to v.

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

  elements ( ) yields (T) 
      % effects  The effect of x.elements() is equivalent to the following body:
      %             for i: int in x.indexes() do yield(x[i]) end
      %           note that stores to the vector may affect the yielded values.

  equal (v: vector[T]) returns (bool)
      % effects   returns true if self and a are the same object.

  similar (v: vector[T]) returns (bool)
        where T has similar (T) returns (bool)
      % effects   returns true if self and v have the same size and the elements at
      %           corresponding positions are similar (using the T similar method to do the test).

  copy ( ) returns (vector[T])
        where T has copy ( ) returns (T)
      % effects   returns a new vector 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 "vector[T]"
  vector_fill[T] (count: int, elem: T) returns (vector[T]) signals (negative_size)
      % effects   creates a new vector containing count elements each of which is elem.
      %           Signals negative size if count is negative.

  vector_create[T] (els: sequence[T]) returns (vector[T])
      % effects   returns a new vector containing the elements of the
      %           the sequence in order.

  vector_generate[T] (n: int, els: iter () yields (T)) returns (vector[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 vector containing the
      %           first n elements yielded by the iterator in order.


theta-questions@lcs.mit.edu