seb30
February 21st, 2008, 03:11 PM
Hi,
I wonder if there is an efficient datastructure with an interface like this (For example, in C++. Answers in other programming languages or pseudo languages or book references are welcome, too):
class Set_Id_Factory
{
public:
SetId empty_set();
SetId insert(SetId, Element);
private:
....
};
SetId should be an integral type (or a pointer), the factory may choose them.
The following condition should hold:
insert(...insert(empty_set(), a_1)..., a_n) == insert(...insert(empty_set(), b_1)..., b_m)
where a_1...a_n is the same set of elements as b_1...b_m, but can be in a different order and have different duplication of elements.
I know I can implement it like this (C++ Pseudo-Code!):
vector<set<Element>> id2set;
map<set<Element>, unsigned> set2id;
unsigned insert(unsigned id, Element e)
{
set<Element> s = id2set(id);
s.insert(e);
if(not set2id.has_key(s))
{
id2set.append(s);
set2id[s] = id2set.size();
}
return set2id[s];
}
But I wonder if there is a more efficient solution which doesn't need as much space (doesn't store each element of each set separately) and/or as much time (ideally less than linear with respect to the size of the set s).
Thanks,
Sebastian
I wonder if there is an efficient datastructure with an interface like this (For example, in C++. Answers in other programming languages or pseudo languages or book references are welcome, too):
class Set_Id_Factory
{
public:
SetId empty_set();
SetId insert(SetId, Element);
private:
....
};
SetId should be an integral type (or a pointer), the factory may choose them.
The following condition should hold:
insert(...insert(empty_set(), a_1)..., a_n) == insert(...insert(empty_set(), b_1)..., b_m)
where a_1...a_n is the same set of elements as b_1...b_m, but can be in a different order and have different duplication of elements.
I know I can implement it like this (C++ Pseudo-Code!):
vector<set<Element>> id2set;
map<set<Element>, unsigned> set2id;
unsigned insert(unsigned id, Element e)
{
set<Element> s = id2set(id);
s.insert(e);
if(not set2id.has_key(s))
{
id2set.append(s);
set2id[s] = id2set.size();
}
return set2id[s];
}
But I wonder if there is a more efficient solution which doesn't need as much space (doesn't store each element of each set separately) and/or as much time (ideally less than linear with respect to the size of the set s).
Thanks,
Sebastian