SA.1 ARRAYS 1166.9 wipe_out is --Make array empty do make_area(capacity) end; clear all is --Reset all items to default values. do spclearall (Sarea) ensure all cleared:all cleared end; feature--Resizing grow (i:INTEGER)is --Change the capacity to at least'i'. do if i capacity then resize (lower,upper +i-capacity) end end; resize(minindex,maxindex:INTEGER)is --Rearrange array so that it can accommodate --indices down to'minindex'and up to'maxindex'. --Do not lose any previously entered item. require good_indices:minindex <maxindex local old_size,new_size,old_count:INTEGER:
§A.1 ARRAYS 1166.9 wipe_out is -- Make array empty. do make_area (capacity) end; clear_all is -- Reset all items to default values. do spclearall ($area) ensure all_cleared: all_cleared end; feature -- Resizing grow (i: INTEGER) is -- Change the capacity to at least `i’. do if i > capacity then resize (lower, upper + i - capacity) end end; resize (minindex, maxindex: INTEGER) is -- Rearrange array so that it can accommodate -- indices down to `minindex’ and up to `maxindex’. -- Do not lose any previously entered item. require good_indices: minindex <= maxindex local old_size, new_size, old_count: INTEGER;
1166. EXTRACTS FROM THE BASE LIBRARY SA.I new_lower,new_upper:INTEGER; do if empty_area then new_lower:=minindex; new_upper:=maxindex else if minindex lower then new_lower:=minindex else new_lower:=lower end; if maxindex upper then new_upper:=maxindex else new_upper upper end end; new_size :new_upper-new_lower +1; if not empty_area then old size:=area.count; old_count :upper-lower +1 end; if empty_area then make_area(new_size); elseif new_size >old_size or new_lower lower then area:=arycpy(Sarea,new_size, lower-new_lower,old_count) end; lower :new_lower; upper :new_upper ensure no_low_lost:lower minindex.min (old lower);
1166. EXTRACTS FROM THE BASE LIBRARY §A.1 new_lower, new_upper: INTEGER; do if empty_area then new_lower := minindex; new_upper := maxindex else if minindex < lower then new_lower := minindex else new_lower := lower end; if maxindex > upper then new_upper := maxindex else new_upper := upper end end; new_size := new_upper - new_lower + 1; if not empty_area then old_size := area.count; old_count := upper - lower + 1 end; if empty_area then make_area (new_size); elseif new_size > old_size or new_lower < lower then area := arycpy ($area, new_size, lower - new_lower, old_count) end; lower := new_lower; upper := new_upper ensure no_low_lost: lower = minindex.min (old lower);
SA.1 ARRAYS 1166. no_high_lost:upper maxindex.max (old upper) end; feature--Conversion to c:ANY is --Address of actual sequence of values, --for passing to external(non-Eiffel)routines. do Result :area end; linear_representation:LINEAR [G]is --Representation as a linear structure local temp:ARRAYED_LIST [G]; i:INTEGER; do !temp.make(capacity ) from i:=lower; until i>upper loop temp.extend (item (i)); i=i+1, end; Result:=temp; end; feature--Duplication
§A.1 ARRAYS 1166. no_high_lost: upper = maxindex.max (old upper) end; feature -- Conversion to_c: ANY is -- Address of actual sequence of values, -- for passing to external (non-Eiffel) routines. do Result := area end; linear_representation: LINEAR [G] is -- Representation as a linear structure local temp: ARRAYED_LIST [G]; i: INTEGER; do !! temp.make (capacity); from i := lower; until i > upper loop temp.extend (item (i)); i := i + 1; end; Result := temp; end; feature -- Duplication
1166. EXTRACTS FROM THE BASE LIBRARY SA.I copy (other:like Current)is --Reinitialize by copying all the items of'other'. --(This is also used by 'clone'.) do standard_copy (other); set area (standard_clone (other.area)); ensure then equal_areas:area.is_equal (other.area) end; subarray(start_pos,end_pos:INTEGER):like Current is --Array made of items of current array within -bounds 'start_pos'and 'end_pos'. require valid_start_pos:valid_index(start_pos) valid end pos:valid index (end_pos) valid_bounds:(start_pos <=end_pos)or(start_pos=end_pos+1) do !Result.make(start_pos,end_pos); Result.subcopy (Current,start_pos,end_pos,start_pos) ensure lower:Result.lower start_pos; upper:Result.upper end_pos; -copied:forall'i'in'start_pos'..'end_pos', - Result.item (i)=item(i) end feature--Obsolete duplicate:like Current is obsolete"Useclone'" do Result :clone(Current)
1166. EXTRACTS FROM THE BASE LIBRARY §A.1 copy (other: like Current) is -- Reinitialize by copying all the items of `other’. -- (This is also used by `clone’.) do standard_copy (other); set_area (standard_clone (other.area)); ensure then equal_areas: area.is_equal (other.area) end; subarray (start_pos, end_pos: INTEGER): like Current is -- Array made of items of current array within -- bounds `start_pos’ and `end_pos’. require valid_start_pos: valid_index (start_pos) valid_end_pos: valid_index (end_pos) valid_bounds: (start_pos <= end_pos) or (start_pos = end_pos + 1) do !! Result.make (start_pos, end_pos); Result.subcopy (Current, start_pos, end_pos, start_pos) ensure lower: Result.lower = start_pos; upper: Result.upper = end_pos; -- copied: forall `i’ in `start_pos’ .. `end_pos’, -- Result.item (i) = item (i) end feature -- Obsolete duplicate: like Current is obsolete “Use ``clone’’” do Result := clone (Current)
SA.1 ARRAYS 1166. end; feature NONE)--Inapplicable prune (v:G)is --Remove first occurrence of'v'if any. --(Precondition is false.) do end; extend (v:G)is --Add'v'to structure. --(Precondition is false.) do end; feature (ARRAY)--Implementation arycpy (old_area:POINTER;newsize,s,n:INTEGER):like area is --New area of size'newsize'containing'n'items --from'oldarea'. --Old items are at position's'in new area. external “C” end; feature (NONE--Implementation auto_resize(minindex,maxindex:INTEGER)is --Rearrange array so that it can accommodate --indices down to'minindex'and up to'maxindex'. --Do not lose any previously entered item
§A.1 ARRAYS 1166. end; feature {NONE} -- Inapplicable prune (v: G) is -- Remove first occurrence of `v’ if any. -- (Precondition is false.) do end; extend (v: G) is -- Add `v’ to structure. -- (Precondition is false.) do end; feature {ARRAY} -- Implementation arycpy (old_area: POINTER; newsize, s, n: INTEGER): like area is -- New area of size `newsize’ containing `n’ items -- from `oldarea’. -- Old items are at position `s’ in new area. external “C” end; feature {NONE} -- Implementation auto_resize (minindex, maxindex: INTEGER) is -- Rearrange array so that it can accommodate -- indices down to `minindex’ and up to `maxindex’. -- Do not lose any previously entered item