note description: "Priority queues implemented as heaps" library: "Free implementation of ELKS library" legal: "See notice at end of class." status: "See notice at end of class." names: sorted_priority_queue, dispenser, heap representation: heap access: fixed, membership contents: generic date: "$Date: 2018-12-15 18:06:16 +0000 (Sat, 15 Dec 2018) $" revision: "$Revision: 102608 $" class HEAP_PRIORITY_QUEUE [G -> COMPARABLE] create make, make_from_iterable feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end make (n: INTEGER_32) -- Allocate heap space. do create area.make_empty (n) end make_from_iterable (other: ITERABLE [G]) -- Create a priority queue with all items obtained from other. do make (estimated_count_of (other)) across other as o loop extend (o.item) end end feature -- Access generating_type: TYPE [detachable HEAP_PRIORITY_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: G): BOOLEAN -- Does structure include v? -- (Reference or object equality, -- based on object_comparison.) local i, nb: INTEGER_32 l_area: like area do l_area := area nb := l_area.count if object_comparison and v /= Void then from until i = nb or Result loop Result := l_area.item (i) ~ v i := i + 1 end else from until i = nb or Result loop Result := l_area.item (i) = v i := i + 1 end end ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty end item: G -- Entry at top of heap. require -- from ACTIVE readable: readable do Result := i_th (1) end feature {HEAP_PRIORITY_QUEUE, HEAP_PRIORITY_QUEUE_ITERATION_CURSOR} -- Access area: SPECIAL [G] -- Storage for queue. Lower: INTEGER_32 = 1 -- Lower bound for internal access to area. 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 do Result := area.capacity ensure -- from BOUNDED capacity_non_negative: Result >= 0 end count: INTEGER_32 -- Number of items do Result := area.count ensure -- from FINITE count_non_negative: Result >= 0 end Growth_percentage: INTEGER_32 = 50 -- Percentage by which structure will grow automatically -- (from RESIZABLE) index_set: INTEGER_INTERVAL -- Range of acceptable indexes do create Result.make (1, count) ensure then 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.) local i, nb: INTEGER_32 do if object_comparison then from i := Lower nb := count until i > nb loop if i_th (i) ~ v then Result := Result + 1 end i := i + 1 end else from i := Lower nb := count until i > nb loop if i_th (i) = v then Result := Result + 1 end i := i + 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: HEAP_PRIORITY_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: like Current): BOOLEAN -- Is other attached to an object considered -- equal to current object? require -- from ANY other_not_void: other /= Void local l_current, l_other: like Current do if other = Current then Result := True elseif object_comparison = other.object_comparison and then count = other.count then l_current := twin l_other := other.twin from Result := True until not Result or l_current.is_empty loop if object_comparison then Result := l_current.item ~ l_other.item else Result := l_current.item = l_other.item end; l_current.remove; l_other.remove 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: HEAP_PRIORITY_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 {NONE} -- Comparison safe_less_than (a, b: G): BOOLEAN -- Same as a < b when a and b are not Void. -- If a is Void and b is not, then True -- Otherwise False do if a /= Void and b /= Void then Result := a < b elseif a = Void and b /= Void then Result := True else Result := False end ensure asymmetric: Result implies not safe_less_than (b, a) 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? 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 structure empty? -- (from FINITE) 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) Prunable: BOOLEAN = True -- May items be removed? (Answer: yes.) 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? do Result := False end resizable: BOOLEAN -- May capacity be changed? (Answer: yes.) -- (from RESIZABLE) 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: like item) -- Add item v. require -- from COLLECTION extendible: extendible do put (v) 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: like item) -- Add item v at its proper position. require -- from DISPENSER extendible: extendible do if full then grow (count + additional_space) end put (v) ensure -- from DISPENSER item_inserted: is_inserted (v) end put (v: like item) -- Insert item v at its proper position. require -- from COLLECTION extendible: extendible local i: INTEGER_32 do from i := count + 1 until i <= 1 or else not safe_less_than (i_th (i // 2), v) loop force_i_th (i_th (i // 2), i) i := i // 2 end force_i_th (v, i) ensure -- from COLLECTION item_inserted: is_inserted (v) end feature -- Removal prune (v: G) -- Remove first occurrence of v if any. require -- from COLLECTION prunable: Prunable local i, nb: INTEGER_32 l_tmp: ARRAYED_LIST [G] l_item: G l_done: BOOLEAN do create l_tmp.make (count) if object_comparison then from i := 1 nb := count until i > nb loop l_item := i_th (i) if not l_done and l_item ~ v then l_done := True else l_tmp.extend (l_item) end i := i + 1 end else from i := 1 nb := count until i > nb loop l_item := i_th (i) if not l_done and l_item = v then l_done := True else l_tmp.extend (l_item) end i := i + 1 end end if l_tmp.count = count - 1 then across l_tmp as x from wipe_out loop put (x.item) end end end prune_all (v: G) -- Remove all occurrences of v. -- (Reference or object equality, -- based on object_comparison.) -- (from COLLECTION) require -- from COLLECTION prunable: Prunable do from until not has (v) loop prune (v) end ensure -- from COLLECTION no_more_occurrences: not has (v) end remove -- Remove item of highest value. require -- from ACTIVE prunable: Prunable writable: writable local i, j: INTEGER_32 up: like item nb: INTEGER_32 stop: BOOLEAN do nb := count - 1 if nb > 0 then from i := 1 up := i_th (nb + 1) until stop or i > nb // 2 loop j := 2 * i if (j < nb) and safe_less_than (i_th (j), i_th (j + 1)) then j := j + 1 end stop := not safe_less_than (up, i_th (j)) if not stop then put_i_th (i_th (j), i) i := j end end put_i_th (up, i) end; area.remove_tail (1) end wipe_out -- Remove all items. require -- from COLLECTION prunable: Prunable do area.wipe_out 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 grow (i: INTEGER_32) -- Ensure that capacity is at least i. require -- from RESIZABLE resizable: resizable do if i > area.capacity then area := area.aliased_resized_area (i) end ensure -- from RESIZABLE new_capacity: capacity >= i end trim -- Decrease capacity to the minimum value. -- Apply to reduce allocated storage. require -- from RESIZABLE True local n: like count do n := count if n < capacity then area := area.aliased_resized_area (n) end ensure -- from RESIZABLE same_count: count = old count minimal_capacity: capacity = count ensure then same_items: linear_representation.is_equal (old linear_representation) end feature -- Conversion linear_representation: ARRAYED_LIST [G] -- Representation as a linear structure -- (Sorted according to decreasing priority) local l_current: like Current do from l_current := twin create Result.make (count) until l_current.is_empty loop Result.extend (l_current.item); l_current.remove 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: like Current) -- Update current object using fields of object attached -- to other, so as to yield equal objects. 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: HEAP_PRIORITY_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: HEAP_PRIORITY_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 duplicate (n: INTEGER_32): like Current obsolete "[ Create a new container explicitly using `make_from_iterable` if available. Otherwise, replace a call to the feature with code that creates and initializes container. [2018-11-30] ]" -- New priority queue containing n greatest items of Current. require n_positive: n >= 0 n_in_bounds: n <= count local l_current: like Current l_tmp: ARRAYED_LIST [G] i: INTEGER_32 do from l_current := twin create l_tmp.make (n) i := 1 until i > n loop l_tmp.extend (l_current.item); l_current.remove i := i + 1 end across l_tmp as x from create Result.make (n) loop Result.put (x.item) end 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: HEAP_PRIORITY_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: HEAP_PRIORITY_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: HEAP_PRIORITY_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 HEAP_PRIORITY_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 HEAP_PRIORITY_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 -- Inapplicable replace (v: like item) -- Replace current item by v. require -- from ACTIVE writable: writable replaceable: replaceable do ensure -- from ACTIVE item_replaced: item = v end feature {HEAP_PRIORITY_QUEUE} -- Implementation force_i_th (v: G; i: INTEGER_32) require valid_index: i >= Lower and i <= count + Lower valid_upper: i = count + Lower implies count < capacity do area.force (v, i - Lower) end i_th (i: INTEGER_32): G require valid_index: area.valid_index (i - Lower) do Result := area.item (i - Lower) end put_i_th (v: G; i: INTEGER_32) require valid_index: area.valid_index (i - Lower) do area.put (v, i - Lower) end feature -- Iteration new_cursor: HEAP_PRIORITY_QUEUE_ITERATION_CURSOR [G] -- Fresh cursor associated with current structure 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 {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 -- 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-2018, 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 HEAP_PRIORITY_QUEUE
Generated by ISE EiffelStudio