digitalmars.D.learn - How to make AA key a pointer
- Clinton (21/21) Feb 19 2018 Hi all, I need advice from better developers on this concern.
- Clinton (6/9) Feb 19 2018 Sorry, on second look my explanation isn't very clear. I want to
- Rene Zwanenburg (4/15) Feb 19 2018 It's a pointer. In D, string is an alias to immutable(char)[]
- ketmar (15/25) Feb 19 2018 there is absolutely no reason to copy `string` ever, as it is `immutable...
- Clinton (4/29) Feb 20 2018 Thanks. I actually did a similar test a little while ago and
Hi all, I need advice from better developers on this concern. I'm using an AA to reference another array for quicker access: [code] alias contactId = string; bool[contactId][] matches; ulong[contactId] idsToMatches; bool[string] matchesForId(string id) { return matches.get(idsToMatches[id], bool[string].init); } [/code] Just wondering, how do I set the keys to avoid copying the id string? So, let's say ids come from another array of structs(e.g. Contact[]). I want to avoid having two copies of the string id value in both of these AAs above (to avoid using extra memory usage). The reason this is a potential issue is because these arrays can get extremely large. This is to match duplicate contacts and group them in matches. The reason I use bool is because it's the smallest size type and I don't think I can use void. I guess my question is: does dmd already create pointers to the id from the AA, or is each new key a new allocation?
Feb 19 2018
On Monday, 19 February 2018 at 14:55:01 UTC, Clinton wrote:Hi all, I need advice from better developers on this concern. I'm using an AA to reference another array for quicker access: [...]Sorry, on second look my explanation isn't very clear. I want to know if: bool[string] myAA; myAA[contact.id] = true; // Does this copy contact.id or is this a pointer to contact.id?
Feb 19 2018
On Monday, 19 February 2018 at 14:57:47 UTC, Clinton wrote:On Monday, 19 February 2018 at 14:55:01 UTC, Clinton wrote:It's a pointer. In D, string is an alias to immutable(char)[] (Slice of immutable characters). A slice is a combination of pointer and length.Hi all, I need advice from better developers on this concern. I'm using an AA to reference another array for quicker access: [...]Sorry, on second look my explanation isn't very clear. I want to know if: bool[string] myAA; myAA[contact.id] = true; // Does this copy contact.id or is this a pointer to contact.id?
Feb 19 2018
Clinton wrote:On Monday, 19 February 2018 at 14:55:01 UTC, Clinton wrote:there is absolutely no reason to copy `string` ever, as it is `immutable`. and compiler knows that. anyway, why don't you just check it by writing the code first? import std.stdio; void main () { int[string] a; string s = "test"; writefln("%08x", s.ptr); a[s] = 666; s = "test1"; writefln("%08x", s.ptr); a[s] = 42; foreach (string k; a.byKey) writefln("%08x", k.ptr); }Hi all, I need advice from better developers on this concern. I'm using an AA to reference another array for quicker access: [...]Sorry, on second look my explanation isn't very clear. I want to know if: bool[string] myAA; myAA[contact.id] = true; // Does this copy contact.id or is this a pointer to contact.id?
Feb 19 2018
On Monday, 19 February 2018 at 15:02:29 UTC, ketmar wrote:Clinton wrote:Thanks. I actually did a similar test a little while ago and found it out. Thanks for confirming. I still struggle a bit with these basic things.On Monday, 19 February 2018 at 14:55:01 UTC, Clinton wrote:there is absolutely no reason to copy `string` ever, as it is `immutable`. and compiler knows that. anyway, why don't you just check it by writing the code first? import std.stdio; void main () { int[string] a; string s = "test"; writefln("%08x", s.ptr); a[s] = 666; s = "test1"; writefln("%08x", s.ptr); a[s] = 42; foreach (string k; a.byKey) writefln("%08x", k.ptr); }[...]Sorry, on second look my explanation isn't very clear. I want to know if: bool[string] myAA; myAA[contact.id] = true; // Does this copy contact.id or is this a pointer to contact.id?
Feb 20 2018