<commit_before>/*=========================================================================

  Program:   itkUNC
  Module:    itkTreeContainerTest2.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 CADDLab @ UNC. All rights reserved.
  See itkUNCCopyright.txt for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include <fstream>
#include <itkCommand.h> 
#include "itkMacro.h"
#include <itkObject.h>
#include <iostream>
#include <itkObjectFactory.h>
#include <itkSmartPointer.h>


/**
 * This is a test file for the itkTreeContainer class.
 */

#include "itkTreeContainer.h"
#include "itkChildTreeIterator.h"
#include "itkLeafTreeIterator.h"
#include "itkLevelOrderTreeIterator.h"
#include "itkInOrderTreeIterator.h"
#include "itkPostOrderTreeIterator.h"
#include "itkPreOrderTreeIterator.h"
#include "itkRootTreeIterator.h"

class TestData {
    int value;
    static int counter;

public:
    TestData() {
        counter++;
    }

    TestData(const int value) {
        this->value = value;
        counter++;
    };

    TestData(const TestData& t) {
        this->value = t.value;
        counter++;
    };

    ~TestData() {
        value = 0;
        counter--;

        if(counter == 0)
            std::cout << "last Data destroyed" << std::endl;
    };

    int Get() const {
        return value;
    };

    void Set( int v ) {
        value = v;
    };


    TestData &operator=(const TestData& t) {
        value = t.value;
        std::cout << "assignation: " << value << std::endl;
        return *this;
    }

    bool operator==(const TestData& t) {

        if( &t == this )
            return true;
        return false;
    };

    friend std::ostream& operator<<( std::ostream& o, TestData& t);
    friend std::istream& operator>>( std::istream& i, TestData& t);
};

int TestData::counter = 0;

/**
 *
 */
std::ostream& operator<<( std::ostream& o, TestData& t) {

    return o << "( TestData Nr. " << t.Get() << " )";
}

/**
 *
 */
std::istream& operator>>( std::istream& i, TestData& t ) {

    char s[10];
    int value;

    i >> s; // {
    i >> s; // TestData
    i >> s; // Nr.
    i >> s; // }
    i >> value;

    t.Set( value );
    return i;
}


typedef TestData* NodeType;
std::list<TestData*> InternalList;// used to delete the pointer in the tree
typedef itk::TreeContainer<NodeType> TreeType;

class TreeChangeListener : public itk::Command {

protected:
  TreeChangeListener(){};
public:

  typedef TreeChangeListener Self;
  typedef itk::SmartPointer<Self>  Pointer;
  typedef itk::SmartPointer<const Self>  ConstPointer;
  itkNewMacro(Self);
  
  /**
   *
   */
  void  Execute (itk::Object* caller, const itk::EventObject& event ) {

       std::cout << event.GetEventName() << std::endl;
  }

  /**
   *
   */
  virtual void  Execute (const itk::Object *caller, const itk::EventObject &event) {
  
    std::cout << event.GetEventName();

    const itk::TreeChangeEvent<TreeType>* e = dynamic_cast<const itk::TreeChangeEvent<TreeType>*>(&event);

    if ( e )
      std::cout << *e->GetChangePosition().Get()  << std::endl;
  }
};

/*
    CreateTree_1()

                                                    [1]

                                                 *   *   *
                                              *    *   *     *
                                          *      *       *       *
                                        [11]    [12]    [13]    [14]
                                      * * *      * * *
                                  *    *  *      *  *    *
                               *     *    *      *    *     *
                            [111] [112] [113]  [121] [122] [123]
                                                             *
                                                             *
                                                             *
                                                            [9]
                                                            * *
                                                           *   *
                                                          *     *
                                                         [91]  [92]
                                                        * * *
                                                      *   *   *
                                                    *     *     *
                                                 [991]  [912]  [913]
 */
