Re: Sorted iterator for mappings

Chris Angelico <[email protected]>
Newsgroups gmane.comp.lang.pike.user
Message-ID <CAPTjJmqh_Qf6Hr=66fJgs5s-xtxYWd31FErj+MnXuOxkKKOOGg@mail.gmail.com>
On Wed, Nov 4, 2015 at 6:54 AM, Fredrik Hubinette <[email protected]> wrote:
> There are alternatives, not sure if they are better though:
>
>   foreach(sort((array)some_map), [mixed key, mixed value])
>
> Alternatively, we could implement a proper tree-based ADT.SortedMap
>
> It seems like your sort_mapping is really a generic iterator sorter though,
> so maybe
> it should be named something like ADT.SortIterator() or something.

Good point. I've renamed it accordingly. So as long as the iterator
supports indices() and values(), you can do this. Since you can sort
by values, this could be used to iterate over a sorted version of an
array, with the original indices:

array words = "Twas brillig, and the slithy toves Did gyre and gimble
in the wabe; All mimsy were the borogoves, And the mome raths
outgrabe."/" ";
foreach (SortIterator(words, 1); int pos; string word)
    write("%-10s at pos %d\n",word,pos);

ChrisA
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.