digitalmars.D.learn - Accessing a Hash table as a Value-Sorted Range
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (11/11) Nov 19 2013 If I have a hash-table `File[string] _subs` and want to access
- Brad Anderson (4/15) Nov 19 2013 You could switch to a RedBlackTree which is sorted and offers
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (6/23) Nov 19 2013 I just found that I can use the .values property member:
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/27) Nov 19 2013 A bit more compact but probably the same logic (performance)
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/29) Nov 19 2013 There is also the lazy range .byValue:
If I have a hash-table `File[string] _subs` and want to access its values in a sorted way is there a better than simply through auto ssubs = new File[_subs.length]; // preallocate sorted subs size_t ix = 0; foreach (sub; _subs) { ssubs[ix++] = sub; // set new reference to sub } ssubs.sort!((a, b) => (a.timeLastModified > b.timeLastModified)); return ssubs;
Nov 19 2013
On Tuesday, 19 November 2013 at 21:11:42 UTC, Nordlöw wrote:If I have a hash-table `File[string] _subs` and want to access its values in a sorted way is there a better than simply through auto ssubs = new File[_subs.length]; // preallocate sorted subs size_t ix = 0; foreach (sub; _subs) { ssubs[ix++] = sub; // set new reference to sub } ssubs.sort!((a, b) => (a.timeLastModified > b.timeLastModified)); return ssubs;You could switch to a RedBlackTree which is sorted and offers somewhat fast lookup. Other than that, I'd say what you are doing is already about as good as you can get.
Nov 19 2013
On Tuesday, 19 November 2013 at 21:14:01 UTC, Brad Anderson wrote:On Tuesday, 19 November 2013 at 21:11:42 UTC, Nordlöw wrote:I just found that I can use the .values property member: import std.stdio, std.algorithm; string[int] x = [0:"b", 1:"a"]; auto y = x.values.map!("a~a"); writeln(y);If I have a hash-table `File[string] _subs` and want to access its values in a sorted way is there a better than simply through auto ssubs = new File[_subs.length]; // preallocate sorted subs size_t ix = 0; foreach (sub; _subs) { ssubs[ix++] = sub; // set new reference to sub } ssubs.sort!((a, b) => (a.timeLastModified > b.timeLastModified)); return ssubs;
Nov 19 2013
On Tuesday, 19 November 2013 at 22:42:10 UTC, Nordlöw wrote:On Tuesday, 19 November 2013 at 21:14:01 UTC, Brad Anderson wrote:A bit more compact but probably the same logic (performance) under the hood, right?On Tuesday, 19 November 2013 at 21:11:42 UTC, Nordlöw wrote:I just found that I can use the .values property member: import std.stdio, std.algorithm; string[int] x = [0:"b", 1:"a"]; auto y = x.values.map!("a~a"); writeln(y);If I have a hash-table `File[string] _subs` and want to access its values in a sorted way is there a better than simply through auto ssubs = new File[_subs.length]; // preallocate sorted subs size_t ix = 0; foreach (sub; _subs) { ssubs[ix++] = sub; // set new reference to sub } ssubs.sort!((a, b) => (a.timeLastModified > b.timeLastModified)); return ssubs;
Nov 19 2013
On 11/19/2013 02:42 PM, "Nordlöw" wrote:On Tuesday, 19 November 2013 at 21:14:01 UTC, Brad Anderson wrote:There is also the lazy range .byValue: x.byValue .values returns an array. It is kind of the equivalent of the following: import std.array; // ... x.byValue.array AliOn Tuesday, 19 November 2013 at 21:11:42 UTC, Nordlöw wrote:I just found that I can use the .values property member: import std.stdio, std.algorithm; string[int] x = [0:"b", 1:"a"]; auto y = x.values.map!("a~a"); writeln(y);If I have a hash-table `File[string] _subs` and want to access its values in a sorted way is there a better than simply through auto ssubs = new File[_subs.length]; // preallocate sorted subs size_t ix = 0; foreach (sub; _subs) { ssubs[ix++] = sub; // set new reference to sub } ssubs.sort!((a, b) => (a.timeLastModified > b.timeLastModified)); return ssubs;
Nov 19 2013