ANY NONE SET
class interface HASHED_SET[E->HASHABLE]
   --
   -- Definition of a mathematical set of hashable objects. All common
   -- operations on mathematical sets are available.
   --

creation
   make
      -- Create an empty set. Internal storage capacity of the set is
      -- initialized using the Default_size value. Then, tuning of needed
      -- storage size is done automatically according to usage. If you
      -- are really sure that your set is always really bigger than
      -- Default_size, you may use with_capacity to save some execution
      -- time.
      ensure
         capacity = Default_size;
         is_empty

   with_capacity (medium_size: INTEGER)
      -- Create an empty set using medium_size as an appropriate value
      -- to help initialization of capacity. Thus, this feature may be
      -- used in place of make to save some execution time if one is
      -- sure that storage size will rapidly become really bigger than
      -- Default_size (if not sure, simply use make). Anyway, the
      -- initial medium_size value is just an indication and never a
      -- limit for the possible capacity. Keep in mind that the
      -- capacity tuning is done automatically according to usage.
      require
         medium_size > 0
      ensure
         is_empty;
         capacity >= medium_size

   from_collection (model: COLLECTION[E])
      -- slightly optimized version

feature(s) from SET
   -- Counting:

   count: INTEGER
      -- Cardinality of the set (i.e. actual count of stored elements).

   is_empty: BOOLEAN
      -- Is the set empty?
      ensure
         Result = (count = 0)

feature(s) from SET
   -- Adding and removing:

   add (e: E)
      -- Add a new item to the set: the mathematical definition of
      -- adding in a set is followed.
      require
         e /= Void
      ensure
         added: has(e);
         not_in_then_added: not old has(e) implies count = old count + 1;
         in_then_not_added: old has(e) implies count = old count

   remove (e: E)
      -- Remove item e from the set: the mathematical definition of
      -- removing from a set is followed.
      require
         e /= Void
      ensure
         removed: not has(e);
         not_in_not_removed: not old has(e) implies count = old count;
         in_then_removed: old has(e) implies count = old count - 1

   clear_count
      -- Empty the current set (is_empty is True after that call). If possible, the actual implementation 
      -- is supposed to keep its internal storage area in order to refill Current in an efficient way.
      -- See also clear_count_and_capacity to select the most appropriate.
      ensure
         is_empty

   clear_count_and_capacity
      -- Empty the current set (is_empty is True after that call). If possible, the actual implementation 
      -- is supposed to release its internal storage area for this memory to be used by other objects.
      -- See also clear_count to select the most appropriate.
      ensure
         is_empty: count = 0

feature(s) from SET
   -- Looking and searching:

   has (e: E): BOOLEAN
      -- Is element e in the set?
      require
         e /= Void
      ensure
         Result implies not is_empty

   fast_has (e: E): BOOLEAN
      -- Is element e in the set?
      require
         e /= Void
      ensure
         Result implies not is_empty

   reference_at (e: E): E
      -- When Is element e in the set?
      require
         e /= Void;
         not e.is_expanded_type
      ensure
         has(e) implies Result /= Void

feature(s) from SET
   -- To provide iterating facilities:

   lower: INTEGER

   upper: INTEGER
      ensure
         Result = count

   valid_index (index: INTEGER): BOOLEAN
      ensure
         Result = index.in_range(lower,upper)

   item (index: INTEGER): E
      -- Return the item indexed by index.
      require
         valid_index(index)
      ensure
         has(Result)

   get_new_iterator: ITERATOR[E]

feature(s) from SET
   -- Mathematical operations:

   union (other: like Current)
      -- Make the union of the Current set with other.
      require
         other /= Void
      ensure
         count <= old count + other.count

   infix "+" (other: like Current): like Current
      -- Return the union of the Current set with other.
      require
         other /= Void
      ensure
         Result.count <= count + other.count;
         Current.is_subset_of(Result) and then other.is_subset_of(Result)

   intersection (other: like Current)
      -- Make the intersection of the Current set with other.
      -- (optimized implementation)
      require
         other /= Void
      ensure
         count <= other.count.min(old count)

   infix "^" (other: like Current): like Current
      -- Return the intersection of the Current set with other.
      require
         other /= Void
      ensure
         Result.count <= other.count.min(count);
         Result.is_subset_of(Current) and then Result.is_subset_of(other)

   minus (other: like Current)
      -- Make the set Current - other.
      require
         other /= Void
      ensure
         count <= old count

   infix "-" (other: like Current): like Current
      -- Return  the set Current - other.
      require
         other /= Void
      ensure
         Result.count <= count;
         Result.is_subset_of(Current)

