Class DS_HASH_SET PreviousNext

note
description:

    "Sets implemented with single arrays. %
    %Keys are hashed using hash_code from HASHABLE."

library:    "Gobo Eiffel Structure Library"
author:     "Eric Bezault <ericb@gobosoft.com>"
copyright:  "Copyright (c) 2001, Eric Bezault and others"
license:    "MIT License"
class interface
DS_HASH_SET [G -> HASHABLE]
inherit
DS_ARRAYED_SPARSE_SET [G]
    DS_SPARSE_SET [G]
        DS_SET [G]
            DS_LINEAR [G]
                DS_TRAVERSABLE [G]
                    DS_CONTAINER [G]
                DS_SEARCHABLE [G]
                    DS_CONTAINER [G]
        DS_BILINEAR [G]
            DS_LINEAR [G]
                DS_TRAVERSABLE [G]
                    DS_CONTAINER [G]
                DS_SEARCHABLE [G]
                    DS_CONTAINER [G]
        DS_RESIZABLE [G]
            DS_CONTAINER [G]
create
make (n: INTEGER)
        -- Create an empty table and allocate
        -- memory space for at least n items.
        -- Use `=' as comparison criterion.
        -- (From DS_SPARSE_SET.)
    require
        positive_n: n >= 0
    ensure
        empty: is_empty
        capacity_set: capacity = n
        before: before
make_equal (n: INTEGER)
        -- Create an empty table and allocate
        -- memory space for at least n items.
        -- Use equal as comparison criterion.
        -- (From DS_SPARSE_SET.)
    require
        positive_n: n >= 0
    ensure
        empty: is_empty
        capacity_set: capacity = n
        before: before
make_default
        -- Create an empty set and allocate memory
        -- space for at least default_capacity items.
        -- Use `=' as comparison criterion.
        -- (From DS_CONTAINER.)
    ensure
        empty: is_empty
        capacity_set: capacity = default_capacity
        before: before
feature -- Access
infix "@", item (v: G): G
        -- Item equal to v held in set
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        has_v: has (v)
    ensure
        definition: same_items (Result, v)
found_item: G
        -- Item found by last call to search
        -- (From DS_SPARSE_SET.)
    require
        item_found: found
item_for_iteration: G
        -- Item at internal cursor position
        -- (From DS_TRAVERSABLE.)
    require
        not_off: not off
first: G
        -- First item in set
        -- (From DS_LINEAR.)
    require
        not_empty: not is_empty
    ensure
        has_first: has (Result)
last: G
        -- Last item in table
        -- (From DS_BILINEAR.)
    require
        not_empty: not is_empty
    ensure
        has_last: has_item (Result)
new_cursor: DS_HASH_SET_CURSOR [G]
        -- New external cursor for traversal
        -- (From DS_TRAVERSABLE.)
    ensure
        cursor_not_void: Result /= Void
        valid_cursor: valid_cursor (Result)