TreeType::Pointer CreateTree_1()
{
  std::cout << "create tree" << std::endl;

  TreeType::Pointer tree = TreeType::New();

  
  TreeChangeListener::Pointer treeChangeListener = TreeChangeListener::New();
  itk::TreeAddEvent<TreeType> ev;
  unsigned long tag = tree->AddObserver( ev, treeChangeListener );
  

  itk::PostOrderTreeIterator<TreeType> it( tree );

  TestData* newNode = new TestData(1);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(11);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(12);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(13);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(14);
  InternalList.push_back(newNode);
  it.Add(newNode);
  it.GoToChild(0);
  newNode = new TestData(111);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(112);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(113);
  InternalList.push_back(newNode);
  it.Add(newNode);
  it.GoToParent();
  it.GoToChild(1);
  newNode = new TestData(121);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(122);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(123);
  InternalList.push_back(newNode);
  it.Add(newNode);
  tree->RemoveObserver( tag );
  
  std::cout << "end create tree" << std::endl;
  return tree;
}

/*
    CreateTree_2()
                                                    
                                                             *
                                                             *
                                                             *
                                                            [9]
                                                            * *
                                                           *   *
                                                          *     *
                                                         [91]  [92]
                                                        * * *
                                                      *   *   *
                                                    *     *     *
                                                 [991]  [912]  [913]
 */
TreeType::Pointer CreateTree_2()
{
  std::cout << "create tree 2" << std::endl;

  TreeType::Pointer tree = TreeType::New(); 
  tree->SetRoot( new TestData( 9 ) );
  itk::PostOrderTreeIterator<TreeType> it( tree );


  TestData* newNode = new TestData(91);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(92);
  InternalList.push_back(newNode);
  it.Add(newNode);
  it.GoToChild( 0 );
  newNode = new TestData(991);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(912);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(913);
  InternalList.push_back(newNode);
  it.Add(newNode);

  std::cout << "end create tree 2" << std::endl;
  return tree;
}


/**<commit_msg>FIX: MLK<commit_after>/*=========================================================================

  Program:   itkUNC
  Module:    itkTreeContainerTest2.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 CADDLab @ UNC. All rights reserved.
  See itkUNCCopyright.txt for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include <fstream>
#include <itkCommand.h> 
#include "itkMacro.h"
#include <itkObject.h>
#include <iostream>
#include <itkObjectFactory.h>
#include <itkSmartPointer.h>


/**
 * This is a test file for the itkTreeContainer class.
 */

#include "itkTreeContainer.h"
#include "itkChildTreeIterator.h"
#include "itkLeafTreeIterator.h"
#include "itkLevelOrderTreeIterator.h"
#include "itkInOrderTreeIterator.h"
#include "itkPostOrderTreeIterator.h"
#include "itkPreOrderTreeIterator.h"
#include "itkRootTreeIterator.h"

class TestData {
    int value;
    static int counter;

public:
    TestData() {
        counter++;
    }

    TestData(const int value) {
        this->value = value;
        counter++;
    };

    TestData(const TestData& t) {
        this->value = t.value;
        counter++;
    };

    ~TestData() {
        value = 0;
        counter--;

        if(counter == 0)
            std::cout << "last Data destroyed" << std::endl;
    };

    int Get() const {
        return value;
    };

    void Set( int v ) {
        value = v;
    };


    TestData &operator=(const TestData& t) {
        value = t.value;
        std::cout << "assignation: " << value << std::endl;
        return *this;
    }

    bool operator==(const TestData& t) {

        if( &t == this )
            return true;
        return false;
    };

    friend std::ostream& operator<<( std::ostream& o, TestData& t);
    friend std::istream& operator>>( std::istream& i, TestData& t);
};

int TestData::counter = 0;

/**
 *
 */
std::ostream& operator<<( std::ostream& o, TestData& t) {

    return o << "( TestData Nr. " << t.Get() << " )";
}

/**
 *
 */
std::istream& operator>>( std::istream& i, TestData& t ) {

    char s[10];
    int value;

    i >> s; // {
    i >> s; // TestData
    i >> s; // Nr.
    i >> s; // }
    i >> value;

    t.Set( value );
    return i;
}


