<commit_before>doc "Represents a collection which maps keys to items,
     where a key can map to at most one item. Each such 
     mapping may be represented by an `Entry`.
     
     A `Map` is a `Collection` of its `Entry`s, and a 
     `Correspondence` from keys to items."
see (forKey, forItem, byItem, byKey)
shared interface Map<out Key,out Item>
        satisfies Collection<Key->Item> &
                  Correspondence<Object,Item> &
                  Cloneable<Map<Key,Item>>
        given Key satisfies Object
        given Item satisfies Object {

    doc "Two `Map`s are considered equal iff they have the 
         same _entry sets_. The entry set of a `Map` is the
         set of `Entry`s belonging to the map. Therefore, the
         maps are equal iff they have same set of `keys`, and 
         for every key in the key set, the maps have equal
         items."
    shared actual default Boolean equals(Object that) {
        if (is Map<Object,Object> that) {
            if (that.size==size) {
                for (entry in this) {
                    if (exists item = that[entry.key]) {
                        if (item==entry.item) {
                            continue;
                        }
                    }
                    return false;
                }
                else {
                    return true;
                }
            }
        }
        return false;
    }

    shared actual default Integer hash {
        variable Integer hashCode := 1;
        for(Entry<Key,Item> elem in this){
            hashCode *= 31;
            hashCode += elem.hash;
        }
        return hashCode;
    }

    doc "Returns the set of keys contained in this `Map`."
    actual shared default Set<Key> keys {
        object keySet satisfies Set<Key> {
            shared actual Set<Key> clone {
                return this;
            }
            shared actual Boolean equals(Object that) {
                return false;
            }
            shared actual Integer hash {
                return outer.size;
            }
            shared actual Iterator<Key> iterator {
                return bottom;
            }
            shared actual Integer size {
                return outer.size;
            }
            shared actual String string {
                return "";
            }
            shared actual Set<Key> complement<Other>(Set<Other> set)
                    given Other satisfies Object {
                return bottom;
            }
            shared actual Set<Key|Other> exclusiveUnion<Other>(Set<Other> set)
                    given Other satisfies Object {
                return bottom;
            }
            shared actual Set<Key&Other> intersection<Other>(Set<Other> set)
                    given Other satisfies Object {
                return bottom;
            }
            shared actual Set<Key|Other> union<Other>(Set<Other> set)
                    given Other satisfies Object {
                return bottom;
            }
        }
        return keySet;
    }

    doc "Returns all the values stored in this `Map`. An 
         element can be stored under more than one key in 
         the map, and so it can be contained more than once 
         in the resulting collection."
    shared default Collection<Item> values {
        object valueCollection satisfies Collection<Item> {
            shared actual Collection<Item> clone {
                return this;
            }
            shared actual Boolean equals(Object that) {
                return false;
            }
            shared actual Integer hash {
                return outer.size;
            }
            shared actual Iterator<Item> iterator {
                return bottom;
            }
            shared actual Integer size {
                return outer.size;
            }
            shared actual String string {
                return "";
            }

        }
        return valueCollection;
    }

    doc "Returns a `Map` in which every key is an `Item` in 
         this map, and every value is the set of keys that 
         stored the `Item` in this map."
    shared default Map<Item, Set<Key>> inverse {
        object inverse satisfies Map<Item, Set<Key>> {
            shared actual Map<Item,Set<Key>> clone {
                return this;
            }
            shared actual Boolean equals(Object that) {
                return false;
            }
            shared actual Integer hash {
                return outer.size;
            }
            shared actual Set<Key>? item(Object key) {
                return bottom;
            }
            shared actual Iterator<Entry<Item,Set<Key>>> iterator {
                return bottom;
            }
            shared actual Integer size {
                return outer.size;
            }
            shared actual String string {
                return "";
            }
        }
        return inverse;
    }

    shared default Map<Key,Result> mapItems<Result>(Result mapping(Key key, Item item)) 
            given Result satisfies Object {
        object mapped extends Object() 
                satisfies Map<Key, Result> {
            shared actual Map<Key, Result> clone { return this; } //TODO: should take a copy
            shared actual Result? item(Object key) {
                if (is Key key) {
                    if (exists item=outer[key]) {
                        return mapping(key, item);
                    }
                }
                return null;
            }
            shared actual Iterator<Key->Result> iterator {
                return elements { for (key->item in outer) key->mapping(key,item) }.iterator;
            }
            shared actual Integer size { return outer.size; }
            shared actual String string { return "mapItems"; } //TODO
        }
        return mapped;
    }
}

doc "Creates a map containing the specified entries."
shared Map<Key, Item> map<Key, Item>(Entry<Key,Item>... entries)
        given Key satisfies Object
        given Item satisfies Object {
    throw;
}<commit_msg>work on Ceylon declaration of Map<commit_after>import ceylon.language { createMap=map }

