473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2154
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_inser ter 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.saelensm i...@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.saelensm i...@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
2453
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 nestings: list l1 contains list l2 which contains l1, etc.). Looking at the CPython source code, it seems that the Py_ReprEnter and Py_ReprLeave functions are used to implement this protection. Is there any equivalent function for defining new...
5
3424
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. And so it says ANTLR. Maybe I don't understand EBNF notation. For me it should look like this. or_expr::= xor_expr | xor_expr "|" xor_expr and in ANTLR grammar file like this:
2
2849
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 0 2 2 B 1 3 2 C 1 4 3 D 3
43
4172
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
3641
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 inheritance (avoiding prosperation of virtual func tables). -- At the same time, a class taking N types shall contain a virtual member function that calls a function according to the number of arguments That means, something like:
13
4528
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 would be a good method to detect recursion loops and stop it by user-Exception (after N passes or some complex criteria) without passing a recursion counter parameter through all the funcs? Robert
30
8308
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? Surprisingly, deep recursion actually isn't that slow for what I'm doing. Programming this task recursively is so much more straightforward to me, but currently I'm forced to use an ugly hack to avoid going over the 1000 level limit. Of course, it could...
35
4741
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
2755
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
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.