473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template recursive class

I've managed to put together a template class that basicaly creates a
recursive tree that lets you easily specify the "base" class of that tree
and and ending notes and lets you stop the recursive process if you want.

The problem now is to make a Type list so I can specify more than one node
at a time to "attach" a class to. I think I will be able to handle this but
I want to run the code by you guys to see there are any major design flaws.
The code is just a "prototype" and will be rewritten once I get the idea
done. The idea is to get something like

sTree<4, 4, AllClass, MyEndClassLeft, MyEndClassRight , TypeList<Node<2 , 3,
MyClassA>, Node<3, 3, MyClassB>, Node<1, 3, MyClassC>> > S;

Which would produce a tree that as for each node a class of AllClass except
the nodes given in the type list and the last nodes are give by
MyEndClassLeft and MyEndClassRight .

Ofcourse right now I can only specify one Node type because I have not
implemented a TypeList.
Anyways, while this models my original problem more than enough I thought it
would be nice to make it as general as I possibly could. But maybe it could
be done much easier? One thing I'm not sure is a good idea is to have the
"user attached" classes be the base classes for the nodes because this might
cause problems with casting? But that was the only way I could see to make
it simple enough without having the user have to worry about deriving things
and all that.

Any ideas?

Thanks, Jon

(The code, again, isn't ment to be perfect... each function in the classes
are just for "looks" and I could have done a better job at something but
please only stick to the real issues)

Oh, one more thing, is there an easy way to have the user choose from using
a vector based container(or whatever type of container) instead of just a
type..

i.e.
sTree<I-1, J, A, L, R, T> Left;

into something like

list<*sTree<I-1, J, A, L, R, T> > Left;

without having to do a crap load of specializing?

i.e.

sTree<4, 4, List, Vector, AllClass, MyEndClassLeft, MyEndClassRight , Node<2,
3, MyClass> > S;

Where sTree uses lists for the Left branches and vectors for the right.

----------------------------------------

#include <iostream>

using namespace std;

struct NullClass { void Null() { } };

template <unsigned int i, unsigned int j, typename T, unsigned int s = 0>
struct Node
{
enum {I = i};
enum {J = j};
enum {S = s};
typedef T Class;
};
template <int I, int J, typename A, typename L, typename R, typename T>
struct sTree : A
{
sTree<I-1, J, A, L, R, T> Left;
sTree<I, J-1, A, L, R, T> Right;
void sTreeFunc() { }
};
// Specializes node type for full recursive process
template <typename A, typename L, typename R, unsigned int I, unsigned int
J, typename T>
struct sTree< I, J, A, L, R, Node<I,J,T,0> > : Node<I,J,T,0>:: Class
{
sTree<I, J, A, L, R, T> Left;
sTree<I, J, A, L, R, T> Right;
void Spec() {}
};

// Specializes node type to end left recursive process
template <typename A, typename L, typename R, unsigned int I, unsigned int
J, typename T>
struct sTree< I, J, A, L, R, Node<I,J,T,1> > : Node<I,J,T,1>:: Class
{
sTree<I, J, A, L, R, T> Right;
void Spec() {}
};

// Specializes node type to end right recursive process
template <typename A, typename L, typename R, unsigned int I, unsigned int
J, typename T>
struct sTree< I, J, A, L, R, Node<I,J,T,2> > : Node<I,J,T,2>:: Class
{
sTree<I, J, A, L, R, T> Left;
void Spec() {}
};

// Specializes node type to end recursive process
template <typename A, typename L, typename R, unsigned int I, unsigned int
J, typename T>
struct sTree< I, J, A, L, R, Node<I,J,T,3> > : Node<I,J,T,3>:: Class
{
void Spec() {}
};

// Specializes last "left" node
template <unsigned int J, typename A, typename L, typename R, typename T>
struct sTree<0, J, A, L, R, T> : L
{
void End() { }
};

// Specializes last "right" node
template <unsigned int I, typename A, typename L, typename R, typename T>
struct sTree<I, 0, A, L, R, T> : R
{
void End() { }
};

// User defined classes for "attaching" to the tree
struct AllClass { void All() { } };
struct MyClass { void MyFunc() { } };
struct MyEndClassLeft { void MyEndLeft() { } };
struct MyEndClassRight { void MyEndRight() { } };

int main( int argc, char* argv[] )
{

sTree<4, 4, AllClass, MyEndClassLeft, MyEndClassRight , Node<2, 3, MyClass> >
S;

system("PAUSE") ;
return 0;
}
Sep 27 '05 #1
1 2380
Does it work?

Ben
Sep 28 '05 #2

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

Similar topics

1
3673
by: SpeedBump | last post by:
This is mostly just another "gee it would be nice if it had X" post. Recently I have come across two separate problems which both exhibit the need for a way to self reference when instantiating a template. Example #1: B-trees While implementing a B-Tree I ended up with an (abbreviated) node structure looking something like this:
2
2534
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1 template parameter, one can specialize the method
6
1575
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each B can be child to zero, one, or more A's. I just call that "A is a parent of B", and "B is a child of A". The same goes for B and C and for C and D. So A is only a parent, B and C are both parents and children, and D is only a child.
2
1974
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of "transform" which works not only on lists, but lists of list, lists of lists of lists, etc. I'm calling it "fmap" and it passes around actual lists instead of iterators (for now). In order to get it to work, I thought I'd have one templated...
6
1920
by: rep_movsd | last post by:
Hi folks I was on topcoder and came across an interesting problem... It involved dynamic programming ( storing function results in a map to avoid repeated computation ) and I ended up having to write 3 functions that looked pretty much the same, so I thought- "Why not template - ize the whole thing" and ended up with a generic Memoizer template, which can be used to memoize any recursive function whose result depends on multiple...
3
2477
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
4
2570
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public TXpQAlloc<MyClass,N> N is a chunking factor (defaults to 10).
7
2980
by: er | last post by:
hi, could someone please help with this code? template<unsigned int N,unsigned int M> class A{ public: A(); };
12
3336
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
8413
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
8842
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
8740
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
8617
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...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
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
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.