doc "Represents a collection which maps _keys_ to _items_,
     where a key can map to at most one item. Each such 
     mapping may be represented by an `Entry`.
     
     A `Map` is a `Collection` of its `Entry`s, and a 
     `Correspondence` from keys to items.

     The prescence of an entry in a map may be tested
     using the `in` operator:
     
         if (\"lang\"->\"en_AU\" in settings) { ... }
     
     The entries of the map may be iterated using `for`:
     
         for (key->item in settings) { ... }
     
     The item for a key may be obtained using the item
     operator:
     
         String lang = settings[\"lang\"] else \"en_US\";"
see (Entry, forKey, forItem, byItem, byKey)
shared interface Map<out Key,out Item>
        satisfies Collection<Key->Item> &
                  Correspondence<Object,Item> &
                  Cloneable<Map<Key,Item>>
        given Key satisfies Object
        given Item satisfies Object {

    doc "Two `Map`s are considered equal iff they have the 
         same _entry sets_. The entry set of a `Map` is the
         set of `Entry`s belonging to the map. Therefore, the
         maps are equal iff they have same set of `keys`, and 
         for every key in the key set, the maps have equal
         items."
    shared actual default Boolean equals(Object that) {
        if (is Map<Object,Object> that) {
            if (that.size==size) {
                for (entry in this) {
                    if (exists item = that[entry.key]) {
                        if (item==entry.item) {
                            continue;
                        }
                    }
                    return false;
                }
                else {
                    return true;
                }
            }
        }
        return false;
    }

    shared actual default Integer hash {
        variable Integer hashCode := 1;
        for(Entry<Key,Item> elem in this){
            hashCode *= 31;
            hashCode += elem.hash;
        }
        return hashCode;
    }

    doc "Returns the set of keys contained in this `Map`."
    actual shared default Set<Key> keys {
        object keySet 
                extends Object() 
                satisfies Set<Key> {
            shared actual Set<Key> clone {
                //TODO: take a shallow copy
                return this;
            }
            shared actual Iterator<Key> iterator {
                return outer.map(forKey((Key k) k)).iterator;
            }
            shared actual Integer size {
                return outer.size;
            }
            shared actual Set<Key> complement<Other>(Set<Other> set)
                    given Other satisfies Object {
                //TODO
                return bottom;
            }
            shared actual Set<Key|Other> exclusiveUnion<Other>(Set<Other> set)
                    given Other satisfies Object {
                //TODO
                return bottom;
            }
            shared actual Set<Key&Other> intersection<Other>(Set<Other> set)
                    given Other satisfies Object {
                //TODO
                return bottom;
            }
            shared actual Set<Key|Other> union<Other>(Set<Other> set)
                    given Other satisfies Object {
                //TODO
                return bottom;
            }
        }
        return keySet;
    }

    doc "Returns all the values stored in this `Map`. An 
         element can be stored under more than one key in 
         the map, and so it can be contained more than once 
         in the resulting collection."
    shared default Collection<Item> values {
        object valueCollection 
                extends Object() 
                satisfies Collection<Item> {
            shared actual Collection<Item> clone {
                //TODO: take a shallow copy
                return this;
            }
            shared actual Boolean equals(Object that) {
                //TODO: what is a reasonable
                //      definition of equality
                //      for a bag?
                return false;
            }
            shared actual Integer hash {
                return outer.size;
            }
            shared actual Iterator<Item> iterator {
                return outer.map((Entry<Key,Item> e) e.item).iterator;
            }
            shared actual Integer size {
                return outer.size;
            }
        }
        return valueCollection;
    }

    doc "Returns a `Map` in which every key is an `Item` in 
         this map, and every value is the set of keys that 
         stored the `Item` in this map."
    shared default Map<Item, Set<Key>> inverse {
        object inverse 
                extends Object() 
                satisfies Map<Item, Set<Key>> {
            shared actual Map<Item,Set<Key>> clone {
                return createMap(this...);
            }
            shared actual Set<Key>? item(Object key) {
                //TODO
                return bottom;
            }
            shared actual Iterator<Entry<Item,Set<Key>>> iterator {
                //TODO
                return bottom;
            }
            shared actual Integer size {
                return outer.size;
            }
        }
        return inverse;
    }

    doc "Returns a `Map` with the same keys as this map. For
         every key, the item is the result of applying the
         given transformation function."
    shared default Map<Key,Result> mapItems<Result>(
            doc "The function that transforms a key/item
                 pair, producing the item of the resulting
                 map."
            Result mapping(Key key, Item item)) 
            given Result satisfies Object {
        object mapped extends Object() 
                satisfies Map<Key, Result> {
            shared actual Map<Key, Result> clone {
                //TODO: should take a copy 
                return this;
            }
            shared actual Result? item(Object key) {
                if (is Key key) {
                    if (exists item=outer[key]) {
                        return mapping(key, item);
                    }
                }
                return null;
            }
            shared actual Iterator<Key->Result> iterator {
                return outer.map((Key->Item e) 
                        e.key->mapping(e.key,e.item))
                                .iterator;
            }
            shared actual Integer size { 
                return outer.size; 
            }
        }
        return mapped;
    }
}

doc "Creates a new map with the given entries."
shared Map<Key, Item> map<Key, Item>(Key->Item... entries)
        given Key satisfies Object
        given Item satisfies Object {
    throw;
}
{'fraction_non_alphanumeric': 0.07154946914909986, 'fraction_numerical': 0.0004616094783812894, 'mean_word_length': 1.303970223325062, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1185392', 'n_tokens_mistral': 3134, 'n_tokens_neox': 2984, 'n_words': 1149}