473,685 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nodes with unlimited children.

Hi All,

I plan on using the following C++ code
to create nodes with unlimited children:

// I would like to declare NodeT like this,
// but it won't compile because Lnk_T is not defined yet.

struct NodeT { Lnk_T Lnk ; };

So I have to declare NodeT like this instead:

struct NodeT {
struct { NodeT * * B, * * E, * * Room ; } Lnk ; };
typedef NodeT * Link ;

typedef Link * Link_P ;

// B is the Beginning of an array of pointers.
// E is the End of an array of pointers that are in use.
// Room the end of an array of all pointers, used or not.

struct Lnk_T { Link_P B, E, Room ; };

Lnk_T Lnk ;

enum { Chunk = 4,
Sz_Ptr = sizeof Link, Sz_Node = sizeof NodeT };

GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link_P ) realloc( Lnk.B, Room * Sz_Ptr );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Ptr ); }
// Below is an example of is how the above might be used,
// I know that it works.

__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {

GrowList ( Lnk ); // Lnk is a global, so it's initialized.

Link & P = * Lnk.E ++ ;

P = ( Link ) realloc( P, Sz_Node );

memset ( P, 0, Sz_Node );

GrowList ( ( Lnk_T & ) P->Lnk );

{ Link Passed = P, & P = * Passed->Lnk.E ++ ;

P = ( Link ) realloc( P, Sz_Node );

memset ( P, 0, Sz_Node );

GrowList ( ( Lnk_T & ) P->Lnk );

// etc.
}

But is there any way to declare NodeT so that
I don't have to use that ( Lnk_T & ) cast ?
Jul 22 '05 #1
47 2671
Jeff Relf wrote:
Hi All,

I plan on using the following C++ code
to create nodes with unlimited children:

// I would like to declare NodeT like this,
// but it won't compile because Lnk_T is not defined yet.

struct NodeT { Lnk_T Lnk ; };

So I have to declare NodeT like this instead:

struct NodeT {
struct { NodeT * * B, * * E, * * Room ; } Lnk ; };
typedef NodeT * Link ;

typedef Link * Link_P ;

// B is the Beginning of an array of pointers.
// E is the End of an array of pointers that are in use.
// Room the end of an array of all pointers, used or not.

struct Lnk_T { Link_P B, E, Room ; };

Lnk_T Lnk ;

enum { Chunk = 4,
Sz_Ptr = sizeof Link, Sz_Node = sizeof NodeT };

GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link_P ) realloc( Lnk.B, Room * Sz_Ptr );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Ptr ); }
// Below is an example of is how the above might be used,
// I know that it works.

__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {

GrowList ( Lnk ); // Lnk is a global, so it's initialized.

Link & P = * Lnk.E ++ ;

P = ( Link ) realloc( P, Sz_Node );

memset ( P, 0, Sz_Node );

GrowList ( ( Lnk_T & ) P->Lnk );

{ Link Passed = P, & P = * Passed->Lnk.E ++ ;

P = ( Link ) realloc( P, Sz_Node );

memset ( P, 0, Sz_Node );

GrowList ( ( Lnk_T & ) P->Lnk );

// etc.
}

But is there any way to declare NodeT so that
I don't have to use that ( Lnk_T & ) cast ?


That doesn't sound type safe.

And modern computing demands type safety.

--
incognito @ http://kentpsychedelic.blogspot.com
Jul 22 '05 #2
On 29 Jul 2004 11:06:12 GMT, Jeff Relf wrote:
Hi All,

I plan on using the following C++ code
to create nodes with unlimited children:

// I would like to declare NodeT like this,
// but it won't compile because Lnk_T is not defined yet.

struct NodeT { Lnk_T Lnk ; };

So I have to declare NodeT like this instead:

struct NodeT {
struct { NodeT * * B, * * E, * * Room ; } Lnk ; };
typedef NodeT * Link ;

typedef Link * Link_P ;

// B is the Beginning of an array of pointers.
// E is the End of an array of pointers that are in use.
// Room the end of an array of all pointers, used or not.

struct Lnk_T { Link_P B, E, Room ; };

Lnk_T Lnk ;

enum { Chunk = 4,
Sz_Ptr = sizeof Link, Sz_Node = sizeof NodeT };

GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link_P ) realloc( Lnk.B, Room * Sz_Ptr );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Ptr ); }
// Below is an example of is how the above might be used,
// I know that it works.

