Any interest in multi_index library

S

Sebastian Galkin

I wonder if the community has any interest in a library a la
boost::multi_index for Ruby.

For those who don't know the library, it implements containers indexed
by several fields. For instance you can have a container of Person
objects indexed uniquely by SSN and non uniquely by birth date. You can
also specify if the indexes are ordered or unordered. The mechanism
used to extract the key from the object, is configurable.
From boost web page:

"The Boost Multi-index Containers Library provides a class template
named multi_index_container which enables the construction of
containers maintaining one or more indices with different sorting and
access semantics."
"The concept of multi-indexing over the same collection of elements
is borrowed from relational database terminology and allows for the
specification of complex data structures in the spirit of multiply
indexed relational tables where simple sets and maps are not enough."


This kind of container is helpfull and I'm sure some of you have
implemented something similar at some point. It allows you to have the
data consistent and centralized while giving several views (the
indices) according to different criteria. Of course these views are
editable, and thats where much of their power comes from.

Maybe there is already an implementation of this available in ruby,
but I couldn't find any.
 
T

Timothy Goddard

A simple wrapper from ruby to this library simply isn't possible as it
depends on template metaprogramming and template needs must be known at
compile-time. This would need to be done in pure ruby, starting with
something like this as a base:

class MultiIndex
def initialize(*indices)
@maps = {}
indices.flatten.each {|i| @maps = {}}
end

def get(index, key)
if !(@maps.has_key?(index))
raise ArgumentError, "Invalid index"
end
return @maps[index][key]
end
alias [] get

def add(entry, *args)
@maps.each do |key, map|
map[entry.send(key, *args)] = entry
end
end
alias << add
end

This assumes all indices are unique and newer versions will displace
old ones.
 
S

Sebastian Galkin

A simple wrapper from ruby to this library simply isn't possible as it
depends on template metaprogramming and template needs must be known > at compile-time. This would need to be done in pure ruby

I agree.
This assumes all indices are unique and newer versions will displace
old ones

The displace behaviour pretty much destroys the utility of the index.

I think that the interface for each index should model its nature and
the container should be only the "manager" of indices. For instance you
have something like a SortedSet for unique ordered indices. This way,
you interact directly with the index that has more meaning to you. That
makes thinks very simple for clients who don't need to know the
multiindexed nature of the container (this is not entirely true, as
clients using a non unique index could have their insertion rejected
because of conflicts in another index).

boost::multi_index designers choosed to give the container the
interface of its first index. Thats a very smart way of keeping code
alive when you realize that you don't need one but two indices.
 
P

Pablo Russo

Hey, I am a senior .net developer, making my first steps in ruby. I
have used some parts of the boost library and I can say the multi_index
is something really usefull to have. (btw, this is something I would
like to see in C# too, but thats offtopic)

I agree with you, the right way of doing it is hiding from the user
that there is more than one index.

I would gladly participate in the design or develop of the library, in
case you need any help.

Cheers,
Pablo.-
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top