digitalmars.D - Adding values to AAs without double lookups?
- Nick (30/30) Jul 08 2005 I have a function that takes a string as an argument, and returns an uni...
- David Medlock (6/43) Jul 08 2005 I agree, add() would be nice.
- Ben Hinkle (7/43) Jul 08 2005 I think it's a bug that & doesn't insert:
I have a function that takes a string as an argument, and returns an unique id to that string. It has to return the same id if the string is looked up twice, and the ids also have to be sequential (0,1,2..) since I use them as array indices later. This is my current solution: < int list[char[]]; < int current; < int addString(char[] str) < { < int *i = str in list; < if(i == null) < { // str is not in the list, we must add it < list[str] = current; // Double lookup! < return current++; < } < return *i; < } Does anyone have any tips on how this could be done without double lookups? If not, I would like to propose a .add property or similar for AAs that looks up a value and adds it if it is not present. This would work exactly as AA lookups did before Walter changed it. The code then becomes < int addString(char[] str) < { < int *i = &list.add(str); // Get pointer to new or existing element < if(*i == 0) // Define zero to mean a 'new' string < *i = ++current; // Assign the id (never zero, since we pre-increase) < return *i; < } Opinions anyone? Nick
Jul 08 2005
Nick wrote:I have a function that takes a string as an argument, and returns an unique id to that string. It has to return the same id if the string is looked up twice, and the ids also have to be sequential (0,1,2..) since I use them as array indices later. This is my current solution: < int list[char[]]; < int current; < int addString(char[] str) < { < int *i = str in list; < if(i == null) < { // str is not in the list, we must add it < list[str] = current; // Double lookup! < return current++; < } < return *i; < } Does anyone have any tips on how this could be done without double lookups? If not, I would like to propose a .add property or similar for AAs that looks up a value and adds it if it is not present. This would work exactly as AA lookups did before Walter changed it. The code then becomes < int addString(char[] str) < { < int *i = &list.add(str); // Get pointer to new or existing element < if(*i == 0) // Define zero to mean a 'new' string < *i = ++current; // Assign the id (never zero, since we pre-increase) < return *i; < } Opinions anyone? NickI agree, add() would be nice. I actually liked the way Walter had it before( in for checking, auto create when assigning/looking up). Seems like weird semantics now for no real benefit. -DavidM
Jul 08 2005
"Nick" <Nick_member pathlink.com> wrote in message news:dalsfo$1tj$1 digitaldaemon.com...I have a function that takes a string as an argument, and returns an unique id to that string. It has to return the same id if the string is looked up twice, and the ids also have to be sequential (0,1,2..) since I use them as array indices later. This is my current solution: < int list[char[]]; < int current; < int addString(char[] str) < { < int *i = str in list; < if(i == null) < { // str is not in the list, we must add it < list[str] = current; // Double lookup! < return current++; < } < return *i; < } Does anyone have any tips on how this could be done without double lookups? If not, I would like to propose a .add property or similar for AAs that looks up a value and adds it if it is not present. This would work exactly as AA lookups did before Walter changed it. The code then becomes < int addString(char[] str) < { < int *i = &list.add(str); // Get pointer to new or existing element < if(*i == 0) // Define zero to mean a 'new' string < *i = ++current; // Assign the id (never zero, since we pre-increase) < return *i; < } Opinions anyone? NickI think it's a bug that & doesn't insert: int[char[]] x; int* p = &x["hello"]; If the idea is that x["hello"] inserts when an lvalue is expected then & should do the same thing that x["hello"]++ does.
Jul 08 2005