ANY NONE
deferred class interface DICTIONARY[V,K]
   --
   -- Associative memory.
   -- Values of type V are stored using Keys of type K.
   --

feature(s) from DICTIONARY
   -- Counting:

   count: INTEGER
      -- Actual count of stored elements.

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

feature(s) from DICTIONARY
   -- Basic access:

   has (k: K): BOOLEAN
      -- Is there a value currently associated with key k?
      require
         k /= Void

   at (k: K): V
      -- Return the value associated to key k.
      -- (See also reference_at if V is a reference type.)
      require
         has(k)

   infix "@" (k: K): V
      -- Return the value associated to key k.
      -- (See also reference_at if V is a reference type.)
      require
         has(k)

   reference_at (k: K): V
      -- Return Void or the value associated with key k. Actually, this
      -- feature is useful when the type of values (the type V) is a
      -- reference type, to avoid using has just followed by at to get
      -- the corresponding value.
      require
         k /= Void;
         values_are_expanded: Result /= Void implies not Result.is_expanded_type
      ensure
         has(k) implies Result = at(k)

   fast_has (k: K): BOOLEAN
      -- Is there a value currently associated with key k?
      require
         k /= Void

   fast_at (k: K): V
      -- Return the value associated to key k.
      -- (See also reference_at if V is a reference type.)
      require
         fast_has(k)

   fast_reference_at (k: K): V
      -- Return Void or the value associated with key k. Actually, this
      -- feature is useful when the type of values (the type V) is a
      -- reference type, to avoid using has just followed by at to get
      -- the corresponding value.
      require
         k /= Void;
         values_are_expanded: Result /= Void implies not Result.is_expanded_type
      ensure
         fast_has(k) implies Result = fast_at(k)

feature(s) from DICTIONARY
   put (v: V; k: K)
      -- Change some existing entry or add the new one. If there is
      -- as yet no key k in the dictionary, enter it with item v.
      -- Otherwise overwrite the item associated with key k.
      require
         k /= Void
      ensure
         v = at(k)

   add (v: V; k: K)
      -- To add a new entry k with its associated value v. Actually,
      -- this is equivalent to call put, but may run a little bit
      -- faster.
      require
         can_add(v,k)
      ensure
         count = 1 + old count;
         v = at(k)

   can_add (v: V; k: K): BOOLEAN

feature(s) from DICTIONARY
   -- Looking and searching some value:

   occurrences (v: V): INTEGER
      -- Number of occurrences using equal.
      -- See also fast_occurrences to choose the appropriate one.
      ensure
         Result >= 0

   fast_occurrences (v: V): INTEGER
      -- Number of occurrences using =.
      -- See also occurrences to choose the appropriate one.
      ensure
         Result >= 0

   key_at (v: V): K
      -- Retrieve the key used for value v using equal for comparison.
      require
         occurrences(v) = 1
      ensure
         (create {SAFE_EQUAL[V]}default_create).test(at(Result),v)

   fast_key_at (v: V): K
      -- Retrieve the key used for value v using = for comparison.
      require
         fast_occurrences(v) = 1
      ensure
         at(Result) = v

feature(s) from DICTIONARY
   -- Removing:

   remove (k: K)
      -- Remove entry k (which may exist or not before this call).
      require
         k /= Void
      ensure
         not has(k)

   clear_count
      -- Discard all items (is_empty is True after that call). The internal capacity is not changed
      -- by this call. See also clear_count_and_capacity to select the most appropriate.
      ensure
         is_empty: count = 0;
         capacity = old capacity

   clear_count_and_capacity
      -- Discard all items (is_empty is True after that call). The internal capacity may also be
      -- reduced after this call. See also clear_count to select the most appropriate.
      ensure
         is_empty: count = 0;
         capacity <= old capacity

   capacity: INTEGER
      -- Approximation of the actual internal storage capacity. The capacity will grow automatically
      -- when needed (i.e. capacity is not a limit for the number of values stored). Also note that
      -- the capacity value may not be always accurate depending of the implementation (anyway, this
      -- capacity value is at least equals to count).

feature(s) from DICTIONARY
   -- 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): V
      require
         valid_index(index)
      ensure
         Result = at(key(index))

   key (index: INTEGER): K
      require
         valid_index(index)
      ensure
         at(Result) = item(index)

   get_new_iterator_on_items: ITERATOR[V]
      ensure
         Result /= Void

   get_new_iterator_on_keys: ITERATOR[K]
      ensure
         Result /= Void

   key_map_in (buffer: COLLECTION[K])
      -- Append in buffer, all available keys (this may be useful to
      -- speed up the traversal).
      require
         buffer /= Void
      ensure
         buffer.count = count + old buffer.count

   item_map_in (buffer: COLLECTION[V])
      -- Append in buffer, all available items (this may be useful to
      -- speed up the traversal).
      require
         buffer /= Void
      ensure
         buffer.count = count + old buffer.count

feature(s) from DICTIONARY
   is_equal (other: like Current): BOOLEAN
      -- Do both dictionaries have the same set of associations?
      -- Keys are compared with is_equal and values are comnpared
      -- with the basic = operator. See also is_equal_map.
      require
         other /= Void
      ensure
         Result implies count = other.count;
         generating_type = other.generating_type implies Result = other.is_equal(Current)

   is_equal_map (other: like Current): BOOLEAN
      -- Do both dictionaries have the same set of associations?
      -- Both keys and values are compared with is_equal. See also is_equal.

   copy (other: like Current)
      -- Reinitialize by copying all associations of other.
      require
         same_dynamic_type(other)
      ensure
         is_equal(other)

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

   do_all (action: ROUTINE[ANY,TUPLE[ANY, ANY]])
      -- Apply action to every [V,K] associations of Current.

   for_all (test: FUNCTION[ANY,TUPLE[ANY, ANY],BOOLEAN]): BOOLEAN
      -- Do all [V,K] associations satisfy test?

   exists (test: FUNCTION[ANY,TUPLE[ANY, ANY],BOOLEAN]): BOOLEAN
      -- Does at least one [V,K] association satisfy test?

feature(s) from DICTIONARY
   -- Other features:

   internal_key (k: K): K
      -- Retrieve the internal key object which correspond to the existing
      -- entry k (the one memorized into the Current dictionary).
      require
         has(k)
      ensure
         Result.is_equal(k)


invariant

    capacity >= count;

end of deferred DICTIONARY[V,K]