Multi-Index Containers

Discussion in 'Ruby' started by Avdi Grimm, Dec 1, 2008.

  1. Avdi Grimm

    Avdi Grimm Guest

    Does anyone know of a multi-index container library for Ruby? I just
    want to check before I put it on my personal TODO list.

    By multi-index, I mean a container which will index its contained
    objects by more than one attribute. E.g. something along these lines:

    mi = MultiIndex.new
    mi.add_index:)prefix) {|val| val[0,3]}
    mi.add_index:)size)

    mi << "foobar"
    mi << "foobaz"
    mi << "fuzz"
    mi << "fuzzbizz"

    mi.by_prefix("foo") # => ["foobar", "foobaz"]
    mi.by_prefix("fuz") # => ["fuzz", "fuzzbizz"]
    mi.by_size(4) # => ["fuzz"]
    mi.by_size(8) #= ["fuzzbizz"]

    Yes, I know I can just uses #select on Ruby's existing containers, but
    the idea here is that a MultiIndex would index each attribute on
    insert, making lookups faster than using a #select.

    So: does this exist?

    --
    Avdi

    Home: http://avdi.org
    Developer Blog: http://avdi.org/devblog/
    Twitter: http://twitter.com/avdi
    Journal: http://avdi.livejournal.com
    Avdi Grimm, Dec 1, 2008
    #1
    1. Advertising

  2. Avdi Grimm

    ara.t.howard Guest

    On Dec 1, 2008, at 8:53 AM, Avdi Grimm wrote:

    > By multi-index, I mean a container which will index its contained
    > objects by more than one attribute. E.g. something along these lines:



    gem install rbtree


    a @ http://codeforpeople.com/
    --
    we can deny everything, except that we have the possibility of being
    better. simply reflect on that.
    h.h. the 14th dalai lama
    ara.t.howard, Dec 1, 2008
    #2
    1. Advertising

  3. Avdi Grimm

    Avdi Grimm Guest

    Avdi Grimm, Dec 1, 2008
    #3
  4. On 01.12.2008 16:53, Avdi Grimm wrote:
    > Does anyone know of a multi-index container library for Ruby? I just
    > want to check before I put it on my personal TODO list.
    >
    > By multi-index, I mean a container which will index its contained
    > objects by more than one attribute. E.g. something along these lines:
    >
    > mi = MultiIndex.new
    > mi.add_index:)prefix) {|val| val[0,3]}
    > mi.add_index:)size)
    >
    > mi << "foobar"
    > mi << "foobaz"
    > mi << "fuzz"
    > mi << "fuzzbizz"
    >
    > mi.by_prefix("foo") # => ["foobar", "foobaz"]
    > mi.by_prefix("fuz") # => ["fuzz", "fuzzbizz"]
    > mi.by_size(4) # => ["fuzz"]
    > mi.by_size(8) #= ["fuzzbizz"]
    >
    > Yes, I know I can just uses #select on Ruby's existing containers, but
    > the idea here is that a MultiIndex would index each attribute on
    > insert, making lookups faster than using a #select.
    >
    > So: does this exist?


    I haven't seen it so far. Basically this would mimic what a relational
    DB does in memory.

    Not difficult to write though.

    class Table
    def initialize(index_keys)
    @indexes =
    index_keys.inject({}) do |idx,key|
    idx[key]= Hash.new {|h,k| h[k]=[]}
    idx
    end
    end

    def add(o)
    @indexes.each do |key,idx|
    if Array === key
    idx[o.send(key)] << o
    else
    idx[key.map{|k| o.send(k)}] << o
    end
    end
    self
    end
    end

    Kind regards

    robert
    Robert Klemme, Dec 1, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    3
    Views:
    388
    Kevin Goodsell
    Apr 19, 2004
  2. ALiX
    Replies:
    8
    Views:
    318
  3. Replies:
    7
    Views:
    547
    Pete Becker
    Jan 25, 2008
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    275
    Tomasz Chmielewski
    Mar 4, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    307
Loading...

Share This Page