__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {

GrowList ( Lnk ); // Lnk is a global, so it's initialized.

Link & P = * Lnk.E ++ ;

P = ( Link ) realloc( P, Sz_Node );

memset ( P, 0, Sz_Node );

GrowList ( ( Lnk_T & ) P->Lnk );

{ Link Passed = P, & P = * Passed->Lnk.E ++ ;

P = ( Link ) realloc( P, Sz_Node );

memset ( P, 0, Sz_Node );

GrowList ( ( Lnk_T & ) P->Lnk );

// etc.
}

But is there any way to declare NodeT so that
I don't have to use that ( Lnk_T & ) cast ?


Good lord, that's the worst code I've ever seen. I hope I don't ever
have to maintain something of yours.

It's considered polite, when incrementing a pointer value, to bracket to
explicity show what you're intending, i.e.
(*p)++ return the value at the pointer, then increment the value
*(p++) return the value at the pointer, then increment the pointer

Maybe I got those backwards. Which is why you really want to be explicit.

--
FreeBSD 4.8-RELEASE i386
11:20AM up 130 days, 3:23, 1 user, load averages: 0.00, 0.01, 0.00
Jul 22 '05 #3
Karl Heinz Buchegger

Re: Nodes with unlimited children,

You wrote,
" Start the whole thing with a forward declaration of NodeT. "

Oh, how so very sweet, thank you much !

By the way,
I just realized that the code that I showed in my original
post was really designed for a NodeT of varying length,
which I don't think I really need.

So this is how it looks now ( and it works ),

struct NodeT ;

typedef NodeT * Link ;

// B is the Beginning of an array of NodeT's.
// E is the End of an array of NodeT's that are in use.
// Room the end of an array of all NodeT's, used or not.

struct Lnk_T { Link B, E, Room ; };

struct NodeT { Lnk_T Lnk ; };

Lnk_T Lnk ;

enum { Sz_Node = sizeof NodeT, Chunk_Lnk = 4 };

GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk_Lnk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link ) realloc( Lnk.B, Room * Sz_Node );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Node ); }

__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {

// Lnk is a global, so it's initialized.
// This creates a child for Lnk.
// There is no limit to the number of children.
GrowList ( Lnk );

// This creates a grandchild for Lnk.
GrowList ( ( * Lnk.E ++ ).Lnk );

// etc
}

You wrote,
" BTW: I consider your formatting style horrible. "

Everyone employs whitespace differently,
I even change my own style from time to time.

So I don't see how it's worth commenting on.
Jul 22 '05 #4

"Jeff Relf" <Jeff_Relf_@_NC Plus.NET.Invali d> wrote in message
news:_J******** *************** **@NCPlus.NET.. .
Hi All,

I plan on using the following C++ code
to create nodes with unlimited children:


<code snipped>

There's a clever way to get this effect with only two Node *'s per Node:

class Node
{
/* put data here */
Node *firstChild;
Node *nextSibling;
};

Then, to get all the children of a node, you just do
for (Node *child = myNode->firstChild; child != 0; child =
child->nextSibling) {
// whatever
}

This works great as long as you don't need constant-time access to the
nth child of a Node.

Joe Gottman


Jul 22 '05 #5
Hi General Protection Fault,

Re: Precedence vs. overused parentheses,
such as: ( * Lnk.E ++ ).Lnk in the following code,

struct NodeT ;

typedef NodeT * Link ;

// B is the Beginning of an array of NodeT's.
// E is the End of an array of NodeT's that are in use.
// Room the end of an array of all NodeT's, used or not.

struct Lnk_T { Link B, E, Room ; };

struct NodeT { Lnk_T Lnk ; };

Lnk_T Lnk ;

enum { Sz_Node = sizeof NodeT, Chunk_Lnk = 4 };

GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk_Lnk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link ) realloc( Lnk.B, Room * Sz_Node );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Node ); }

__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {

// Lnk is a global, so it's initialized.
// This creates a child for Lnk.
// There is no limit to the number of children.
GrowList ( Lnk );

// This creates a grandchild for Lnk.
GrowList ( ( * Lnk.E ++ ).Lnk );

// etc
}

If you're asking me,
it's much better to know the precedence of operators
than to overuse parentheses.

For example:
( * Lnk.E ++ ).Lnk is better than ( * ( Lnk.E ++ ) ).Lnk

But it's subjective, so why quibble ?

As for you maintaining my code,
I wouldn't worry about that if I were you.

Here is the order of precedence, highest first:
++ Post-increment, Left to right
-- Post-decrement
( ) Function call
[ ] Array element
-> Pointer to structure member
.. Structure or union member
++ Pre-increment, Right to left
-- Pre-decrement
! Logical NOT
~ Bitwise NOT
- Unary minus
+ Unary plus
& Address
* Indirection
sizeof Size in bytes
new Allocate program memory
delete Deallocate program memory
( type ) Type cast [ for example, ( float ) i ]
..* Pointer to member ( objects ), Left to right
->* Pointer to member ( pointers )
* Multiply, Left to right
/ Divide
% Remainder
+ Add, Left to right
- Subtract
<< Left shift, Left to right
Right shift
< Less than, Left to right
<= Less than or equal to Greater than
= Greater than or equal to

