Re: Collections of structured-data objects: what approach?



Graham Wideman wrote:
Robert:

Question and then a comment:

Question: What does the keyword "self" do when sitting on a line by itself, as in several of your methods?

Simen answered that one. As an additional note, traditionally #each returns self. Also, by doing this you can chain methods.

Comment:

For the sake of discussion I present a different approach. As long as
you don't need 4 (lookup by key) an Array is perfect. However, with the
lookup IMHO a new data structure is preferred because then consistency
can be handled internally. I'll attach a half grown quick hack to
demonstrate what I mean.

If I'm reading you correctly, you are saying a bit more than this:

1. For lookup (or for that matter if one want to implement a SortedCollection), then it's desirable to have an index for the array on the key field(s), which can be a ruby hash. (Ie: one *could* use Enumerable#find or find_all, but on larger collections that's slow?)

find is O(n) which can seriously hurt your applications performance if the Array grows beyond a certain limit. Hash lookup on the other hand is O(1).

2. But if you are going to implement an index using a helper hash, then you want a way to keep the hash up-to-date as items are inserted or deleted from the array.

3. So you advocate a class to wrap these together so that "consistency can be handled internally".

Right. You got it. That's what OO is about.

Kind regards

robert
.



Relevant Pages

  • Re: Collections of structured-data objects: what approach?
    ... you don't need 4 (lookup by key) an Array is perfect. ... then it's desirable to have an index for the array on the ... But if you are going to implement an index using a helper hash, ... Microsoft Visio MVP ...
    (comp.lang.ruby)
  • Re: Associative Array in C
    ... OK so my current idea for implementing this is using an array of C ... If you provided the errors in sorted order, ... do the lookup. ... I guess that a hash table will work perfectly well and be the best ...
    (comp.lang.c)
  • Re: hash tables, non-random keys
    ... large numbers of insertions, or I would just use an array. ... optimize hash tables). ... I guess then the issue is how frequent lookup fails are... ...
    (comp.programming)
  • Re: hash tables, non-random keys
    ... large numbers of insertions, or I would just use an array. ... optimize hash tables). ... I guess then the issue is how frequent lookup fails are... ...
    (comp.programming)
  • Re: Idea for ECMA/C# Standard - compile time hash for performance
    ... contains 100 fields that I do hash lookups on to get and set the values. ... counting the lookup then and processing. ... this could be done with a straight array lookup using the minimal memory ... > The key is I don't want to burn a huge amount of memory and I want to incur ...
    (microsoft.public.dotnet.languages.csharp)

Loading