equality_tester: DS_EQUALITY_TESTER [G]
        -- Equality tester;
        -- A void equality tester means that `='
        -- will be used as comparison criterion.
        -- (From DS_SEARCHABLE.)
feature -- Measurement
count: INTEGER
        -- Number of items in set
        -- (From DS_CONTAINER.)
capacity: INTEGER
        -- Maximum number of items in set
        -- (From DS_RESIZABLE.)
default_capacity: INTEGER
        -- Initial capacity in make_default
        -- (Default value: 10)
        -- (From DS_RESIZABLE.)
    ensure
        default_capacity_positive: Result >= 0
occurrences (v: G): INTEGER
        -- Number of times v appears in set
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SEARCHABLE.)
    ensure
        positive: Result >= 0
        has: has (v) implies Result >= 1
        has_v: has (v) implies (Result = 1)
        not_has_v: not has (v) implies (Result = 0)
feature -- Status report
has (v: G): BOOLEAN
        -- Does container include v?
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SEARCHABLE.)
    ensure
        not_empty: Result implies not is_empty
found: BOOLEAN
        -- Did last call to search succeed?
        -- (From DS_SPARSE_SET.)
is_subset (other: DS_SET [G]): BOOLEAN
        -- Are all items of current set included in other?
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        other_not_void: other /= Void
        same_equality_tester: same_equality_tester (other)
is_superset (other: DS_SET [G]): BOOLEAN
        -- Does current set include all items of other?
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        other_not_void: other /= Void
        same_equality_tester: same_equality_tester (other)
    ensure
        definition Result = other.is_subset (Current)
is_disjoint (other: DS_SET [G]): BOOLEAN
        -- Are none of the items of current set included in other?
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        other_not_void: other /= Void
        same_equality_tester: same_equality_tester (other)
is_empty: BOOLEAN
        -- Is set empty?
        -- (From DS_CONTAINER.)
is_full: BOOLEAN
        -- Is set  full?
        -- (From DS_RESIZABLE.)
is_first: BOOLEAN
        -- Is internal cursor on first item?
        -- (From DS_LINEAR.)
    ensure
        not_empty: Result implies not is_empty
        not_off: Result implies not off
        definition: Result implies (item_for_iteration = first)
is_last: BOOLEAN
        -- Is internal cursor on last item?
        -- (From DS_BILINEAR.)
    ensure
        not_empty: Result implies not is_empty
        not_off: Result implies not off
        definition: Result implies (item_for_iteration = last)
after: BOOLEAN
        -- Is there no valid position to right of internal cursor?
        -- (From DS_LINEAR.)
before: BOOLEAN
        -- Is there no valid position to left of internal cursor?
        -- (From DS_BILINEAR.)
off: BOOLEAN
        -- Is there no item at internal cursor position?
        -- (From DS_TRAVERSABLE.)
same_position (a_cursor: like new_cursor): BOOLEAN
        -- Is internal cursor at same position as a_cursor?
        -- (From DS_TRAVERSABLE.)
    require
        a_cursor_not_void: a_cursor /= Void
valid_cursor (a_cursor: DS_CURSOR [G]): BOOLEAN
        -- Is a_cursor a valid cursor?
        -- (From DS_TRAVERSABLE.)
    require
        a_cursor_not_void: a_cursor /= Void
same_items (v, u: G): BOOLEAN
        -- Are v and u considered equal?
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SEARCHABLE.)
same_equality_tester (other: DS_SEARCHABLE [G]): BOOLEAN
        -- Does container use the same comparison
        -- criterion as other?
        -- (From DS_SEARCHABLE.)
    require
        other_not_void: other /= Void
equality_tester_settable (a_tester: like equality_tester): BOOLEAN
        -- Can set_equality_tester be called with a_tester
        -- as argument in current state of container?
        -- (Answer: the set has to be empty.)
        -- (From DS_SEARCHABLE.)
feature -- Search
search (v: G)
        -- Search for item equal to v.
        -- (Use equality_tester's comparison criterion
        -- if not Void, use `=' criterion otherwise.)
        -- If found, set found to true, and make that
        -- item available in found_item.
        -- (From DS_SPARSE_SET.)
    ensure
        found_set: found = has (v)
        found_item_set: found implies (found_item = item (v))
feature -- Comparison
is_equal (other: like Current): BOOLEAN
        -- Is set equal to other?
        -- Do not take cursor positions, and capacity into
        -- account, but equality_tester should be the same.
        -- (From GENERAL.)
    require
        other_not_void: other /= Void
    ensure
        consistent: standard_is_equal (other) implies Result
        same_type: Result implies same_type (other)
        symmetric: Result implies other.is_equal (Current)
        same_count: Result implies count = other.count
feature -- Duplication
copy (other: like Current)
        -- Copy other to current set.
        -- Move all cursors off (unless other = Current).
        -- (From GENERAL.)
    require
        other_not_void: other /= Void
        type_identity: same_type (other)
    ensure
        is_equal: is_equal (other)
