Sather Home Page

Section 8.5.1.2.1:
ARRAY

class ARRAY{T} < $ARR{T} is -- This class implements one-dimensional arrays of elements of type T. -- The operations provided include sorting, searching, etc. Array indices -- start at 0 and go up to `asize-1'. -- -- Most features here work when self is void. The intent is that -- a void array behaves just like a zero-sized array. Thus self may be void -- on operations which don't try to directly access specific elements since -- any such access would be out of range. create_from( elems : $ELT{T} ) : SAME is -- This routine creates an array out of the elements of "elems". It is -- expensive - first converts the argument into an FLIST to determine the -- number of elements and then converts the FLIST into an array. equals( arr : $CONTAINER{T} ) : BOOL is -- This predicate returns true if and only if all elements of self are -- the same as the corresponding elements of arr. inds : ARRAY{CARD} pre ~void(self) post (result.size = size) is -- This routine returns an index array which is the same size as self -- and is set to the values of the indices. resize( new_size : CARD ) : SAME pre ~void(self) post (result.size = new_size) is -- This routine allocates a new array and copies whatever will fit of -- self. It then returns the new array. copy( src : SAME ) pre ~void(self) and ~void(src) post true -- (self.size >= src.size) -- and {forall ind member dom.src -- | self(ind) = src(ind)} -- and {forall ind member dom.self -- and ~member dom.src -- | self(ind) = -- initial(self(ind)) -- or (self.size < src.size) -- and {forall ind member dom.self -- | self(ind) = src(ind)} is -- This routine copies as many elements from src to self as will fit. -- Built-in to this implementation. copy( beg : CARD, src : SAME ) pre ~void(self) and ((beg <= (size - 1)) or (src.size = 0)) post true -- + vdm stuff!?! is -- This routine copies as many elements from src to self as will fit -- when starting at index beg of self. Self may not be void but src may -- be void. Built-in to this implementation. copy( beg, num : CARD, src : SAME ) pre ~void(self) and ~void(src) and (beg <= (size - 1)) and (num <= (size - beg)) and (num <= src.size) post (self.size = initial(self.size)) is -- This routine copies num elements from src to self starting at index -- beg of self. Neither self nor src may be void. Built-in to this -- implementation. copy( beg, num, srcbeg : CARD, src : SAME ) pre ~void(self) and ~void(src) and (beg <= (size - 1)) and (num <= (size - beg)) and (num <= (src.size - srcbeg)) post (self.size = initial(self.size)) is -- This routine copies num elements from src to self starting at index -- beg of self and index srcbeg of src. Neither self nor src may be void. -- Built-in to this implementation. subarr( beg, num : CARD ) : SAME pre ~void(self) and (beg <= (size - 1)) and (num = (size - beg)) post (result.size = num) is -- This routine returns a new array which is a sub-sequence of self, -- with num entries copied from self starting at beg. Self may not be void. to_reverse pre ~void(self) post true -- and is reversed, of course! is -- This routine reverses the order of the elements in self. Self may -- not be void. reverse : SAME pre true post void(result) or true -- (result.to_reverse = self) is -- This routine returns a copy of self with all elements in reverse -- order. Self may be void. to( src : SAME ) pre (src.size = size) post true -- (src = self) is -- This routine sets self to be a copy of src. Both may be void! to_val( val : T ) pre true post void(self) or (count(val) = size) is -- This routine sets each element of self to val. Self may be void. append( arr : SAME ) : SAME pre true post true -- + vdm stuff !?! is -- This routine returns a new array consisting of self followed by -- arr. Both may be void. append( arr1, arr2 : SAME ) : SAME pre true post true -- + vdm stuff !?! is -- This routine returns a new array consisting of self followed by -- arr1 and then arr2. This is more efficient than two appends. Any of -- the arrays may be void. append( arr1, arr2, arr3 : SAME ) : SAME pre true post true -- + vdm stuff !?! is -- This routine returns a new array consisting of self followed by -- arr1 then arr2 and finally arr3. This is more efficient than three -- appends. Any of the arrays may be void. remove( elem : T ) : SAME pre true post ~result.contains(elem) is -- This routine returns a new array in which no elements are elt_eq to -- elem. Self may be void. remove_if( test : ROUT{T} : BOOL ) : SAME pre void(self) or ~void(test) post true is -- This routine returns a new array without those elements which satisfy -- the given test. Self may be void. is_sorted : BOOL is -- This predicate returns true if and only if the elements of self are -- sorted according to the relation elt_lt. Self may be void. remove_duplicates : SAME pre is_sorted post (result.size <= size) is -- This routine returns a new arry with only the first copy of -- duplicated elements. Self may be void. If not it must be sorted -- prior to calling this routine. to_replace( old, new_val : T ) pre true post (initial(count(old)) <= count(new_val)) is -- This routine replaces those elements that are elt_eq to old by -- new_val wherever the match is found. Self may be void. to_replace_if( test : ROUT{T} : BOOL, new_val : T ) pre void(self) or ~void(test) post true is -- This routine replaces all of the elements of self which satisfy -- the given test by new_val. Self may be void. find_if( test : ROUT{T} : BOOL ) : T pre void(self) or ~void(test) post true is -- Searching from the lowest index, this routine returns the first -- element which satisfies the given test. If there is no such element -- then void is returned. Self may be void. index_if( test : ROUT{T} : BOOL ) : CARD pre void(self) or ~void(test) post (result = CARD::maxval) or test.call([result]) is -- This routine returns the index of the first element, searching from -- zero, which satisfies the given test. If there is no such element then -- CARD::maxval is returned. Self may be void. mismatch( arr : SAME ) : CARD pre true post (result = CARD::maxval) or (result < asize) is -- This routine returns the index of the first element of self which -- differs from the corresponding element of arr. If there is no such -- element or self is void or self is a prefix of arr then CARD::maxval is -- returned. search( arr : SAME ) : CARD pre true post (result = CARD::maxval) or (result < asize) is -- This routine returns the index of the first sub-array of self, -- starting at zero, which matches arr. If self is void or there is no -- match then CARD::maxval is returned. search( beg : CARD, arr : SAME ) : CARD pre ~void(self) and ~void(arr) and (beg <= (asize - 1)) post (result = CARD::maxval) or (result < asize) is -- This routine returns the index of the beginning of the first -- sub-array of self which matches arr, starting at zero. If there is no -- such sub-array then CARD::maxval is returned. map( operation : ROUT{T} : T ) pre ~void(operation) post true is -- This routine sets each element of self to be the result of applying -- the given operation to it. Self may be void. reduce( operation : ROUT{T,T} : T ) : T pre ~void(operation) post true is -- This routine returns the result of applying the given operation -- to self from the lowest index to the highest. Void is returned if self -- is void. scan( operation : ROUT{T,T} : T ) pre ~void(operation) post true is -- This routine sets each element of self to be the result of applying -- the given operation to adjacent elements of self from lowest to highest -- index. The element with index zero is not altered. Self may be void. insertion_sort_range( lower, upper : CARD ) pre (self.size > 1) and (lower < upper) and (upper <= (asize - 1)) post is_sorted(lower,upper) is -- This routine performs an insertion sort of the elements of self -- in the index range lower to upper. Ordering is defined by elt_lt. quicksort_range( lower, upper : CARD ) pre (self.size > 1) and (lower < upper) and (upper <= (asize - 1)) post is_sorted(lower,upper) is -- This routine implements part of the quicksort algorithm to search -- the range of self between lower and upper according to the relation elt_lt sort pre true post is_sorted is -- This routine sorts the entire array using quick-sort with respect -- to elt_lt. Self may be void. stable_sort pre true post is_sorted is -- This routine uses an insertion sort to permute the elements of self -- so that it is sorted with respect to elt_lt and equal elements are in -- the original order. Self may be void. binary_search( elem : T ) : CARD pre is_sorted post (result = CARD::maxval) or (result < asize) is -- This search routine assumes that self is sorted. It returns -- the index of the element preceding the first element greater than elem -- according to elt_lt or CARD::maxval if self is void or if all elements -- are greater than elem. is_sorted_by( order : ROUT{T,T} : BOOL ) : BOOL pre void(self) or ~void(order) post true is -- This predicate returns true if and only if the elements of self are -- sorted in an order specified by the order routine which is expected to -- return true if and only if its first argument is less than its second -- argument. Self may be void. insertion_sort_by( order : ROUT{T,T} : BOOL ) pre ~void(order) post is_sorted_by(order) is -- This routine sorts the elements of self using insertion sorting -- in an order specified by the order routine which is expected to return -- true if and only if its first argument is less than its second argument. -- Self may be void. binary_search_by( elem : T, order : ROUT{T,T} : BOOL ) : CARD pre ~void(order) and is_sorted_by(order) post (result = CARD::maxval) or (result < asize) is -- This routine returns the index of the element of self preceding -- the first element greater than elem unless self is void or all elements -- are greater then elem, when CARD::maxval is returned. merge_with_by( arr : SAME, order : ROUT{T,T} : BOOL ) : SAME pre is_sorted_by(order) and arr.is_sorted_by(order) post result.is_sorted_by(order) is -- This routine returns a new array with the elements of self merged -- together according to order which should return true if and only if its -- first argument is less than its second. select( index : CARD ) pre ~void(self) and (index < size) post true is -- This routine moves the elements of self so that the element with -- the given index is not elt_lt any element with lower indices and -- no element with a larger index is elt_lt it. median : T pre true post void(result) or self.contains(result) is -- This routine returns the median of the elements contained in self -- according to the ordering relation elt_lt. This permutes the elements -- of self. Void is returned if self is void. select_by( order : ROUT{T,T} : BOOL, index : CARD ) pre ~void(self) and ~void(order) and (index < size) post true is -- This routine moves the elements of self so that the element with -- the given index is not less than (as determined by the order routine, -- which is expected to return true if and only if its first argument is -- less than its second) any element with lower indices and no element with -- a larger index is less than it. elt!( once beg : CARD ) : T pre ~void(self) and (beg <= (asize - 1)) post contains(result) -- (result = [ind!]) is -- This iter yields each element of self starting at `beg'. Self may -- not be void. Built-in to this implementation. builtin ARRAY_ELT_BEGB end ; elt!( once beg, once num : CARD ) : T pre ~void(self) and (beg <= (size - 1)) and (num <= (size - beg)) post contains(result) -- (result = [beg + ind!]) is -- This iter yields num successive elements of self starting at -- index beg. Self may not be void. Built-in to this implementation. builtin ARRAY_ELT_BEG_NUMB end ; elt!( once beg, once num : CARD, once step : INT ) : T pre ~void(self) and is_legal_aelts_arg(beg,num,step) post contains(result) -- (result = [beg + ind!]) is -- This iter yields num elements of self starting at beg and stepping -- by step which must not be zero. Self may not be void. Built-in to this -- implementation. builtin ARRAY_ELT_BEG_NUM_STEPB end ; set!( once beg : CARD, val : T ) pre ~void(self) and (beg <= (size - 1)) post contains(val) -- ([beg + ind!] = val) is -- This iter sets successive elements starting at beg to the values of -- val. Self may not be void. Built-in to this implementation. set!( once beg, once num : CARD, val : T ) pre ~void(self) and (beg <= (size - 1)) and (num <= (size - beg)) post contains(val) -- ([beg + ind!] = val) is -- This iter sets num successive elements of self starting at beg -- to the values of val. Self may not be void. Built-in to this -- implementation. set!( once beg, once num : CARD, once step : INT, val : T ) pre ~void(self) and is_legal_aelts_arg(beg,num,step) post contains(val) -- + vdm stuff !?! is -- This iter sets num elements of self starting at beg stepping by -- step to the values of val. Step must not be zero. Self may not be void. -- Built-in to this implementation. ind1! : CARD pre true post (result < size) is -- This iter yields the indices of self in order. Self may be void. -- Built-in to this implementation. -- -- NOTE This is provided for consistency with ARRAY2 and ARRAY3 classes.
Language Index Library Index Container Index
Comments or enquiries should be made to Keith Hopper.
Page last modified: Thursday, 25 May 2000.
Produced with Amaya