feature(s) from SET
   -- Comparison:

   is_subset_of (other: like Current): BOOLEAN
      -- Is the Current set a subset of other?
      require
         other /= Void
      ensure
         Result implies count <= other.count

   is_disjoint_from (other: like Current): BOOLEAN
      -- Is the Current set disjoint from other ?
      require
         other /= Void
      ensure
         Result = (Current ^ other).is_empty

   is_equal (other: like Current): BOOLEAN
      -- Is the Current set equal to other?
      require
         other /= Void
      ensure
         double_inclusion: Result = (is_subset_of(other) and other.is_subset_of(Current));
         generating_type = other.generating_type implies Result = other.is_equal(Current)

feature(s) from SET
   copy (other: like Current)
      -- Copy 'other' into the current set (slightly optimized version)
      require
         same_dynamic_type(other)
      ensure
         is_equal(other)

   from_collection (model: COLLECTION[E])
      -- slightly optimized version

feature(s) from SET
   -- Agents based features:

   do_all (action: ROUTINE[ANY,TUPLE[HASHABLE]])
      -- Apply action to every item of Current.

   for_all (test: FUNCTION[ANY,TUPLE[HASHABLE],BOOLEAN]): BOOLEAN
      -- Do all items satisfy test?

   exists (test: FUNCTION[ANY,TUPLE[HASHABLE],BOOLEAN]): BOOLEAN
      -- Does at least one item satisfy test?

feature(s) from SET
   make
      -- Create an empty set. Internal storage capacity of the set is
      -- initialized using the Default_size value. Then, tuning of needed
      -- storage size is done automatically according to usage. If you
      -- are really sure that your set is always really bigger than
      -- Default_size, you may use with_capacity to save some execution
      -- time.
      ensure
         capacity = Default_size;
         is_empty

feature(s) from HASHED_SET
   Default_size: INTEGER
      -- Minimum size for storage in number of items.

feature(s) from HASHED_SET
   buckets: NATIVE_ARRAY[SET_NODE[E]]
      -- The buckets storage area is the primary hash table of capacity
      -- elements. To search some element, the first access is done in
      -- buckets using the remainder of the division of the key hash_code
      -- by capacity. In order to try to avoid clashes, capacity is
      -- always a prime number (selected using HASH_TABLE_SIZE).

feature(s) from HASHED_SET
   -- Internal cache handling:

   cache_user: INTEGER
      -- The last user's external index in range [1 .. count] (see item
      -- and valid_index for example) may be saved in cache_user otherwise
      -- -1 to indicate that the cache is not active. When the cache is
      -- active, the corresponding index in buckets is save in
      -- cache_buckets and the corresponding node in cache_node.

   cache_node: SET_NODE[E]
      -- Meaningful only when cache_user is not -1.

   cache_buckets: INTEGER
      -- Meaningful only when cache_user is not -1.

feature(s) from HASHED_SET
   with_capacity (medium_size: INTEGER)
      -- Create an empty set using medium_size as an appropriate value
      -- to help initialization of capacity. Thus, this feature may be
      -- used in place of make to save some execution time if one is
      -- sure that storage size will rapidly become really bigger than
      -- Default_size (if not sure, simply use make). Anyway, the
      -- initial medium_size value is just an indication and never a
      -- limit for the possible capacity. Keep in mind that the
      -- capacity tuning is done automatically according to usage.
      require
         medium_size > 0
      ensure
         is_empty;
         capacity >= medium_size

feature(s) from HASHED_SET
   -- Counting:

   capacity: INTEGER
      -- Of the buckets storage area.

feature(s) from HASHED_SET
   increase_capacity
      -- There is no more free slots: the set must grow.

   set_cache_user (index: INTEGER)


invariant

    capacity > 0;

    capacity >= count;

    cache_user.in_range(-1,count);

    cache_user > 0 implies cache_node /= Void;

    cache_user > 0 implies cache_buckets.in_range(0,capacity - 1);

end of HASHED_SET[E->HASHABLE]