Nodes with unlimited children.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Relf

    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 ?


  • Jumpin' Jehosephat

    #2
    Better Safe Than Sorry...

    Jeff Relf wrote:[color=blue]
    > 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 ?
    >
    >[/color]

    That doesn't sound type safe.

    And modern computing demands type safety.



    --
    incognito @ http://kentpsychedelic.blogspot.com

    Comment

    • General Protection Fault

      #3
      Re: Nodes with unlimited children.

      On 29 Jul 2004 11:06:12 GMT, Jeff Relf wrote:[color=blue]
      > 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 ?[/color]

      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

      Comment

      • Jeff Relf

        #4
        Re: Nodes with unlimited children.

        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.


        Comment

        • Joe Gottman

          #5
          Re: Nodes with unlimited children.


          "Jeff Relf" <Jeff_Relf_@_NC Plus.NET.Invali d> wrote in message
          news:_Jeff_Relf _2004_Jul_29_6q MA@NCPlus.NET.. .[color=blue]
          > Hi All,
          >
          > I plan on using the following C++ code
          > to create nodes with unlimited children:[/color]

          <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




          Comment

          • Jeff Relf

            #6
            Precedence vs. overused parentheses,

            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[color=blue][color=green]
            >> Right shift[/color][/color]
            < Less than, Left to right
            <= Less than or equal to[color=blue]
            > Greater than
            >= Greater than or equal to[/color]
            == 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

            Comment

            • International All-Star Cast

              #7
              COM+

              Jeff Relf wrote:
              [color=blue]
              > Hi General Protection Fault,
              >
              > Re: Precedence vs. overused parentheses,
              > such as: ( * Lnk.E ++ ).Lnk in the following code,
              >
              > struct NodeT ;
              >[/color]

              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.

              [color=blue]
              > 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[color=green][color=darkred]
              >>> Right shift[/color][/color]
              > < Less than, Left to right
              > <= Less than or equal to[color=green]
              >> Greater than
              >>= Greater than or equal to[/color]
              > == 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[/color]

              --

              Comment

              • Jeff Relf

                #8
                Precedence vs. overused parentheses,

                Oops, I should've said:

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

                Comment

                • Jeff Relf

                  #9
                  Re: Nodes with unlimited children.

                  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:_Jeff_Relf _2004_Jul_29_6q MA@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
                  }



                  Comment

                  • Jeff Relf

                    #10
                    Re: COM+

                    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.


                    Comment

                    • Sargeant Vince Carter

                      #11
                      Web Services Eliminate Testing

                      Jeff Relf wrote:
                      [color=blue]
                      > 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.[/color]

                      Sure, sure, but it's a web service...so I could care less.

                      Yesterday, I wrote an synchronous Delegate ( what you overtrained c++ might
                      term a callback function ) so that my GUI web service consumer could do
                      work in the background.

                      It's very cool.

                      --

                      Comment

                      • Karl Heinz Buchegger

                        #12
                        Re: Nodes with unlimited children.

                        Jeff Relf wrote:[color=blue]
                        >
                        > You wrote,
                        > " BTW: I consider your formatting style horrible. "
                        >
                        > Everyone employs whitespace differently,
                        > I even change my own style from time to time.
                        >[/color]

                        It's not only about whitespace.

                        Eg.

                        // 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 ; };

                        Why do you need the comment? To describe what B, E and Room
                        are for. So then why not write it more verbose in the
                        first place:

                        struct Lnk_T { Link Beginn, End, Room ; };

                        Why everything on one line?

                        struct Lnk_T
                        {
                        Link Begin;
                        Link End;
                        Link Room;
                        };

                        Same for the code. Your code looks like one of the early
                        C programmers who thought that cramming as much as they can
                        into a single line is a good idea. It is not! Everybody
                        programming for years and having to maintain old code knows
                        this. One of the most important things in a code (besides
                        beeing correct) is maintainability .

                        Compare

                        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
                        }

                        To

                        void GrowList ( Lnk_T & Lnk )
                        {
                        if ( Lnk.E + 1 < Lnk.Room )
                        return;

                        int Room = Lnk.Room - Lnk.B + Chunk_Lnk;
                        int 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 );
                        }

                        BTW what you do with pointers and int in the above is illegal.
                        I wonder why your compiler didn't barf on it.


                        __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
                        }

                        In your code I didn't see where GrowList end and WinMain starts.
                        It took me reformatting to notice that I accidently copied not
                        only one function but two. And I didn't notice until I had the
                        whole thing in the newreader.
                        [color=blue]
                        > So I don't see how it's worth commenting on.[/color]

                        It is. Because at least in this group (clc++) we are used to
                        see problem request which can be traced back to simply bad
                        formatting. Reformatting and reindenting shows the error
                        quite clearly. So the regulars do this, reply with the
                        reformatted code and the OP sees his problem on his own.

                        Your code is the worst case I have seen for a long time.

                        Just because I notice it right now. You are also posting to
                        comp.os.linux.a dvocacy. Is this intended? Your code contains
                        __stdcall WinMain( ... )
                        which is clearly not a linux thing.

                        --
                        Karl Heinz Buchegger
                        kbuchegg@gascad .at

                        Comment

                        • Karl Heinz Buchegger

                          #13
                          Re: Nodes with unlimited children.

                          Karl Heinz Buchegger wrote:[color=blue]
                          >
                          >
                          > BTW what you do with pointers and int in the above is illegal.
                          > I wonder why your compiler didn't barf on it.[/color]

                          Forget that. It is legal.
                          My fault.

                          --
                          Karl Heinz Buchegger
                          kbuchegg@gascad .at

                          Comment

                          • Jeff Relf

                            #14
                            Re: Nodes with unlimited children.

                            Hi Karl Heinz Buchegger,

                            You wrote, << ...in this group ( clc++ )
                            we are used to seeing problems/requests
                            which can be traced back to simply bad formatting.
                            Reformatting and reindenting shows the errors quite clearly.

                            So the regulars do this,
                            reply with the reformatted code
                            and the OP sees his problem on his own. >>

                            I'm of the opinion that one's choice of whitespace
                            and the names one chooses for the identifiers
                            is less important than just going over the code.

                            i.e. It helps to simply change the whitespace,
                            no matter how you change it.

                            For that very reason, I often change my style.

                            Besides, my original question was answered quickly by you
                            after you examined the first few lines of my post.
                            ( And for that I thank you )

                            What style of code is more maintainable ?

                            I can't answer that question.

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

                            You suggested that
                            I would not need so many comment lines if I wrote it as:

                            struct Lnk_T
                            {
                            Link Begin;
                            Link End;
                            Link Room;
                            };

                            It's ironic that you would rename those 3 variables,
                            because after examining this code myself
                            I've decided to change them to: B, P, and E.

                            I much prefer the shorter names for often used pointers.

                            B is what I usually call the beginning of a buffer,
                            and E what I call the end of a buffer,
                            and P is what I call my working pointer.

                            So long as the scope is kept small, it works quite well.

                            Another change I made was to pre-increment P inside Inc(),
                            ( the function that makes room for it )
                            so now P always points to the current Node.

                            Pre-incrementing is always my preference.

                            Using your preferred whitespace style ( kind of ),
                            my code now looks like this:

                            struct NodeT ;

                            typedef NodeT * Link ;

                            // B is the Beginning of an array of NodeT's.
                            // P is the Pointer to the NodeT in use.
                            // E is the End of an array NodeT's.
                            // Inc() increments P.

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

                            struct NodeT
                            {
                            int Whatever ;
                            Lnk_T Lnk ;
                            };

                            Lnk_T Lnk ; // A global, so it's already initialized.

                            enum {
                            Sz_Node = sizeof NodeT
                            };

                            NodeT & Inc ( Lnk_T & Ln )
                            {
                            if ( Ln.E && ++ Ln.P < Ln.E )
                            return * Ln.P ;

                            int E = Ln.E - Ln.B + 4 ;
                            int P = Ln.P - Ln.B ;

                            Ln.B = ( Link ) realloc( Ln.B, E * Sz_Node );
                            Ln.E = Ln.B + E ;
                            Ln.P = Ln.B + P ;

                            memset( Ln.P, 0, ( Ln.E - Ln.P ) * Sz_Node );

                            return * Ln.P ;
                            }

                            // The - 1 below is so that I can pre-increment.

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

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

                            // In the line below, the first call to Inc()
                            // creates one child for Lnk ( Lnk is a global )
                            // and returns it as a reference to Lnk.P
                            //
                            // That child is then is passed in a second call to InC(),
                            // thus creating a grandchild
                            // and returning a reference to Lnk.P->Lnk.P.
                            //
                            // There is no limit to the number of children per node.
                            //
                            // Afterwards Lnk.P->Lnk.P->Whatever
                            // is valid and is initialized to zero.

                            printf( "%d", Inc ( Inc ( Lnk ).Lnk )->Whatever );

                            // This prints all of Lnk's children.

                            LoopChildren printf( "%d", _Lnk->Whatever );

                            }


                            P.S.

                            I cross-posted to Comp.OS.Linux.A dvocacy ( Cola )
                            because I'm only reading that group, not Comp.Lang.C++.

                            And yes, as my WinMain() hinted at,
                            I only program for Win XP.

                            I'm only in Cola because I have some friends there.


                            Comment

                            • Jeff Relf

                              #15
                              Re: Nodes with unlimited children.

                              Oops, two errors,

                              It's printf( "%d", Inc ( Inc ( Lnk ).Lnk ).Whatever );
                              not printf( "%d", Inc ( Inc ( Lnk ).Lnk )->Whatever );

                              Because the each call to Inc()
                              returns it as a reference to * Lnk.P, not Lnk.P


                              Comment

                              Working...