== Equal, Left to right
!= Not equal
& Bitwise AND, Left to right
^ Bitwise exclusive OR, Left to right
| Bitwise OR, Left to right
&& Logical AND, Left to right
|| Logical OR, Left to right
? : Conditional, Right to left
= Assignment, Right to left
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= Compound assignment
, Comma, Left to right

Jul 22 '05 #6
Jeff Relf wrote:
Hi General Protection Fault,

Re: Precedence vs. overused parentheses,
such as: ( * Lnk.E ++ ).Lnk in the following code,

struct NodeT ;

What a bunch of boring bullshit.

Here's something more interesting. I was writing a c# service and that
created a file that would then be transferred somewhere else. Originally I
coded some meta-data in the file name, but then because that had specific
size requirements, I had to use something else. How about File Properties
( right click on a text file, and look at the Properties ( third ) tab. It
has stuff like Author, Title, Subject.

I figure those nice m$ people created a cool assembly that would let me
set/get those. Well there is a class with get methods but not set methods.
So, I had to use a COM object that M$ supplies called DFOfile.dll

Yeah, so, when you dig a little beneath the pretty surface of .NET -- it's
all COM.

typedef NodeT * Link ;

// B is the Beginning of an array of NodeT's.
// E is the End of an array of NodeT's that are in use.
// Room the end of an array of all NodeT's, used or not.

struct Lnk_T { Link B, E, Room ; };

struct NodeT { Lnk_T Lnk ; };

Lnk_T Lnk ;

enum { Sz_Node = sizeof NodeT, Chunk_Lnk = 4 };

GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk_Lnk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link ) realloc( Lnk.B, Room * Sz_Node );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Node ); }

__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {

// Lnk is a global, so it's initialized.
// This creates a child for Lnk.
// There is no limit to the number of children.
GrowList ( Lnk );

// This creates a grandchild for Lnk.
GrowList ( ( * Lnk.E ++ ).Lnk );

// etc
}

If you're asking me,
it's much better to know the precedence of operators
than to overuse parentheses.

For example:
( * Lnk.E ++ ).Lnk is better than ( * ( Lnk.E ++ ) ).Lnk

But it's subjective, so why quibble ?

As for you maintaining my code,
I wouldn't worry about that if I were you.

Here is the order of precedence, highest first:
++ Post-increment, Left to right
-- Post-decrement
( ) Function call
[ ] Array element
-> Pointer to structure member
. Structure or union member
++ Pre-increment, Right to left
-- Pre-decrement
! Logical NOT
~ Bitwise NOT
- Unary minus
+ Unary plus
& Address
* Indirection
sizeof Size in bytes
new Allocate program memory
delete Deallocate program memory
( type ) Type cast [ for example, ( float ) i ]
.* Pointer to member ( objects ), Left to right
->* Pointer to member ( pointers )
* Multiply, Left to right
/ Divide
% Remainder
+ Add, Left to right
- Subtract
<< Left shift, Left to right
Right shift

< Less than, Left to right
<= Less than or equal to
Greater than
= Greater than or equal to

== Equal, Left to right
!= Not equal
& Bitwise AND, Left to right
^ Bitwise exclusive OR, Left to right
| Bitwise OR, Left to right
&& Logical AND, Left to right
|| Logical OR, Left to right
? : Conditional, Right to left
= Assignment, Right to left
*=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= Compound assignment
, Comma, Left to right


--
http://kentpsychedelic.blogspot.com
Jul 22 '05 #7
Oops, I should've said:

I prefer Lnk.E ++ -> Lnk over ( Lnk.E ++ ) -> Lnk

Jul 22 '05 #8
Hi Joe Gottman,

You showed something similar this:

class Node_Type {
Node * firstChild ;
Node * nextSibling ; };

for ( Node_Type * child = myNode->firstChild ;
child != 0 ;
child = child->nextSibling ) ...
For what I'm doing, I prefer my dynamic array of nodes.

such as: Lnk.E ++ -> Lnk ( defined below ),

That allows me to do things like: Lnk.B [ 4 ]
which directly references the fifth node.

My NodeT is very small and has a fixed size,
if NodeT was large or if it had a dynamic size
I'd use the code that I originally posted in this thread:
news:_J******** *************** **@NCPlus.NET

To loop though all the child nodes I define this:
( Lnk is a global, see below )

#define LoopChildren \
Link _Lnk = Lnk.B - 1 ; while ( ++ _Lnk < Lnk.E )

Which is then called like this:

LoopChildren _Lnk->Whatever ;

Here's how I declared those variables:

struct NodeT ;

typedef NodeT * Link ;

// B is the Beginning of an array of NodeT's.
// E is the End of an array of NodeT's that are in use.
// Room the end of an array of all NodeT's, used or not.

struct Lnk_T { Link B, E, Room ; };

struct NodeT { int Whatever ; Lnk_T Lnk ; };

Lnk_T Lnk ; // This is a global variable, so it's initialized.

enum { Sz_Node = sizeof NodeT, Chunk_Lnk = 4 };

GrowList ( Lnk_T & Lnk ) {
if ( Lnk.E + 1 < Lnk.Room ) return;
int Room = Lnk.Room - Lnk.B + Chunk_Lnk, E = Lnk.E - Lnk.B ;
Lnk.B = ( Link ) realloc( Lnk.B, Room * Sz_Node );
Lnk.Room = Lnk.B + Room ; Lnk.E = Lnk.B + E ;
memset( Lnk.E, 0, ( Lnk.Room - Lnk.E ) * Sz_Node ); }

__stdcall WinMain( HINSTANCE, HINSTANCE, LPSTR, int ) {

// This creates a child for Lnk.
// There is no limit to the number of children.
GrowList ( Lnk );

// This creates a grandchild for Lnk.
GrowList ( Lnk.E ++ -> Lnk );

// etc
}

Jul 22 '05 #9
Hi International All-Star Cast,

Win XP's File Properties is... well... specific to Win XP,
and therefore it can't be part of the main C# framework.

I'm sure that there is
plenty of C# code that only runs on Win XP.

To be sure that your C# code _ Shines _
on dual 64-bit PowerPCs as well as on some arcane cell phone,
you'd better damn well test it on all of those devices.
Jul 22 '05 #10

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

Similar topics

0
2777
by: Dinesh | last post by:
Hi, I have two tables 'master' and 'child', the master is the master table for all nodes in all trees. To get children of any node, we need to go to the 'child' table to get the nodeid of the children. The master has about 40,000 such trees with about 400 nodes in each tree. The input to me is the 'Root Node'/'First Node' of a tree. I need to traverse thru all the child nodes starting from 'Root Node' ( and process it ) and then...
8
1530
by: Xamle Eng | last post by:
One of the things I find most unnatural about most XML APIs is that they try to abstract both elements and text into some kind of "node" object when they have virtually nothing in common. The reason these APIs do it is to make it possible for both text and elements to be children of elements. But there is another way. The XPath/XQuery data model does not allow two consecutive text nodes. As far as I can tell, most XML processing...
12
1831
by: Ole Noerskov | last post by:
The function below is supposed to remove all childnodes with a name that starts with "keywords" in "myform" in the window.opener. It works fine if there's one keyword node. But you have to run the function several times if there are many keyword nodes. Why? function removeKeywords() { var form_obj = window.opener.document.getElementById('myform');
19
6768
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has one, and only one parent. The depth should not exceed 6 or 7 levels. The initial import will have about 6 million leaves, and 3 million branches. I would expect the leaves to grow significantly, in number easily tripling. However, the branches will...
6
3517
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not want to delete their child nodes("w:p","w:r","w:t"). How can I do this? <ns0:Proposal> <ns0:Company> <w:p> <w:r>
4
12836
by: J.B. | last post by:
Hi Can anybody show me how to loop all treeview nodes ini vb.net. I've been working on this for days but still no result. I got a sample code in vb 6.0, this one works. Private Sub SaveNested( Dim TNode As Node Set TNode = TreeView1.Nodes(1).Roo While Not TNode Is Nothin
2
2257
by: Kristopher Wragg | last post by:
I'm having some serious problems with the TreeView control. I've got a control that inherits TreeView and has some methods that firstly create a TreeNode then does some recursive procedure to add all the children from a database of a sort. Then once this is complete I clear the nodes, then add the TreeNode so it should be the only root node. The only problem is that for some very VERY strange reason there are two root nodes, with...
4
3020
by: reflex | last post by:
I have to get every node within range or selection? Is it possible? Sry for my engrish :] Ref
1
1800
by: Sasi Kumar | last post by:
I have a xml file. I want to display the nodes and childnodes in my datagrid, i used xmldatasource, but i can read a specific path only. The problem is i should not use dataset and xmldocument. Give me some suggestions <?xml version="1.0" encoding="utf-8"?> <newbookingrs> <bookingid>187250</bookingid> <bookingstatus>We have received your request. As you did not submit the required payment details, no booking will be held for you. If...
0
8507
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
8926
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...
1
8761
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8773
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
6431
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
5792
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
4302
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
2938
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
2198
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.