www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Adding values to AAs without double lookups?

reply Nick <Nick_member pathlink.com> writes:
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
next sibling parent David Medlock <noone nowhere.com> writes:
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?
 
 Nick
 
 

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
prev sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"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?

 Nick

I 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