0

Which library of the kernel should I use to sort a collection in eiffel?

where can I find an example of sort? are the tipical bubble sort, etc available?

Any sorter with agent would be very useful too to avoid having to do it with a comparable which is not implemented for example with PATH class

Pipo
  • 4,653
  • 38
  • 47

1 Answers1

1

Library base_extension provides a deferred class SORTER with effective implementations of bubble, quick and shell sort algorithms.

It uses a comparator object to do the comparison on elements of an arbitrary type, not necessarily of type COMPARABLE. One of implementations of the comparator interface is AGENT_EQUALITY_TESTER to which you pass an agent to determine whether one object is less than another one.

Here is an example how paths can be sorted by their canonical name using bubble sort:

sort_paths (a_paths: INDEXABLE [PATH, INTEGER])
    local
        l_sorter: SORTER [PATH]
    do
        create {BUBBLE_SORTER [PATH]} l_sorter.make
            (create {AGENT_EQUALITY_TESTER [PATH]}.make (
                agent (a_p1, a_p2: PATH): BOOLEAN
                    do
                        Result := a_p1.canonical_path < a_p2.canonical_path
                    end))
        l_sorter.sort (a_paths)
    end

Now, if there is a list of paths paths of type ARRAYED_LIST [PATH], it can be sorted by calling sort_paths (paths).

Pipo
  • 4,653
  • 38
  • 47
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • It works, many thx, may I ask you how an `ARRAYED_LIST[PATH]` conforms to `INDEXABLE[PATH, INTEGER]` I missed this part – Pipo Nov 14 '19 at 09:46
  • @Pipo Here is the inheritance path: `ARRAYED_LIST` -> `DYNAMIC_LIST` -> `LIST` -> `CHAIN` -> `INDEXABLE`. `CHAIN` represents a sequence of items, therefore it's indexable. You can change the signature of `sort_paths` to accept `ARRAYED_LIST [PATH]` instead of `INDEXABLE`. I just used the most general type. – Alexander Kogtenkov Nov 14 '19 at 11:04
  • With the general signature you can also sort `ARRAY [PATH]` or `TWO_WAY_LIST [PATH]` (though in the latter case the efficiency would be problematic, as the sorters rely on the possibility to read and write elements at "random" positions, so it's better to pass structure with efficient access by an index). – Alexander Kogtenkov Nov 14 '19 at 11:15
  • thx, my question was more about PATH, INTEGER where is the second generic parameter given for conformance? I think I'd have to search into descendants of PATH... – Pipo Nov 14 '19 at 13:34
  • @Pipo `INTEGER` is the type of the index to access items. My guess is that it is there for historical reasons. – Alexander Kogtenkov Nov 14 '19 at 14:12
  • PATH is a heir of INDEXABLE[PATH, INTEGER], that was the answer of my conformity question – Pipo Nov 15 '19 at 15:20
  • @Pipo No, the essential fact here is that `ARRAYED_LIST [PATH]` conforms to `INDEXABLE [PATH, INTEGER]`. – Alexander Kogtenkov Nov 15 '19 at 15:23