www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Associative Array w/ Boxes.

reply AJG <AJG nospam.com> writes:
Hi there,

I have an AA, indexed with boxes. I'm not sure if I'm using it properly, 
though. Anyway, here's my problem:

This works:

import std.boxer;
int[Box] array;
array[box(1.3)] = 42;
assert(box(1.3) in array);

This also works:

char[] s = "key";
int[Box] array;
array[box(s)] = 42;
assert(box(s) in array);

But this doesn't work (assertion fails):

int[Box] array;
array[box("key")] = 42;
assert(box("key") in array);

Am I doing something wrong?
Thanks,
--AJG.
Aug 21 2005
next sibling parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <debnc0$29e0$2 digitaldaemon.com>, AJG says...
Hi there,

I have an AA, indexed with boxes. I'm not sure if I'm using it properly, 
though. Anyway, here's my problem:

This works:

import std.boxer;
int[Box] array;
array[box(1.3)] = 42;
assert(box(1.3) in array);

This also works:

char[] s = "key";
int[Box] array;
array[box(s)] = 42;
assert(box(s) in array);

But this doesn't work (assertion fails):

int[Box] array;
array[box("key")] = 42;
assert(box("key") in array);

Am I doing something wrong?
Thanks,
--AJG.

AJG, I'm not exactly sure why your last example errors out, but adding the .ptr (pointer to the array of characters) seems to work just fine. # private import std.boxer; # # int main() # { # int[Box] array; # array[box("key".ptr)] = 42; # assert(box("key".ptr) in array); # return 0; # } David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Aug 22 2005
parent AJG <AJG_member pathlink.com> writes:
Hi,

AJG, I'm not exactly sure why your last example errors out, but adding the .ptr
(pointer to the array of characters) seems to work just fine.

Thanks for the suggestion. The problem is that in my real code, I don't have access to the original string (and its ptr). In fact, it might not even be a string. It could be anything. Thus, the cycle would be like this: # class SomeClass { # int[Box] Array; # } 1) Create index in Array with box A (with anything inside). 2) Do various things. Original box/data is lost. 3) Check to see if index with box B (with anything inside) exists in Array. If (A == B), then Array should return that element. However, as shown before, that is failing. I'm clueless. What kind of comparison do the Boxes do when they are used as keys? Does it compare the internal pointer for identity, or does it check the actual data for equality? Thanks again, --AJG.
# private import std.boxer;
#
# int main()
# {
#     int[Box] array;
#     array[box("key".ptr)] = 42;
#     assert(box("key".ptr) in array);
#     return 0;
# }

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html

Aug 22 2005
prev sibling parent reply Burton Radons <burton-radons smocky.com> writes:
AJG wrote:

 Hi there,
 
 I have an AA, indexed with boxes. I'm not sure if I'm using it properly, 
 though. Anyway, here's my problem:
 
 This works:
 
 import std.boxer;
 int[Box] array;
 array[box(1.3)] = 42;
 assert(box(1.3) in array);
 
 This also works:
 
 char[] s = "key";
 int[Box] array;
 array[box(s)] = 42;
 assert(box(s) in array);
 
 But this doesn't work (assertion fails):
 
 int[Box] array;
 array[box("key")] = 42;
 assert(box("key") in array);

Blagh, I implemented opCmp "wrong". I have it as: float opCmp(Box other) { return opCmpInternal(other, false); } But for it to work with associative arrays, it must be: int opCmp(Box other) { return cast(int) opCmpInternal(other, false); } Which means that the comparison operators won't completely work. I'll switch it over anyway and mark it as a bug.
Aug 22 2005
parent reply AJG <AJG nospam.com> writes:
Hi,

 Blagh, I implemented opCmp "wrong".  I have it as:
 
     float opCmp(Box other)
     {
          return opCmpInternal(other, false);
     }

I'd been wondering about that float when I perused the source. ;)
 But for it to work with associative arrays, it must be:
 
     int opCmp(Box other)
     {
         return cast(int) opCmpInternal(other, false);
     }

 Which means that the comparison operators won't completely work.  I'll 
 switch it over anyway and mark it as a bug.

Hey, thanks a ton! I made the change, recompiled, and now it works! If you don't mind, did you take a look at my other boxer problem in the bugs newsgroup? Essentially, if I try to unbox void* I get linking errors, even though unboxable!() by itself returns true. Thanks again! --AJG.
Aug 22 2005
parent Burton Radons <burton-radons smocky.com> writes:
AJG wrote:
 Hi,
 
 Blagh, I implemented opCmp "wrong".  I have it as:

     float opCmp(Box other)
     {
          return opCmpInternal(other, false);
     }

I'd been wondering about that float when I perused the source. ;)

It's the right way to write the method, but TypeInfo_Struct is implemented a little too stupidly.
 But for it to work with associative arrays, it must be:

     int opCmp(Box other)
     {
         return cast(int) opCmpInternal(other, false);
     }

 Which means that the comparison operators won't completely work.  I'll 
 switch it over anyway and mark it as a bug.

Hey, thanks a ton! I made the change, recompiled, and now it works! If you don't mind, did you take a look at my other boxer problem in the bugs newsgroup? Essentially, if I try to unbox void* I get linking errors, even though unboxable!() by itself returns true.

It's a code generation bug, unsurprisingly. You can force it to link properly by prefixing the line with the dummy expression: typeid(Object);
Aug 22 2005