Next: Strings
Up: Indexed collections: vector, array,
Previous: Indexed collections: vector, array,
Index
Several concrete implementations of indexed collections
exist:
i_vector
s are primitive fixed-length immutable indexed
collections,
item m_vector
s are primitive fixed-length mutable indexed
collections,
- arrays are extensible mutable indexed collections,
- intervals are immutable indexed collections representing a finite
arithmetic sequence of integers,
- strings are indexed collections of
characters, with mutable and immutable varieties,
bit_vector
s
are fixed-length mutable indexed collections of 0/1 values,
word_vector
s are fixed-length mutable indexed collections
of single-word short integers, and
float_vector
s are fixed-length mutable indexed collections
of floating-point values (short integers).
In vector.diesel:
i_vector
is a primitive implementation of fixed-length immutable
indexed collections. Besides the new_
methods (see the next
paragraph on how they work), instances of i_vector
can be created
by Diesel vector constructor expressions, e.g., [3, 4, 5, x*12].
m_vector
is a primitive implementation of fixed-length mutable
indexed collections.
The new_(i|m)_vector
function constructs a new (i|m)_vector
of a
given size all of whose elements are filler. The
new_(i|m)_vector_init function
constructs a (i|m)_vector
of a
particular size and uses the elems closure to construct the initial
values of the vector's elements from the indices. The
new_(i|m)_vector_init_from
function constructs a (i|m)_vector
of the
same length as some other ordered collection and applies a mapping
function to translate each element of the receiver collection into
the corresponding vector element; new_(i|m)_vector_init_from
is
simply the well-known map function, specialized to return a
particular vector representation. In the current implementation,
vectors cannot be subclassed.
module Vector;
abstract class vector[`T] isa indexed[T],
- type parameter is covariant:
vector[`S >= T];
abstract class vector_exactly[`T] isa indexed_exactly[T], vector[T];
fun as_vector(c:collection[`T]):vector[T];
A vector_factory
is the abstract superclass of factories for
vectors.
abstract class vector_factory isa indexed_factory;
abstract class i_vector[T] isa vector[T], i_indexed[T],
i_vector[`S >= T];
abstract class i_vector_exactly[T] isa vector_exactly[T],
i_indexed_exactly[T],
i_vector[T];
fun new_i_vector(size:int, filler:`T):i_vector_exactly[T];
fun new_i_vector[T](size:int, filler:T):i_vector_exactly[T];
fun new_i_vector_init[T](size:int, cl:&(int):T):i_vector_exactly[T];
fun new_i_vector_from(c:collection[`T1],
cl:&(T1):`T2):i_vector_exactly[T2];
fun new_i_vector_init_from[T2](c:collection[`T1],
cl:&(T1):T2):i_vector_exactly[T2];
fun new_i_vector_from_associations(c:table[`K,`T1],
cl:&(K,T1):`T2
):i_vector_exactly[T2];
fun new_i_vector_init_from_associations[T2](c:table[`K,`T1],
cl:&(K,T1):T2
):i_vector_exactly[T2];
fun as_i_vector(c:collection[`T]):i_vector[T];
fun as_i_vector[T](c:collection[T]):i_vector_exactly[T];
An i_vector_factory
is the concrete factory object for
i_vectors.
object i_vector_factory isa vector_factory, i_indexed_factory;
abstract class m_vector[T] isa vector_exactly[T], m_indexed[T];
fun new_m_vector(size:int):m_vector[dynamic];
fun new_m_vector[T](size:int):m_vector[T];
fun new_m_vector(size:int, filler:`T):m_vector[T];
fun new_m_vector[T](size:int, filler:T):m_vector[T];
fun new_m_vector_init[T](size:int, cl:&(int):T):m_vector[T];
fun new_m_vector_from(c:collection[`T1], cl:&(T1):`T2):m_vector[T2];
fun new_m_vector_init_from[T2](c:collection[`T1],
cl:&(T1):T2):m_vector[T2];
fun new_m_vector_from_associations(c:table[`K,`T1],
cl:&(K,T1):`T2):m_vector[T2];
fun new_m_vector_init_from_associations[T2](c:table[`K,`T1],
cl:&(K,T1):T2
):m_vector[T2];
resize returns a new m_vector
new_size
long whose first
elements are copied from v. if new_size
is greater than
v.length, then the extra elements are initialized to filler.
fun resize(v:vector[`T], new_size:int, filler:T):m_vector[T];
fun as_m_vector(c:collection_exactly[`T]):m_vector[T];
fun as_m_vector[T](c:collection[T]):m_vector[T];
An m_vector_factory
is the concrete factory object for
m_vectors.
object m_vector_factory isa vector_factory, m_indexed_factory;
In interval.diesel:
intervals are immutable indexed collections representing a finite
arithmetic sequence of integers. An interval returned by
new_interval(start, stop)
represents the integers in order from
start to stop, inclusive; if stop is less than start then the
sequence is empty. The interval may optionally specify a step value
between start and stop. If the step is negative, then the sequence
goes from start down to stop, inclusive; if stop is greater than
start, then the sequence is empty.
module Interval;
class interval isa i_indexed_exactly[int];
field start(:interval):int;
field stop(:interval):int;
field step(:interval):int;
fun new_interval(start:int, stop:int):interval;
fun new_interval(start:int, stop:int, step:int):interval;
Intervals can be used to implement regular for loops. For example,
for i := 1 to 10 do ... end
can be expressed in Diesel as:
new_interval(1, 10).do(&(i:int){
...
});
The for methods provide a convenient interface for this idiom; they
also currently implement this idiom more efficiently. (Recently,
compiler optimizations have been implemented that greatly reduce the
performance cost of the new_interval(...).do(...) version; if
debug_support is disabled, there should be no difference in
performance between new_interval(...).do(...) and for(...).)
fun for(start:int, stop:int, body:&(int):void):void;
fun for(start:int, stop:int, step:int, body:&(int):void):void;
In bit-vector.diesel:
A bit vector is a dense representation of a mutable indexed
collection of zeros and ones. A new bit vector of zeros can be
created with new_bit_vector
. A bit vector can be resized in place;
if expanded, then the new positions are filled in with zeros.
module BitVector;
class bit_vector isa m_indexed[int];
field method length(@bit_vector):int;
fun new_bit_vector(sz:int):bit_vector;
fun resize(t:bit_vector, new_num_bits:int):bit_vector;
fun =_or_zero(l:bit_vector, r:bit_vector):bool;
Bit vectors can be or'd, and'd, xor'd, subtracted, and negated,
all bitwise, to compute new bit vectors from old. For binary
operations, the shorter bit vector is zero-extended before being
operated on. The ..._in_place
versions try to construct the
new bit_vector by overwriting the first bit vector, but if the
first bit vector is too short, it will construct and return a
new bit vector
fun bit_and(l:bit_vector, r:bit_vector):bit_vector;
fun bit_and_in_place(l:bit_vector, r:bit_vector):bit_vector;
fun bit_or(l:bit_vector, r:bit_vector):bit_vector;
fun bit_or_in_place(l:bit_vector, r:bit_vector):bit_vector;
fun bit_xor(l:bit_vector, r:bit_vector):bit_vector;
fun bit_xor_in_place(l:bit_vector, r:bit_vector):bit_vector;
fun bit_xnor(l:bit_vector, r:bit_vector):bit_vector;
fun bit_xnor_in_place(l:bit_vector, r:bit_vector):bit_vector;
fun bit_not(l:bit_vector):bit_vector;
fun bit_not_in_place(l:bit_vector):bit_vector;
fun bit_difference(l:bit_vector, r:bit_vector):bit_vector;
fun bit_difference_in_place(l:bit_vector, r:bit_vector):bit_vector;
A bit vector can be mutated to be all zeros (clear_all_bits
)
or all ones (set_all_bits
) or tested for those conditions
(is_all_zeros
and is_all_ones
). The is_disjoint_bits
method returns true if its arguments share no set bits;
is_disjoint_bits(a,b)
is equivalent to
is_all_zeros(bit_and(a,b))
. The includes_all_bits
method
returns true if its first argument includes all the 1 bits of
its second argument; includes_all_bits(a,b)
is equivalent to
=_or_zero(bit_and(a,b),b)
.
fun includes_all_bits(l:bit_vector, r:bit_vector):bool;
fun is_disjoint_bits(l:bit_vector, r:bit_vector):bool;
fun clear_all_bits(v:bit_vector):void;
fun set_all_bits(v:bit_vector):void;
fun is_all_zeros(v:bit_vector):bool;
fun is_all_ones(v:bit_vector):bool;
The do_ones
control structure iterates over all the set
positions in the bit vector; this operation runs faster than a
regular do loop containing a test in each iteration.
fun do_ones(v:bit_vector, cl:&(int):void):void;
In byte-vector.diesel:
module ByteVector;
abstract class byte_vector isa indexed_exactly[int];
fun as_byte_vector(c:collection[int]):byte_vector;
fun new_i_byte_vector(size:int, filler_oop:int):i_byte_vector;
fun new_i_byte_vector_init(size:int, cl:&(int):int):i_byte_vector;
fun new_i_byte_vector_init_from(c:collection[`T],
cl:&(T):int):i_byte_vector;
fun as_i_byte_vector(c:collection[int]):i_byte_vector;
fun new_m_byte_vector(size:int):m_byte_vector;
fun new_m_byte_vector(size:int, filler_oop:int):m_byte_vector;
fun new_m_byte_vector_init(size:int, cl:&(int):int):m_byte_vector;
fun new_m_byte_vector_init_from(c:collection[`T],
cl:&(T):int):m_byte_vector;
fun as_m_byte_vector(c:collection[int]):m_byte_vector;
In short-vector.diesel:
module ShortVector;
abstract class short_vector isa indexed_exactly[int];
fun as_short_vector(c:collection[int]):short_vector;
fun new_i_short_vector(size:int, filler_oop:int):i_short_vector;
fun new_i_short_vector_init(size:int, cl:&(int):int):i_short_vector;
fun new_i_short_vector_init_from(c:ordered_collection[`T],
cl:&(T):int):i_short_vector;
fun as_i_short_vector(c:collection[int]):i_short_vector;
fun new_m_short_vector(size:int):m_short_vector;
fun new_m_short_vector(size:int, filler_oop:int):m_short_vector;
fun new_m_short_vector_init(size:int, cl:&(int):int):m_short_vector;
fun new_m_short_vector_init_from(c:ordered_collection[`T],
cl:&(T):int):m_short_vector;
fun as_m_short_vector(c:collection[int]):m_short_vector;
In word-vector.diesel:
module WordVector;
abstract class word_vector isa indexed_exactly[int];
fun as_word_vector(c:collection[int]):word_vector;
fun new_i_word_vector(size:int, filler_oop:int):i_word_vector;
fun new_i_word_vector_init(size:int, cl:&(int):int):i_word_vector;
fun new_i_word_vector_init_from(c:ordered_collection[`T],
cl:&(T):int):i_word_vector;
fun as_i_word_vector(c:collection[int]):i_word_vector;
fun new_m_word_vector(size:int):m_word_vector;
fun new_m_word_vector(size:int, filler_oop:int):m_word_vector;
fun new_m_word_vector_init(size:int, cl:&(int):int):m_word_vector;
fun new_m_word_vector_init_from(c:ordered_collection[`T],
cl:&(T):int):m_word_vector;
fun as_m_word_vector(c:collection[int]):m_word_vector;
In float-vector.diesel:
module FloatVector;
abstract class float_vector isa indexed_exactly[float];
fun as_float_vector(c:collection[float]):float_vector;
fun new_i_float_vector(size:int, filler_oop:float):i_float_vector;
fun new_i_float_vector_init(size:int, cl:&(int):float):i_float_vector;
fun new_i_float_vector_init_from(c:collection[`T],
cl:&(T):float):i_float_vector;
fun as_i_float_vector(c:collection[float]):i_float_vector;
fun new_m_float_vector(size:int):m_float_vector;
fun new_m_float_vector(size:int, filler_oop:float):m_float_vector;
fun new_m_float_vector_init(size:int, cl:&(int):float):m_float_vector;
fun new_m_float_vector_init_from(c:collection[`T],
cl:&(T):float):m_float_vector;
fun as_m_float_vector(c:collection[float]):m_float_vector;
Next: Strings
Up: Indexed collections: vector, array,
Previous: Indexed collections: vector, array,
Index
Cecil/Vortex Project