Friday, November 13, 2009, 9:45:10 AM, you wrote:
RK> On 12.11.2009 21:24, Caleb C. wrote:
tell Ruby, “OK, I’m done with this array.”
Can one create such an array such that the garbage collector can
free the memory in one fast operation?
I’m surprised that no-one has yet mentioned narray; I’d think it’s the
ideal data structure for this kind of problem. (I’ve never used it,
tho.)
RK> Actually everybody waited for you to finally come up with your
reply.
RK> Shame on you that it took you so long.
I’d like to point out that while the memory cost of a Fixnum is small
(4 or 8 bytes), the memory cost of a Bignum is considerably larger…
something like 32 bytes or so, even if it’s only storing a 64bit
value. If you expect a significant number of Bignums to be stored in
this array, you should be aware of this. Yet another reason to
consider narray, to my mind.
RK> Certainly. However, if Bignums work as well, why worry to use
another
RK> lib? Whether the allocation overhead is OK or not depends of course
on
RK> the use case. Btw, did we see a more concrete description of the
RK> problem? I cannot remember having seen it.
I am the OP and there really isn’t much more to the problem.
Basically, each row of the array is a list of integers. The first
element of the array is an id. The remaining elements of the array are
a list of ids.
Thus, if
MyMatrix = [ [100, [200, 300, 400]],
[200, [300, 400, 500, 600]],
[300, [400, 600, 555, 1902, 1708]],
[400, 101, 200, 100] ]
If I am processing the last vector (i.e. [400, 101, 200, 100]) then I
want to be able to note that
101 has no corresponding element
200 was found and its remaining content is [300, 400, 500, 600]
100 was found and its remaining content is [200, 300, 400]
Speed is critical. The array is more-or-less static. That is, access
to this array swamps any changes to the array.
The order of the rows is irrelevant … and for speed reasons the
Matrix will be sorted by the first column so that I can do a binary
search.
Of course, I could use a hash … but I doubt that a hash would beat
my binary search … I could be wrong about this.
RK> Kind regards
RK> robert