M
mike3
Hi.
I've been attempting to write a program in C++ that implements the
“weird kind of graph” I discussed in this thread:
http://groups.google.com/group/comp...033938a519c276f9?hl=en&lnk=gst&q="class+Bead+{%22+%2Bmike3#033938a519c276f9
and am wondering if how I've done it is a good idea or not. I was
following on the idea to use a conventional graph to underlie it, with
the “strings” of nodes implemented on top. So what I did was the
following:
1. There is a class “Web” which contains the strings, and has members
that allow for the adding, removing, and accessing of the stored
strings. It also contains a graph, on top of which the strings are
laid.
2. The strings are represented in a private member class of Web, which
contains among other things a list of iterators pointing to the nodes
in the graph that make up the string, as well as the string's ID code,
which is used to tag the string and also to identify the nodes in the
graph.
3. When a new string object is constructed (happens inside Web), the
constructor adds nodes into the graph (passed as argument) for the
string, and each node contains the unique ID code as a tag. The ID
codes are also passed as an argument, and Web keeps track to make sure
no collisions occur. ID codes are passed by the user of Web, so that
strings can be accessed by indicating the ID code (as there are two
access options: index and ID code, with ID code slower than direct
access by string index due to lookup, but we don't expect there to be
a huge volume of strings on a Web anyways, and it is robust against
string deletions which is why it's included. And collisions are
prevented by seeing if the ID code is already used – if it is, then
it's rejected on new string creation.).
4. When a string is removed, the destructor for the string object
removes the nodes. After removing a string, then all the other strings
(which are kept in a list in the web) must be updated (via a special
member function) to reset their iterators since the iterators are
invalidated when nodes are removed from the graph.
My questions relate to those internal details: namely, is it a good
idea to implement this where the constructor for the string object
creates nodes in the graph and the destructor then deletes them? As
what if we need to make a copy of that string object? This would also
seem to create another problem, since if a copy is made then its nodes
will have the same ID code, and that will clash when we go to update
on node removal. Would it be OK to just say not to pass string objects
by value (as that's my concern insofar as making copies goes)?
Also, is there any way by which the update step could be eliminated,
apart from using a graph whose iterators do not invalidate when nodes
are removed or is that otherwise an inevitable requirement? That part
is not so essential since this is not a performance-critical piece of
code.
Finally, would it be a good idea to perhaps separate the ID code used
to identify the string from the one used to identify the nodes, and
generate the one to identify nodes internally to the string object by
some approach (like have some unique-identifier approach or something,
with, say, 64-bit super-long ID codes that will never get exhausted in
any feasible amount of time? (it'd take 7 months to exhaust it at a
rate of a trillion creations per second which will never happen in
this program – is it a good idea to count on that never happening?
Personally, I'd think so, but I just want to be sure.))? This would
seem to have the advantage of allowing the string class to mind its
own business, so the web doesn't have to babysit it and its node-
discrimination abilities.
These questions are because I'm not entirely sure what would be a
“Good Design” for this, although what I have does seem to _work_, it's
just that it “feels” messy somehow.
I've been attempting to write a program in C++ that implements the
“weird kind of graph” I discussed in this thread:
http://groups.google.com/group/comp...033938a519c276f9?hl=en&lnk=gst&q="class+Bead+{%22+%2Bmike3#033938a519c276f9
and am wondering if how I've done it is a good idea or not. I was
following on the idea to use a conventional graph to underlie it, with
the “strings” of nodes implemented on top. So what I did was the
following:
1. There is a class “Web” which contains the strings, and has members
that allow for the adding, removing, and accessing of the stored
strings. It also contains a graph, on top of which the strings are
laid.
2. The strings are represented in a private member class of Web, which
contains among other things a list of iterators pointing to the nodes
in the graph that make up the string, as well as the string's ID code,
which is used to tag the string and also to identify the nodes in the
graph.
3. When a new string object is constructed (happens inside Web), the
constructor adds nodes into the graph (passed as argument) for the
string, and each node contains the unique ID code as a tag. The ID
codes are also passed as an argument, and Web keeps track to make sure
no collisions occur. ID codes are passed by the user of Web, so that
strings can be accessed by indicating the ID code (as there are two
access options: index and ID code, with ID code slower than direct
access by string index due to lookup, but we don't expect there to be
a huge volume of strings on a Web anyways, and it is robust against
string deletions which is why it's included. And collisions are
prevented by seeing if the ID code is already used – if it is, then
it's rejected on new string creation.).
4. When a string is removed, the destructor for the string object
removes the nodes. After removing a string, then all the other strings
(which are kept in a list in the web) must be updated (via a special
member function) to reset their iterators since the iterators are
invalidated when nodes are removed from the graph.
My questions relate to those internal details: namely, is it a good
idea to implement this where the constructor for the string object
creates nodes in the graph and the destructor then deletes them? As
what if we need to make a copy of that string object? This would also
seem to create another problem, since if a copy is made then its nodes
will have the same ID code, and that will clash when we go to update
on node removal. Would it be OK to just say not to pass string objects
by value (as that's my concern insofar as making copies goes)?
Also, is there any way by which the update step could be eliminated,
apart from using a graph whose iterators do not invalidate when nodes
are removed or is that otherwise an inevitable requirement? That part
is not so essential since this is not a performance-critical piece of
code.
Finally, would it be a good idea to perhaps separate the ID code used
to identify the string from the one used to identify the nodes, and
generate the one to identify nodes internally to the string object by
some approach (like have some unique-identifier approach or something,
with, say, 64-bit super-long ID codes that will never get exhausted in
any feasible amount of time? (it'd take 7 months to exhaust it at a
rate of a trillion creations per second which will never happen in
this program – is it a good idea to count on that never happening?
Personally, I'd think so, but I just want to be sure.))? This would
seem to have the advantage of allowing the string class to mind its
own business, so the web doesn't have to babysit it and its node-
discrimination abilities.
These questions are because I'm not entirely sure what would be a
“Good Design” for this, although what I have does seem to _work_, it's
just that it “feels” messy somehow.