1166.4 EXTRACTS FROM THE BASE LIBRARY SA.I feature--Measurement lower:INTEGER; --Minimum index upper:INTEGER: --Maximum index count,capacity:INTEGER is --Number of available indices do Result :upper-lower +1 end; occurrences(v:G):INTEGER is --Number of times'v'appears in structure local i:INTEGER do if object_comparison then if v/=Void then from i:=lower until i>upper loop if item (i)/=Void and then v.is_equal (item (i)) then Result:Result+1 end i=i+1
1166.4 EXTRACTS FROM THE BASE LIBRARY §A.1 feature -- Measurement lower: INTEGER; -- Minimum index upper: INTEGER; -- Maximum index count, capacity: INTEGER is -- Number of available indices do Result := upper - lower + 1 end; occurrences (v: G): INTEGER is -- Number of times `v’ appears in structure local i: INTEGER do if object_comparison then if v /= Void then from i := lower until i > upper loop if item (i) /= Void and then v.is_equal (item (i)) then Result := Result + 1 end i := i + 1
SA.1 ARRAYS 1166.5 end end else from i:=lower until i>upper loop if item (i)=v then Result:=Result+1 end; i=i+1 end end; end; feature--Comparison is_equal (other:like Current):BOOLEAN is --Is array made of the same items as'other'? do Result :area.is_equal (other.area) end; feature--Status report consistent (other:like Current):BOOLEAN is --Is object in a consistent state so that'other' --may be copied onto it?(Default answer:yes). do Result :=(capacity other.capacity) end;
§A.1 ARRAYS 1166.5 end end else from i := lower until i > upper loop if item (i) = v then Result := Result +1 end; i := i + 1 end end; end; feature -- Comparison is_equal (other: like Current): BOOLEAN is -- Is array made of the same items as `other’? do Result := area.is_equal (other.area) end; feature -- Status report consistent (other: like Current): BOOLEAN is -- Is object in a consistent state so that `other’ -- may be copied onto it? (Default answer: yes). do Result := (capacity = other.capacity) end;
1166.6 EXTRACTS FROM THE BASE LIBRARY SA.I full:BOOLEAN is --Is structure filled to capacity?(Answer:yes) do Result:=true end; all cleared:BOOLEAN is --Are all items set to default values? local i:INTEGER: dead element:G; do from i:=lower variant upper +1-i until (i>upper)or else not (dead_element item (i)) loop i=i+1 end; Result :=i>upper; end; valid index(i:INTEGER):BOOLEAN is -Is'i'within the bounds of the array? do Result:=(lower <=i)and then (i<=upper) end; extendible:BOOLEAN is
1166.6 EXTRACTS FROM THE BASE LIBRARY §A.1 full: BOOLEAN is -- Is structure filled to capacity? (Answer: yes) do Result := true end; all_cleared: BOOLEAN is -- Are all items set to default values? local i: INTEGER; dead_element: G; do from i := lower variant upper + 1 - i until (i > upper) or else not (dead_element = item (i)) loop i := i + 1 end; Result := i > upper; end; valid_index (i: INTEGER): BOOLEAN is -- Is `i’ within the bounds of the array? do Result := (lower <= i) and then (i <= upper) end; extendible: BOOLEAN is
SA.1 ARRAYS 1166.7 --May items be added? --(Answer:no,although array may be resized.) do Result:=false end; prunable:BOOLEAN is --May items be removed?(Answer:no.) do Result :false end; feature--Element change frozen put,enter(v:like item;i:INTEGER)is --Replace'i'-th entry,if in index interval,by'v'. do area.put(v,i-lower); end; force (v:like item;i:INTEGER)is --Assign item'v'to'i'-th entry. --Always applicable:resize the array if'i'falls out of --currently defined bounds;preserve existing items. do ifi<lower then auto resize (i,upper); elseifi>upper then auto resize (lower,i); end; put (v,i) ensure
§A.1 ARRAYS 1166.7 -- May items be added? -- (Answer: no, although array may be resized.) do Result := false end; prunable: BOOLEAN is -- May items be removed? (Answer: no.) do Result := false end; feature -- Element change frozen put, enter (v: like item; i: INTEGER) is -- Replace `i’-th entry, if in index interval, by `v’. do area.put (v, i - lower); end; force (v: like item; i: INTEGER) is -- Assign item `v’ to `i’-th entry. -- Always applicable: resize the array if `i’ falls out of -- currently defined bounds; preserve existing items. do if i < lower then auto_resize (i, upper); elseif i > upper then auto_resize (lower, i); end; put (v, i) ensure
1166.8 EXTRACTS FROM THE BASE LIBRARY SA.I inserted:item (i)=v; higher_count:count>=old count end; subcopy (other:like Current;start_pos,end_pos,index_pos:INTEGER)is --Copy items of'other'within bounds'start_pos'and'end_pos' -to current array starting at index'index_pos'. require other_not_void:other/=Void; valid_start_pos:other.valid_index(start_pos) valid_end_pos:other.valid_index(end_pos) valid_bounds:(start_pos <=end_pos)or(start_pos=end_pos+1) valid_index_pos:valid_index(index_pos) enough_space:(upper-index_pos)>=(end_pos-start_pos) local other_area:like area; other_lower:INTEGER: start0,end0,index0:INTEGER do other_area:=other.area; other lower :other.lower; starto :start_pos-other_lower; endo:=end_pos-other_lower; index0:=index_pos-lower; spsubcopy (Sother_area,Sarea,starto,endo,index0) ensure --copied:forall'i'in0..('end_pos'-'start_pos'), -item (index pos +i)=other.item (start pos +i) end feature--Removal
1166.8 EXTRACTS FROM THE BASE LIBRARY §A.1 inserted: item (i) = v; higher_count: count >= old count end; subcopy (other: like Current; start_pos, end_pos, index_pos: INTEGER) is -- Copy items of `other’ within bounds `start_pos’ and `end_pos’ -- to current array starting at index `index_pos’. require other_not_void: other /= Void; valid_start_pos: other.valid_index (start_pos) valid_end_pos: other.valid_index (end_pos) valid_bounds: (start_pos <= end_pos) or (start_pos = end_pos + 1) valid_index_pos: valid_index (index_pos) enough_space: (upper - index_pos) >= (end_pos - start_pos) local other_area: like area; other_lower: INTEGER; start0, end0, index0: INTEGER do other_area := other.area; other_lower := other.lower; start0 := start_pos - other_lower; end0 := end_pos - other_lower; index0 := index_pos - lower; spsubcopy ($other_area, $area, start0, end0, index0) ensure -- copied: forall `i’ in 0 .. (`end_pos’-`start_pos’), -- item (index_pos + i) = other.item (start_pos + i) end feature -- Removal