note description: "Queues with a bounded physical size, implemented by arrays" library: "Free implementation of ELKS library" legal: "See notice at end of class." status: "See notice at end of class." names: dispenser, array representation: array access: fixed, fifo, membership size: fixed contents: generic date: "$Date: 2012-07-23 21:02:19 +0000 (Mon, 23 Jul 2012) $" revision: "$Revision: 91989 $" class BOUNDED_QUEUE [G] create make feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end make (n: INTEGER_32) -- Create queue for at most n items. -- (from ARRAYED_QUEUE) require -- from ARRAYED_QUEUE non_negative_argument: n >= 0 do create area.make_empty (n) out_index := 1 count := 0 ensure -- from ARRAYED_QUEUE capacity_expected: capacity = n is_empty: is_empty end feature -- Access generating_type: TYPE [detachable BOUNDED_QUEUE [G]] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generating_type_not_void: Result /= Void end generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty end has (v: like item): BOOLEAN -- Does queue include v? -- (Reference or object equality, -- based on object_comparison.) -- (from ARRAYED_QUEUE) require -- from CONTAINER True local i, j, nb: INTEGER_32 do i := out_index - Lower j := count nb := area.capacity if object_comparison then from until j = 0 or v ~ area.item (i) loop i := i + 1 if i = nb then i := 0 end j := j - 1 end else from until j = 0 or v = area.item (i) loop i := i + 1 if i = nb then i := 0 end j := j - 1 end end Result := j > 0 ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty end item: G -- Oldest item. -- (from ARRAYED_QUEUE) require -- from ACTIVE readable: readable do Result := area.item (out_index - Lower) end feature {ARRAYED_QUEUE, ARRAYED_QUEUE_ITERATION_CURSOR} -- Access area: SPECIAL [G] -- Storage for queue. -- (from ARRAYED_QUEUE) out_index: INTEGER_32 -- Position of oldest item. -- (from ARRAYED_QUEUE) feature {ARRAYED_QUEUE_ITERATION_CURSOR} -- Access Lower: INTEGER_32 = 1 -- Lower bound for accessing list items via indexes -- (from ARRAYED_QUEUE) feature -- Measurement additional_space: INTEGER_32 -- Proposed number of additional items -- (from RESIZABLE) do Result := (capacity // 2).max (Minimal_increase) ensure -- from RESIZABLE at_least_one: Result >= 1 end capacity: INTEGER_32 -- Number of items that may be stored -- (from ARRAYED_QUEUE) require -- from BOUNDED True do Result := area.capacity ensure -- from BOUNDED capacity_non_negative: Result >= 0 end count: INTEGER_32 -- Number of items -- (from ARRAYED_QUEUE) Growth_percentage: INTEGER_32 = 50 -- Percentage by which structure will grow automatically -- (from RESIZABLE) index_set: INTEGER_INTERVAL -- Range of acceptable indexes -- (from ARRAYED_QUEUE) do create Result.make (1, count) ensure then -- from ARRAYED_QUEUE count_definition: Result.count = count end Minimal_increase: INTEGER_32 = 5 -- Minimal number of additional items -- (from RESIZABLE) occurrences (v: G): INTEGER_32 -- Number of times v appears in structure -- (Reference or object equality, -- based on object_comparison.) -- (from ARRAYED_QUEUE) local i, j, nb: INTEGER_32 do i := out_index - Lower j := count nb := area.capacity if object_comparison then from until j = 0 loop if area.item (i) ~ v then Result := Result + 1 end i := i + 1 if i = nb then i := 0 end j := j - 1 end else from until j = 0 loop if area.item (i) = v then Result := Result + 1 end i := i + 1 if i = nb then i := 0 end j := j - 1 end end ensure -- from BAG non_negative_occurrences: Result >= 0 end feature {NONE} -- Measurement estimated_count_of (other: ITERABLE [G]): INTEGER_32 -- Estimated number of elements in other. -- (from CONTAINER) do if attached {FINITE [G]} other as f then Result := f.count elseif attached {READABLE_INDEXABLE [G]} other as r then Result := r.upper - r.lower + 1 end ensure -- from CONTAINER instance_free: class non_negative_result: Result >= 0 end feature -- Comparison frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void -- or attached to isomorphic object structures? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_deep_equal (b) end ensure -- from ANY instance_free: class shallow_implies_deep: standard_equal (a, b) implies Result both_or_none_void: (a = Void) implies (Result = (b = Void)) same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b)) symmetric: Result implies deep_equal (b, a) end frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached -- to objects considered equal? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b)) end frozen is_deep_equal alias "≡≡≡" (other: BOUNDED_QUEUE [G]): BOOLEAN -- Are Current and other attached to isomorphic object structures? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY shallow_implies_deep: standard_is_equal (other) implies Result same_type: Result implies same_type (other) symmetric: Result implies other.is_deep_equal (Current) end is_equal (other: BOUNDED_QUEUE [G]): BOOLEAN -- Is other attached to an object considered -- equal to current object? -- (from ARRAYED_QUEUE) require -- from ANY other_not_void: other /= Void local i, j: INTEGER_32 nb, other_nb: INTEGER_32 c: INTEGER_32 do c := count if c = other.count and object_comparison = other.object_comparison then i := out_index - Lower j := other.out_index - Lower nb := area.capacity other_nb := other.area.capacity Result := True if object_comparison then from until c = 0 or not Result loop Result := area.item (i) ~ other.area.item (j) j := j + 1 if j > other_nb then j := 0 end i := i + 1 if i = nb then i := 0 end c := c - 1 end else from until c = 0 or not Result loop Result := area.item (i) = other.area.item (j) j := j + 1 if j > other_nb then j := 0 end i := i + 1 if i = nb then i := 0 end c := c - 1 end end end ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result end frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached to -- field-by-field identical objects of the same type? -- Always uses default object comparison criterion. -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.standard_is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b)) end frozen standard_is_equal alias "≜" (other: BOUNDED_QUEUE [G]): BOOLEAN -- Is other attached to an object of the same type -- as current object, and field-by-field identical to it? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) end feature -- Status report changeable_comparison_criterion: BOOLEAN -- May object_comparison be changed? -- (Answer: yes by default.) -- (from CONTAINER) do Result := True end conforms_to (other: ANY): BOOLEAN -- Does type of current object conform to type -- of other (as per Eiffel: The Language, chapter 13)? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" end empty: BOOLEAN obsolete "ELKS 2000: Use `is_empty' instead. [2017-05-31]" -- Is there no element? -- (from CONTAINER) do Result := is_empty end extendible: BOOLEAN -- May items be added? (Answer: yes.) require -- from COLLECTION True do Result := not full end full: BOOLEAN -- Is structure full? -- (from BOUNDED) require -- from BOX True do Result := count = capacity end is_empty: BOOLEAN -- Is the structure empty? -- Was declared in ARRAYED_QUEUE as synonym of off. -- (from ARRAYED_QUEUE) require -- from CONTAINER True do Result := count = 0 end is_inserted (v: G): BOOLEAN -- Has v been inserted by the most recent insertion? -- (By default, the value returned is equivalent to calling -- has (v). However, descendants might be able to provide more -- efficient implementations.) -- (from COLLECTION) do Result := has (v) end object_comparison: BOOLEAN -- Must search operations use equal rather than = -- for comparing references? (Default: no, use =.) -- (from CONTAINER) off: BOOLEAN -- Is the structure empty? -- Was declared in ARRAYED_QUEUE as synonym of is_empty. -- (from ARRAYED_QUEUE) do Result := count = 0 end prunable: BOOLEAN -- May items be removed? (Answer: no.) -- (from ARRAYED_QUEUE) do Result := False end readable: BOOLEAN -- Is there a current item that may be read? -- (from DISPENSER) do Result := not is_empty end replaceable: BOOLEAN -- Can current item be replaced? -- (from ACTIVE) do Result := True end resizable: BOOLEAN -- May capacity be changed? (Answer: yes.) -- (from RESIZABLE) require -- from BOUNDED True do Result := True end same_type (other: ANY): BOOLEAN -- Is type of current object identical to type of other? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) end writable: BOOLEAN -- Is there a current item that may be modified? -- (from DISPENSER) do Result := not is_empty end feature -- Status setting compare_objects -- Ensure that future search operations will use equal -- rather than = for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion: changeable_comparison_criterion do object_comparison := True ensure -- from CONTAINER object_comparison end compare_references -- Ensure that future search operations will use = -- rather than equal for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion: changeable_comparison_criterion do object_comparison := False ensure -- from CONTAINER reference_comparison: not object_comparison end feature -- Element change append (s: SEQUENCE [G]) -- Append a copy of s. -- (Synonym for fill) -- (from DISPENSER) require -- from DISPENSER s_not_void: s /= Void extendible: extendible do fill (s) end extend (v: G) -- Add v as newest item. -- Was declared in ARRAYED_QUEUE as synonym of put and force. -- (from ARRAYED_QUEUE) require -- from COLLECTION extendible: extendible local l_capacity: like capacity l_count: like count do l_capacity := capacity l_count := count if l_count >= l_capacity then grow (l_capacity + additional_space) end; area.force (v, in_index - Lower) count := l_count + 1 ensure -- from COLLECTION item_inserted: is_inserted (v) end fill (other: CONTAINER [G]) -- Fill with as many items of other as possible. -- The representations of other and current structure -- need not be the same. -- (from COLLECTION) require -- from COLLECTION other_not_void: other /= Void extendible: extendible local lin_rep: LINEAR [G] do lin_rep := other.linear_representation from lin_rep.start until not extendible or else lin_rep.off loop extend (lin_rep.item); lin_rep.forth end end force (v: G) -- Add v as newest item. -- Was declared in ARRAYED_QUEUE as synonym of extend and put. -- (from ARRAYED_QUEUE) require -- from DISPENSER extendible: extendible local l_capacity: like capacity l_count: like count do l_capacity := capacity l_count := count if l_count >= l_capacity then grow (l_capacity + additional_space) end; area.force (v, in_index - Lower) count := l_count + 1 ensure -- from DISPENSER item_inserted: is_inserted (v) end put (v: G) -- Add v as newest item. -- Was declared in ARRAYED_QUEUE as synonym of extend and force. -- (from ARRAYED_QUEUE) require -- from COLLECTION extendible: extendible local l_capacity: like capacity l_count: like count do l_capacity := capacity l_count := count if l_count >= l_capacity then grow (l_capacity + additional_space) end; area.force (v, in_index - Lower) count := l_count + 1 ensure -- from COLLECTION item_inserted: is_inserted (v) end replace (v: like item) -- Replace oldest item by v. -- (from ARRAYED_QUEUE) require -- from ACTIVE writable: writable replaceable: replaceable do area.put (v, out_index - Lower) ensure -- from ACTIVE item_replaced: item = v end feature -- Removal prune (v: G) -- Remove one occurrence of v if any. -- (Reference or object equality, -- based on object_comparison.) -- (from ARRAYED_QUEUE) require -- from COLLECTION prunable: prunable do end prune_all (v: G) -- Remove all occurrences of v. -- (Reference or object equality, -- based on object_comparison.) -- (from ARRAYED_QUEUE) require -- from COLLECTION prunable: prunable do ensure -- from COLLECTION no_more_occurrences: not has (v) end remove -- Remove oldest item. -- (from ARRAYED_QUEUE) require -- from ACTIVE prunable: prunable writable: writable require else -- from ARRAYED_QUEUE writable: writable local l_removed_index: like out_index do l_removed_index := out_index out_index := l_removed_index \\ capacity + 1 count := count - 1 if count = 0 then wipe_out else area.put (newest_item, l_removed_index - Lower) end end wipe_out -- Remove all items. -- (from ARRAYED_QUEUE) require -- from COLLECTION prunable: prunable require else -- from ARRAYED_QUEUE prunable: True do area.wipe_out out_index := 1 count := 0 ensure -- from COLLECTION wiped_out: is_empty end feature -- Resizing automatic_grow -- Change the capacity to accommodate at least -- Growth_percentage more items. -- (from RESIZABLE) require -- from RESIZABLE resizable: resizable do grow (capacity + additional_space) ensure -- from RESIZABLE increased_capacity: capacity >= old capacity + old additional_space end trim -- Decrease capacity to the minimum value. -- Apply to reduce allocated storage. -- (from ARRAYED_QUEUE) require -- from RESIZABLE True local i: like Lower j: like Lower n: like count m: like capacity do n := count m := capacity if n < m then i := out_index - Lower j := in_index - Lower if i < j then area.move_data (i, 0, n) out_index := Lower elseif n > 0 then area.move_data (i, j, m - i) out_index := j + Lower end area := area.aliased_resized_area (n) end ensure -- from RESIZABLE same_count: count = old count minimal_capacity: capacity = count ensure then -- from ARRAYED_QUEUE same_items: linear_representation ~ old linear_representation end feature -- Conversion linear_representation: ARRAYED_LIST [G] -- Representation as a linear structure -- (in the original insertion order) -- (from ARRAYED_QUEUE) require -- from CONTAINER True local i, j, nb: INTEGER_32 do from i := out_index - Lower j := count nb := area.capacity create Result.make (j) until j = 0 loop Result.extend (area.item (i)) i := i + 1 if i = nb then i := 0 end j := j - 1 end end feature -- Duplication frozen clone (other: detachable ANY): like other obsolete "Use `twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- equal to other -- -- For non-void other, clone calls copy; -- to change copying/cloning semantics, redefine copy. -- (from ANY) do if other /= Void then Result := other.twin end ensure -- from ANY instance_free: class equal: Result ~ other end copy (other: BOUNDED_QUEUE [G]) -- Update current object using fields of object attached -- to other, so as to yield equal objects. -- (from ARRAYED_QUEUE) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) do if other /= Current then standard_copy (other) area := area.twin end ensure -- from ANY is_equal: Current ~ other end frozen deep_clone (other: detachable ANY): like other obsolete "Use `deep_twin' instead. [2017-05-31]" -- Void if other is void: otherwise, new object structure -- recursively duplicated from the one attached to other -- (from ANY) do if other /= Void then Result := other.deep_twin end ensure -- from ANY instance_free: class deep_equal: deep_equal (other, Result) end frozen deep_copy (other: BOUNDED_QUEUE [G]) -- Effect equivalent to that of: -- copy (other . deep_twin) -- (from ANY) require -- from ANY other_not_void: other /= Void do copy (other.deep_twin) ensure -- from ANY deep_equal: deep_equal (Current, other) end frozen deep_twin: BOUNDED_QUEUE [G] -- New object structure recursively duplicated from Current. -- (from ANY) external "built_in" ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) end frozen standard_clone (other: detachable ANY): like other obsolete "Use `standard_twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) do if other /= Void then Result := other.standard_twin end ensure -- from ANY instance_free: class equal: standard_equal (Result, other) end frozen standard_copy (other: BOUNDED_QUEUE [G]) -- Copy every field of other onto corresponding field -- of current object. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: BOUNDED_QUEUE [G] -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) external "built_in" ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) end frozen twin: BOUNDED_QUEUE [G] -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) external "built_in" ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current end feature -- Basic operations frozen as_attached: attached BOUNDED_QUEUE [G] obsolete "Remove calls to this feature. [2017-05-31]" -- Attached version of Current. -- (Can be used during transitional period to convert -- non-void-safe classes to void-safe ones.) -- (from ANY) do Result := Current end frozen default: detachable BOUNDED_QUEUE [G] -- Default value of object's type -- (from ANY) do end frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) do ensure -- from ANY instance_free: class end default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) do end frozen do_nothing -- Execute a null action. -- (from ANY) do ensure -- from ANY instance_free: class end feature {ARRAYED_QUEUE} -- Implementation in_index: INTEGER_32 -- Position for next insertion -- (from ARRAYED_QUEUE) local c: like capacity do c := capacity if c > 0 then Result := (out_index - Lower + count) \\ c + Lower else Result := out_index end end feature -- Implementation grow (n: INTEGER_32) -- Ensure that capacity is at least i. -- (from ARRAYED_QUEUE) require -- from RESIZABLE resizable: resizable local old_count, new_capacity: like capacity nb: INTEGER_32 do new_capacity := area.capacity.max (n) if count = 0 or else in_index > out_index then area := area.aliased_resized_area (new_capacity) else old_count := area.count area := area.aliased_resized_area_with_default (newest_item, new_capacity) nb := old_count - out_index + 1; area.move_data (out_index - Lower, new_capacity - nb, nb) out_index := new_capacity - nb + 1 end ensure -- from RESIZABLE new_capacity: capacity >= n end feature {NONE} -- Implementation newest_item: G -- Most recently added item. -- (from ARRAYED_QUEUE) local l_pos: INTEGER_32 do l_pos := in_index - 1 if l_pos = 0 then Result := area.item (area.upper) else Result := area.item (l_pos - Lower) end end upper: INTEGER_32 -- Upper bound for accessing list items via indexes -- (from ARRAYED_QUEUE) do Result := area.count end feature -- Correction Mismatch_information: MISMATCH_INFORMATION -- Original attribute values of mismatched object -- (from MISMATCH_CORRECTOR) once create Result end feature -- Iteration new_cursor: ARRAYED_QUEUE_ITERATION_CURSOR [G] -- Fresh cursor associated with current structure -- (from ARRAYED_QUEUE) require -- from ITERABLE True do create Result.make (Current) ensure -- from ITERABLE result_attached: Result /= Void end feature -- Output Io: STD_FILES -- Handle to standard file setup -- (from ANY) once create Result; Result.set_output_default ensure -- from ANY instance_free: class io_not_void: Result /= Void end out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) do Result := tagged_out ensure -- from ANY out_not_void: Result /= Void end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) local s: READABLE_STRING_8 do if attached o then s := o.out if attached {READABLE_STRING_32} s as s32 then Io.put_string_32 (s32) elseif attached {READABLE_STRING_8} s as s8 then Io.put_string (s8) else Io.put_string_32 (s.as_string_32) end end ensure -- from ANY instance_free: class end frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) external "built_in" ensure -- from ANY tagged_out_not_void: Result /= Void end feature -- Platform Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) once create Result ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void end feature -- Retrieval correct_mismatch -- Attempt to correct object mismatch using Mismatch_information. require -- from MISMATCH_CORRECTOR True local i, nb: INTEGER_32 do if attached {ARRAY [G]} Mismatch_information.item ("fl") as a and then attached {INTEGER_32} Mismatch_information.item ("in_index") as l_in_index and then attached {INTEGER_32} Mismatch_information.item ("out_index") as l_out_index and then attached {BOOLEAN} Mismatch_information.item ("object_comparison") as c then out_index := 1 create area.make_empty (a.capacity) if l_out_index > l_in_index then from i := l_out_index nb := a.count until i >= nb loop extend (a.item (i)) i := i + 1 end from i := 0 until i >= l_in_index loop extend (a.item (i)) i := i + 1 end else from i := l_out_index until i >= l_in_index loop extend (a.item (i)) i := i + 1 end end object_comparison := c else Precursor end end feature {NONE} -- Retrieval frozen internal_correct_mismatch -- Called from runtime to perform a proper dynamic dispatch on correct_mismatch -- from MISMATCH_CORRECTOR. -- (from ANY) local l_msg: STRING_32 l_exc: EXCEPTIONS do if attached {MISMATCH_CORRECTOR} Current as l_corrector then l_corrector.correct_mismatch else create l_msg.make_from_string ("Mismatch: ".as_string_32) create l_exc; l_msg.append (generating_type.name_32); l_exc.raise_retrieval_exception (l_msg) end end invariant valid_count: count <= capacity -- from DISPENSER readable_definition: readable = not is_empty writable_definition: writable = not is_empty -- from ACTIVE writable_constraint: writable implies readable empty_constraint: is_empty implies (not readable) and (not writable) -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) -- from FINITE empty_definition: is_empty = (count = 0) -- from RESIZABLE increase_by_at_least_one: Minimal_increase >= 1 -- from BOUNDED valid_count: count <= capacity full_definition: full = (count = capacity) note copyright: "Copyright (c) 1984-2012, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software 5949 Hollister Ave., Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Website http://www.eiffel.com Customer support http://support.eiffel.com ]" end -- class BOUNDED_QUEUE
Generated by ISE EiffelStudio