typedef TestData* NodeType;
std::list<TestData*> InternalList;// used to delete the pointer in the tree
typedef itk::TreeContainer<NodeType> TreeType;

class TreeChangeListener : public itk::Command {

protected:
  TreeChangeListener(){};
public:

  typedef TreeChangeListener Self;
  typedef itk::SmartPointer<Self>  Pointer;
  typedef itk::SmartPointer<const Self>  ConstPointer;
  itkNewMacro(Self);
  
  /**
   *
   */
  void  Execute (itk::Object* caller, const itk::EventObject& event ) {

       std::cout << event.GetEventName() << std::endl;
  }

  /**
   *
   */
  virtual void  Execute (const itk::Object *caller, const itk::EventObject &event) {
  
    std::cout << event.GetEventName();

    const itk::TreeChangeEvent<TreeType>* e = dynamic_cast<const itk::TreeChangeEvent<TreeType>*>(&event);

    if ( e )
      std::cout << *e->GetChangePosition().Get()  << std::endl;
  }
};

/*
    CreateTree_1()

                                                    [1]

                                                 *   *   *
                                              *    *   *     *
                                          *      *       *       *
                                        [11]    [12]    [13]    [14]
                                      * * *      * * *
                                  *    *  *      *  *    *
                               *     *    *      *    *     *
                            [111] [112] [113]  [121] [122] [123]
                                                             *
                                                             *
                                                             *
                                                            [9]
                                                            * *
                                                           *   *
                                                          *     *
                                                         [91]  [92]
                                                        * * *
                                                      *   *   *
                                                    *     *     *
                                                 [991]  [912]  [913]
 */
TreeType::Pointer CreateTree_1()
{
  std::cout << "create tree" << std::endl;

  TreeType::Pointer tree = TreeType::New();

  
  TreeChangeListener::Pointer treeChangeListener = TreeChangeListener::New();
  itk::TreeAddEvent<TreeType> ev;
  unsigned long tag = tree->AddObserver( ev, treeChangeListener );
  

  itk::PostOrderTreeIterator<TreeType> it( tree );

  TestData* newNode = new TestData(1);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(11);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(12);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(13);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(14);
  InternalList.push_back(newNode);
  it.Add(newNode);
  it.GoToChild(0);
  newNode = new TestData(111);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(112);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(113);
  InternalList.push_back(newNode);
  it.Add(newNode);
  it.GoToParent();
  it.GoToChild(1);
  newNode = new TestData(121);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(122);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(123);
  InternalList.push_back(newNode);
  it.Add(newNode);
  tree->RemoveObserver( tag );
  
  std::cout << "end create tree" << std::endl;
  return tree;
}

/*
    CreateTree_2()
                                                    
                                                             *
                                                             *
                                                             *
                                                            [9]
                                                            * *
                                                           *   *
                                                          *     *
                                                         [91]  [92]
                                                        * * *
                                                      *   *   *
                                                    *     *     *
                                                 [991]  [912]  [913]
 */
TreeType::Pointer CreateTree_2()
{
  std::cout << "create tree 2" << std::endl;

  TreeType::Pointer tree = TreeType::New();
  TestData* newNode = new TestData(9);
  InternalList.push_back(newNode);
  tree->SetRoot(newNode);
  itk::PostOrderTreeIterator<TreeType> it( tree );

  newNode = new TestData(91);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(92);
  InternalList.push_back(newNode);
  it.Add(newNode);
  it.GoToChild( 0 );
  newNode = new TestData(991);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(912);
  InternalList.push_back(newNode);
  it.Add(newNode);
  newNode = new TestData(913);
  InternalList.push_back(newNode);
  it.Add(newNode);

  std::cout << "end create tree 2" << std::endl;
  return tree;
}


/**
{'fraction_non_alphanumeric': 0.13714539569715048, 'fraction_numerical': 0.015104398045313194, 'mean_word_length': 1.4194687547980962, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 132, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7650268', 'n_tokens_mistral': 4526, 'n_tokens_neox': 4272, 'n_words': 906}