feature -- Setting
set_equality_tester (a_tester: like equality_tester)
        -- Set equality_tester to a_tester.
        -- A void equality tester means that `='
        -- will be used as comparison criterion.
        -- (From DS_SEARCHABLE.)
    require
        equality_tester_settable: equality_tester_settable (a_tester)
    ensure
        equality_tester_set: equality_tester = a_tester
feature -- Cursor movement
start
        -- Move internal cursor to first position.
        -- (From DS_LINEAR.)
    ensure
        empty_behavior: is_empty implies after
        not_empty_behavior: not is_empty implies is_first
finish
        -- Move internal cursor to last position.
        -- (From DS_BILINEAR.)
    ensure
        empty_behavior: is_empty implies before
        not_empty_behavior: not is_empty implies is_last
forth
        -- Move internal cursor to next position.
        -- (From DS_LINEAR.)
    require
        not_after: not after
back
        -- Move internal cursor to previous position.
        -- (From DS_BILINEAR.)
    require
        not_before: not before
search_forth (v: G)
        -- Move internal cursor to first position at or after current
        -- position where item_for_iteration and v are equal.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Move after if not found.
        -- (From DS_LINEAR.)
    require
        not_off: not off or after
search_back (v: G)
        -- Move internal cursor to first position at or before current
        -- position where item_for_iteration and v are equal.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Move before if not found.
        -- (From DS_BILINEAR.)
    require
        not_off: not off or before
go_after
        -- Move cursor to after position.
        -- (From DS_LINEAR.)
    ensure
        after: after
go_before
        -- Move cursor to before position.
        -- (From DS_BILINEAR.)
    ensure
        before: before
go_to (a_cursor: like new_cursor)
        -- Move internal cursor to a_cursor's position.
        -- (From DS_TRAVERSABLE.)
    require
        cursor_not_void: a_cursor /= Void
        valid_cursor: valid_cursor (a_cursor)
    ensure
        same_position: same_position (a_cursor)
feature -- Element change
put (v: G)
        -- Add v to set, replacing any existing item.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Do not move cursors.
        -- (From DS_SPARSE_SET.)
    require
        not_full: not is_full
    ensure
        same_count: (old has (v)) implies (count = old count)
        one_more: (not old has (v)) implies (count = old count + 1)
        inserted: has (v) and then item (v) = v
put_new (v: G)
        -- Add v to set.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Do not move cursors.
        -- (From DS_SPARSE_SET.)
    require
        not_full: not is_full
        new_item: not has (v)
    ensure
        one_more: count = old count + 1
        inserted: has (v) and then item (v) = v
force (v: G)
        -- Add v to set, replacing any existing item.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Resize set if necessary.
        -- Do not move cursors.
        -- (From put in DS_SET.)
    ensure
        inserted: has (v) and then item (v) = v
        same_count: (old has (v)) implies (count = old count)
        one_more: (not old has (v)) implies (count = old count + 1)
force_new (v: G)
        -- Add v to set.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Resize set if necessary.
        -- Do not move cursors.
        -- (From DS_SPARSE_SET.)
    require
        new_item: not has (v)
    ensure
        one_more: count = old count + 1
        inserted: has (v) and then item (v) = v
force_last (v: G)
        -- Add v at the end of set if not already included,
        -- or replace it otherwise.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Resize set if necessary.
        -- Do not move cursors.
        -- (From DS_SPARSE_SET.)
    ensure
        same_count: (old has (v)) implies (count = old count)
        one_more: (not old has (v)) implies (count = old count + 1)
        inserted: has (v) and then item (v) = v
        last: (not old has (v)) implies last = v
feature -- Removal
remove (v: G)
        -- Remove item equal to v from set.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- Move any cursors at this position forth.
        -- (From DS_SET.)
    ensure
        same_count: (not old has (v)) implies (count = old count)
        one_less: (old has (v)) implies (count = old count - 1)
        removed: not has (v)
remove_found_item
        -- Remove item found by last call to search.
        -- Move any cursors at this position forth.
        -- (From DS_SPARSE_SET.)
    require
        item_found: found
    ensure
        one_less: count = old count - 1
wipe_out
        -- Remove all items from set.
        -- Move all cursors off.
        -- (From DS_CONTAINER.)
    ensure
        wiped_out: is_empty
feature -- Basic operations
append (other: DS_SET [G])
        -- Add all items of other to current set.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        other_not_void: other /= Void
        same_equality_tester: same_equality_tester (other)
    ensure
        is_superset: is_superset (other)
intersect (other: DS_SET [G])
        -- Remove all items not included in other.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        other_not_void: other /= Void
        same_equality_tester: same_equality_tester (other)
    ensure
        is_subset: is_subset (other)
subtract (other: DS_SET [G])
        -- Remove all items also included in other.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        other_not_void: other /= Void
        same_equality_tester: same_equality_tester (other)
    ensure
        is_disjoint: is_disjoint (other)
symdif (other: DS_SET [G])
        -- Add items of other which are not included
        -- in current set and remove those which are.
        -- (Use equality_tester's comparison criterion
        -- if not void, use `=' criterion otherwise.)
        -- (From DS_SET.)
    require
        other_not_void: other /= Void
        same_equality_tester: same_equality_tester (other)
feature -- Resizing
resize (n: INTEGER)
        -- Resize set so that it can contain
        -- at least n items. Do not lose any item.
        -- Move all cursors off.
        -- (From DS_RESIZABLE.)
    require
        n_large_enough: n >= capacity
    ensure
        capacity_set: capacity = n
invariant
positive_count: count >= 0
empty_definition: is_empty = (count = 0)
    -- (From DS_CONTAINER.)
empty_constraint: is_empty implies off
    -- (From DS_TRAVERSABLE.)
after_constraint: after implies off
    -- (From DS_LINEAR.)
not_both: not (after and before)
before_constraint: before implies off
    -- (From DS_BILINEAR.)
count_constraint: count <= capacity
full_definition: is_full = (count = capacity)
    -- (From DS_RESIZABLE.)
end -- class DS_HASH_SET

Copyright © 2001, Eric Bezault
mailto:
ericb@gobosoft.com
http:
//www.gobosoft.com
Last Updated: 6 April 2001

HomeTocPreviousNext