www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Doc confusion about AA rehash method

reply James Dunne <james.jdunne gmail.com> writes:
rehash:  "Reorganizes the associative array in place so that lookups are more
efficient. rehash is effective when, for example, the program is done loading up
a symbol table and now needs fast lookups in it. Returns a reference to the
reorganized array."

This says what seem to be (at first) two contradictory things:
1) rehash is an in-place operation.
2) returns a reference to the reorganized array.

The abiguity is in the 2nd statement.  Is the "reorganized array" the same array
reference you called the rehash property on?  If it is an in-place operation,
then I would assume so.  This should be rewritten.

Regards,
James Dunne
Aug 27 2005
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
James Dunne wrote in news:deqogk$27c8$1 digitaldaemon.com

[...]
 The abiguity is in the 2nd statement.
[...] Not an ambiguity, because .rehash may very well return a superfluous reference to the AA given. But the implementation shows, that .rehash is not a reorganization in place in all cases. -manfred
Aug 28 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Manfred Nowak" <svv1999 hotmail.com> wrote in message 
news:des8uk$g10$1 digitaldaemon.com...
 James Dunne wrote in news:deqogk$27c8$1 digitaldaemon.com

 [...]
 The abiguity is in the 2nd statement.
[...] Not an ambiguity, because .rehash may very well return a superfluous reference to the AA given.
Rehash returns the equivalent to "this".
 But the implementation shows, that .rehash
 is not a reorganization in place in all cases.
I would say rehash is in-place. It allocates memory but that memory is hidden from the user. The original array reference is updated and any user pointers to the key/value pairs are preserved. What rehash does is it takes the given lvalue array reference, rehashes it and assigns the new data (length+pointer) back into the lvalue and returns it.
Aug 28 2005
next sibling parent James Dunne <james.jdunne gmail.com> writes:
In article <desdu4$jtc$1 digitaldaemon.com>, Ben Hinkle says...
"Manfred Nowak" <svv1999 hotmail.com> wrote in message 
news:des8uk$g10$1 digitaldaemon.com...
 James Dunne wrote in news:deqogk$27c8$1 digitaldaemon.com

 [...]
 The abiguity is in the 2nd statement.
[...] Not an ambiguity, because .rehash may very well return a superfluous reference to the AA given.
Rehash returns the equivalent to "this".
 But the implementation shows, that .rehash
 is not a reorganization in place in all cases.
I would say rehash is in-place. It allocates memory but that memory is hidden from the user. The original array reference is updated and any user pointers to the key/value pairs are preserved. What rehash does is it takes the given lvalue array reference, rehashes it and assigns the new data (length+pointer) back into the lvalue and returns it.
So, always use the reference returned by the rehash property then? Is this how the sort property works also? If not, the two should be consistent in their usage patterns. That or the documentation should be clarified to reflect that the two behave the same. Regards, James Dunne
Aug 28 2005
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Ben Hinkle wrote:
<snip>
 I would say rehash is in-place. It allocates memory but that memory is 
 hidden from the user. The original array reference is updated and any user 
 pointers to the key/value pairs are preserved.
<snip> Rehashing in-place implies that all references to the same AA remain in sync. int[int] qwert; ... int[int] yuiop = qwert; int[int] asdfg = yuiop.rehash; assert (qwert is yuiop); assert (yuiop is asdfg); Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Aug 31 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message 
news:df42n3$1cvk$1 digitaldaemon.com...
 Ben Hinkle wrote:
 <snip>
 I would say rehash is in-place. It allocates memory but that memory is 
 hidden from the user. The original array reference is updated and any 
 user pointers to the key/value pairs are preserved.
<snip> Rehashing in-place implies that all references to the same AA remain in sync. int[int] qwert; ... int[int] yuiop = qwert; int[int] asdfg = yuiop.rehash; assert (qwert is yuiop); assert (yuiop is asdfg); Stewart.
I'm comfortable with in-place applying only to the specific reference (just as inserting an element into an AA) but since you and other apparently read the phrase "in-place" differently then clearly Walter needs to spell out what he means exactly by in-place. There would be no confusion if he stuck in a sentance that said exactly what it does. ps - what's with the variable names - yuiop, asdfg? Aren't a,b,c or x,y,z enough? :-)
Aug 31 2005
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Ben Hinkle wrote:
<snip>
     int[int] qwert;
     ...
     int[int] yuiop = qwert;
     int[int] asdfg = yuiop.rehash;

     assert (qwert is yuiop);
     assert (yuiop is asdfg);

 Stewart.
I'm comfortable with in-place applying only to the specific reference (just as inserting an element into an AA) but since you and other apparently read the phrase "in-place" differently then clearly Walter needs to spell out what he means exactly by in-place. There would be no confusion if he stuck in a sentance that said exactly what it does.
With structs and their having value semantics, it's perfectly possible to have methods that fail the first assert or even both asserts. OTOH a class can't fail the first assert, since it can't change what object yuiop references. But looking at internal/aaA.d, the function signature is aaA*[] _aaRehash(aaA*[]* paa, TypeInfo keyti) which seems to imply that an AA is an aaA*[]. And the code of the function implies that, if there's anything to do, it always reallocates the hashtable. So it seems you're right, and AAs behave the struct way to this level. The documentation needs tidying up a bit indeed.
 ps - what's with the variable names - yuiop, asdfg? Aren't a,b,c or x,y,z 
 enough? :-) 
Just my version of the old foo, bar, baz, etc.... http://www.foldoc.org/?metasyntactic+variable Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Aug 31 2005