Bug 16998 – Provide a uniq & group range methods that doesn't rely on sortedness

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-12-22T02:27:50Z
Last change time
2024-12-01T16:28:26Z
Assigned to
No Owner
Creator
Seb
Moved to GitHub: phobos#10213 →

Comments

Comment #0 by greeenify — 2016-12-22T02:27:50Z
Sorting is in O(n log n) and requires a random access range (=allocation!) However plain iteration can be done in O(n) and groups can be created with a simple hashmap and e.g. the simplest implementation of uniq is sth. like this: auto uniq(R)(R r) { import std.algorithm.iteration : each; bool[dchar] d; r.each!(key => d[key] = true); return d.byKey; } unittest { import std.algorithm.iteration : walkLength; assert("cbbba"d.uniq.walkLength == 3); } The idea is that for the common use case (unsorted ranges), these non-lazy range functions allocate less and should perform faster.
Comment #1 by robert.schadek — 2024-12-01T16:28:26Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10213 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB