473,289 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

avoiding boilerplate recursion code - visitors, iterators

Hi

I work with a nested tree-like data structure template (that happens
to be a tuple of vectors of tuples of trees of something, with
different tuple elements having different types)

I need to define a variety of ELEMENTWISE operations, like

a. increment all leaves (unary)
b. match two data structures (binary)
c. subtract two operands, write into third (ternary)
d. increment the first operand by a product of second, third and
fourth (quaternary)
... etc.

Because of the complexity of the data structure (it's more than a
simple tree), recursing through the whole thing requires 50 lines of
code.

I'd like to avoid writing this boilerplate code for each operation,
obviously.

If all elements in a given structure had the same type, I could define
iterators for this data structure, which would ease the task.

If all of the operations to be defined were unary, a visitor pattern
of some sort would be useful here (although even then, I would need a
const and non-const versions).

However, neither of these conditions applies. I could implement a
visitor-like pattern for each arity (up to 4), but that's roughly 300
lines of very dull code, ignoring const-ness.

Considering that a quaternary procedure can have 2^4 = 16 const-ness
types, this makes the problem of factoring out the boilerplate
recursion code for any possible elementwise operation even harder
(thousands of lines of code) and pointless.

Any suggestions?

Apr 30 '07 #1
3 2125
On May 1, 4:54 am, n.torrey.pi...@gmail.com wrote:
I work with a nested tree-like data structure template (that happens
to be a tuple of vectors of tuples of trees of something, with
different tuple elements having different types)

I need to define a variety of ELEMENTWISE operations, like

a. increment all leaves (unary)
b. match two data structures (binary)
c. subtract two operands, write into third (ternary)
d. increment the first operand by a product of second, third and
fourth (quaternary)
... etc.
[snip]
Considering that a quaternary procedure can have 2^4 = 16 const-ness
types, this makes the problem of factoring out the boilerplate
recursion code for any possible elementwise operation even harder
(thousands of lines of code) and pointless.

Any suggestions?
If you have iterators that can walk the structure then std::transform
should handle the unary case. Taking this idea further it ought to be
possible to build along those lines for what you want.

You will probably also want something like the std::back_inserter as
well, but that depends on how the structures work.

Take a look at std::bind, boost::bind and boost::lambda::bind - they
should help you write the transformation functions and apply them
within the transforms.
K

May 1 '07 #2
On Apr 30, 9:16 pm, Kirit Sælensminde <kirit.saelensmi...@gmail.com>
wrote:
On May 1, 4:54 am, n.torrey.pi...@gmail.com wrote:
I work with a nested tree-like data structure template (that happens
to be a tuple of vectors of tuples of trees of something, with
different tuple elements having different types)
I need to define a variety of ELEMENTWISE operations, like
a. increment all leaves (unary)
b. match two data structures (binary)
c. subtract two operands, write into third (ternary)
d. increment the first operand by a product of second, third and
fourth (quaternary)
... etc.

[snip]
Considering that a quaternary procedure can have 2^4 = 16 const-ness
types, this makes the problem of factoring out the boilerplate
recursion code for any possible elementwise operation even harder
(thousands of lines of code) and pointless.
Any suggestions?

If you have iterators that can walk the structure then std::transform
should handle the unary case. Taking this idea further it ought to be
possible to build along those lines for what you want.
I wrote about iterators (in the part you snipped) :

"If all elements in a given structure had the same type, I could
define
iterators for this data structure, which would ease the task."

IOW, the problem is that *i has different type, depending on where i
is pointing. I'm currently thinking about a kind of discriminated
union (variant) iterator generalization, but that too has its own
downsides, like runtime overhead compared to hand-written code.

May 1 '07 #3
On May 2, 1:40 am, n.torrey.pi...@gmail.com wrote:
On Apr 30, 9:16 pm, Kirit Sælensminde <kirit.saelensmi...@gmail.com>
wrote:
If you have iterators that can walk the structure then std::transform
should handle the unary case. Taking this idea further it ought to be
possible to build along those lines for what you want.

I wrote about iterators (in the part you snipped) :
Whoops! My reading comprehension often leaves a little something to be
desired :(
>
"If all elements in a given structure had the same type, I could
define
iterators for this data structure, which would ease the task."

IOW, the problem is that *i has different type, depending on where i
is pointing. I'm currently thinking about a kind of discriminated
union (variant) iterator generalization, but that too has its own
downsides, like runtime overhead compared to hand-written code.
Can you wrap the changing iterators into a containing iterator that de-
references to a abstract data type that can have the operations
applied to it? This doesn't mean that the items in the structure need
to be part of the same class hierarchy.

It might be a bit easier to understand if you give a concrete example
of how the items change. You must be able to store the non-homogeneous
data in the structure now. What does part of a structure actually look
like?

I've done something for the O/RM part of FOST.3 which also needs to
iterate over an arbitrary data structure (the classes, their
attributes and nested classes etc.). To do this I wrote a custom
message dispatcher to dynamically match the code with the type (using
reflection/RTTI) and/or attribute name.

In another part of the system we used boost::bind and
boost::lambda::bind to abstract out arguments and return types for
functions in order to schedule work for execution in separate threads.
K

May 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David C. Fox | last post by:
The __repr__ methods of the built-in list and dict objects properly avoid an infinite loop when you take the representation of a list or dictionary which contains itself (or more complicated...
5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
2
by: csx | last post by:
Hi all, I'm trying to count the number of leafnodes for a particular node. What im trying to do is make a function, that taking the tree structure: key row desc parent 1 1 A ...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
3
by: zr | last post by:
Hi, Does usage of checked iterators and checked containers make code more secure? If so, can that code considered to be reasonably secure?
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.