Introducing the map-list structure

by Finnian Reilly (modified: 2018 May 22)


Eiffel-Loop contains a useful class EL_ARRAYED_MAP_LIST defined as an arrayed list of key-value pair tuples. It can be initialized from a hash table and converted to a hash table or a list of strings.

class EL_ARRAYED_MAP_LIST [K -> HASHABLE, G] inherit EL_ARRAYED_LIST [TUPLE [key: K; value: G]] rename extend as map_extend end


The descendants of class EL_ARRAYED_MAP_LIST offer a convenient way to sort a list of things from a derived sort order key.

EL_ARRAYED_MAP_LIST (source links)


Sort-by-key Example

Here is an example from class EL_UNIQUE_MACHINE_ID of sorting by a derived key. mac_address: ARRAY [NATURAL_8] local ordered_list: EL_KEY_SORTABLE_ARRAYED_MAP_LIST [INTEGER, EL_IP_ADAPTER] do create ordered_list.make_sorted (new_adapter_list, agent order_key, True) if ordered_list.is_empty then create Result.make_filled (0, 1, 6) else Result := ordered_list.first.value.address end end order_key (adapter: EL_IP_ADAPTER): INTEGER local order_list: like Selection_order do order_list := Selection_order order_list.start (adapter.type) Result := order_list.index end

Sort-by-value Example

ordered_by_size (a_file_paths: ARRAY [EL_FILE_PATH]): ARRAY [EL_FILE_PATH] -- Files to import local ordered_list: EL_VALUE_SORTABLE_ARRAYED_MAP_LIST [EL_FILE_PATH, INTEGER] do create ordered_list.make_sorted (a_file_paths, agent File_system.file_byte_count, True) Result := ordered_list.key_list.to_array end