Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

node_edge_swap.cpp


SourceForge.net Logo     CTTL on    
    SourceForge    
    Download    
    Latest    
    Documentation    
    Index    
    Library    
    News    
    CVS    
    Repository    
   Other    
   Links    

Demonstrates exchange of the node identities when two nodes are swapped by the std::swap() algorithm. In a similar way, identities of edge boundary nodes are exchanged when two edge objects are swapped.

// sample program: node_edge_swap.cpp
// demonstrates concept of offset hooks

//#define NDEBUG    // define to stop assertions from being compiled 
#include <cassert>
#include <iostream>
#include "cttl/cttl.h"

int main()
{
    using namespace cttl;

    // create input object with 6 offset hooks
    input<> inp( "hello, world!", 4 );

    // create nodes
    node<> n0( inp, 0 );
    node<> n1( inp, 1 );

    n0.go_bof();
    n1.go_eof();

    std::cout << "NODES BEFORE SWAP:" << std::endl;
    std::cout << "\t      n0 id=" << n0.identity() << " offset=" << n0.offset() << std::endl;
    std::cout << "\t      n1 id=" << n1.identity() << " offset=" << n1.offset() << std::endl;
    std::swap( n0, n1 );
    std::cout << "AFTER\t\t\t\t\tstd::swap( n0, n1 ):" << std::endl;
    std::cout << "\t      n0 id=" << n0.identity() << " offset=" << n0.offset() << std::endl;
    std::cout << "\t      n1 id=" << n1.identity() << " offset=" << n1.offset() << std::endl;
    std::cout << std::endl;

    // create edges
    const_edge<> e01( inp, 0, 1 );
    const_edge<> e10( inp, 1, 0 );

    std::cout << "EDGES BEFORE SWAP:" << std::endl;
    std::cout << "\t e01.1st id=" << e01.first.identity() << " offset=" << e01.first.offset() << std::endl;
    std::cout << "\t e01.2nd id=" << e01.second.identity() << " offset=" << e01.second.offset() << std::endl;
    std::cout << "\t e10.1st id=" << e10.first.identity() << " offset=" << e10.first.offset() << std::endl;
    std::cout << "\t e10.2nd id=" << e10.second.identity() << " offset=" << e10.second.offset() << std::endl;
    std::swap( e01, e10 );
    std::cout << "AFTER\t\t\t\t\tstd::swap( e01, e10 ):" << std::endl;
    std::cout << "\t e01.1st id=" << e01.first.identity() << " offset=" << e01.first.offset() << std::endl;
    std::cout << "\t e01.2nd id=" << e01.second.identity() << " offset=" << e01.second.offset() << std::endl;
    std::cout << "\t e10.1st id=" << e10.first.identity() << " offset=" << e10.first.offset() << std::endl;
    std::cout << "\t e10.2nd id=" << e10.second.identity() << " offset=" << e10.second.offset() << std::endl;
    std::cout << std::endl;

    std::cout << "NODE BEFORE ASSIGNMENT:" << std::endl;
    std::cout << "\t      n0 id=" << n0.identity() << " offset=" << n0.offset() << std::endl;
    std::cout << "\t      n1 id=" << n1.identity() << " offset=" << n1.offset() << std::endl;
    n0 = n1;
    std::cout << "AFTER\t\t\t\t\tn0 = n1:" << std::endl;
    std::cout << "\t      n0 id=" << n0.identity() << " offset=" << n0.offset() << std::endl;
    std::cout << "\t      n1 id=" << n1.identity() << " offset=" << n1.offset() << std::endl;
    std::cout << std::endl;

    std::cout << "EDGES BEFORE ASSIGNMENT:" << std::endl;
    std::cout << "\t e01.1st id=" << e01.first.identity() << " offset=" << e01.first.offset() << std::endl;
    std::cout << "\t e01.2nd id=" << e01.second.identity() << " offset=" << e01.second.offset() << std::endl;
    std::cout << "\t e10.1st id=" << e10.first.identity() << " offset=" << e10.first.offset() << std::endl;
    std::cout << "\t e10.2nd id=" << e10.second.identity() << " offset=" << e10.second.offset() << std::endl;
    e01 = e10;
    std::cout << "AFTER\t\t\t\t\te01 = e10:" << std::endl;
    std::cout << "\t e01.1st id=" << e01.first.identity() << " offset=" << e01.first.offset() << std::endl;
    std::cout << "\t e01.2nd id=" << e01.second.identity() << " offset=" << e01.second.offset() << std::endl;
    std::cout << "\t e10.1st id=" << e10.first.identity() << " offset=" << e10.first.offset() << std::endl;
    std::cout << "\t e10.2nd id=" << e10.second.identity() << " offset=" << e10.second.offset() << std::endl;
    std::cout << std::endl;

    return 0;
}
This program displays the following output:
NODES BEFORE SWAP:
          n0 id=0 offset=0
          n1 id=1 offset=13
AFTER                   std::swap( n0, n1 ):
          n0 id=1 offset=13
          n1 id=0 offset=0

EDGES BEFORE SWAP:
     e01.1st id=0 offset=0
     e01.2nd id=1 offset=13
     e10.1st id=1 offset=13
     e10.2nd id=0 offset=0
AFTER                   std::swap( e01, e10 ):
     e01.1st id=1 offset=13
     e01.2nd id=0 offset=0
     e10.1st id=0 offset=0
     e10.2nd id=1 offset=13

NODE BEFORE ASSIGNMENT:
          n0 id=1 offset=13
          n1 id=0 offset=0
AFTER                   n0 = n1:
          n0 id=0 offset=0
          n1 id=0 offset=0

EDGES BEFORE ASSIGNMENT:
     e01.1st id=1 offset=13
     e01.2nd id=0 offset=0
     e10.1st id=0 offset=0
     e10.2nd id=1 offset=13
AFTER                   e01 = e10:
     e01.1st id=0 offset=0
     e01.2nd id=1 offset=13
     e10.1st id=0 offset=0
     e10.2nd id=1 offset=13



Copyright © 1997-2006 Igor Kholodov mailto:cttl@users.sourceforge.net.

Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.


Generated on Thu Nov 2 17:44:55 2006 for Common Text Transformation Library by  doxygen 1.3.9.1