1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/**
* \file main.cpp
* \brief Example program to show the use of the the claw::avl class.
*
* The program take avl values from a file and do some operations.
*
* \author Julien Jorge
*/
#include <iostream>
#include <fstream>
#include <claw/avl.hpp>
/**
* \brief This class allows us to test the claw::avl class with a non- type.
*/
class some_class
{
public:
int value;
public:
bool operator==(const some_class& that) const { return value == that.value; }
bool operator<(const some_class& that) const { return value < that.value; }
friend std::ostream& operator<<(std::ostream& os, const some_class& o)
{
return os << o.value;
}
}; // class some_class
void instanciate_methods( const claw::avl<some_class>& tree )
{
claw::avl<some_class> cpy(tree.begin(), tree.end());
std::cout << (tree == cpy) << std::endl;
std::cout << (tree != cpy) << std::endl;
std::cout << (tree <= cpy) << std::endl;
std::cout << (tree >= cpy) << std::endl;
std::cout << (tree < cpy) << std::endl;
std::cout << (tree > cpy) << std::endl;
claw::avl<some_class>::const_iterator it;
for (it=tree.begin(); it!=tree.end(); ++it)
std::cout << *it << ' ';
std::cout << std::endl;
for (it=cpy.begin(); it!=cpy.end(); ++it)
std::cout << *it << ' ';
std::cout << std::endl;
it = tree.find_nearest_greater( *tree.lower_bound() );
std::cout << *it << std::endl;
it = tree.find_nearest_lower( *tree.upper_bound() );
std::cout << *it << std::endl;
cpy.insert( tree.begin(), tree.end() );
} // instanciate_methods()
int main( int argc, char* argv[] )
{
if (argc != 2)
{
std::cout << argv[0] << " test_file" << std::endl;
return 1;
}
else
{
claw::avl<some_class> tree;
claw::avl<some_class>::const_iterator it;
std::ifstream f( argv[1] );
if (!f)
{
std::cerr << "Can't open file " << argv[1] << std::endl;
return 1;
}
else
{
int i,j;
some_class o;
if ( tree.empty() )
std::cout << "Tree is empty" << std::endl;
else
std::cout << "Tree is not empty but should !" << std::endl;
std::cout << "File loading..." << std::endl;
while ( f >> o.value )
tree.insert( o );
f.close();
std::cout << "size : " << tree.size() << std::endl;
instanciate_methods(tree);
it = tree.lower_bound();
if ( it != tree.end() )
std::cout << "Min = " << *it << std::endl;
it = tree.upper_bound();
if ( it != tree.end() )
std::cout << "Max = " << *it << std::endl;
if ( tree.empty() )
std::cout << "Tree is empty but shouldn't !" << std::endl;
else
std::cout << "Tree's not empty" << std::endl;
std::cout << "Found :";
for( o.value=0; o.value!=100; ++o.value )
if ( tree.find(o) != tree.end() )
std::cout << " " << o;
std::cout << std::endl;;
std::cout << "Scan with an iterator :";
for (it = tree.begin(); it != tree.end(); ++it )
std::cout << " " << *it;
std::cout<< std::endl;
std::cout << "Scan bis, iterator affectation :";
for (it = tree.begin(); it != tree.end(); ++it )
{
claw::avl<some_class>::const_iterator it_bis = it;
std::cout << *it << ":";
for (; it_bis != tree.end(); ++it_bis )
std::cout << " " << *it_bis;
std::cout<< std::endl;
}
std::cout << "Deletions" << std::endl;
// Try to be a few random
for( i=0; i!=10; ++i )
for ( j=0; j!=100; ++j )
{
o.value = i + j*10;
tree.erase(o);
}
std::cout << "Copy and scan" << std::endl;
std::cout << "source size : " << tree.size() << std::endl;
claw::avl<some_class> avl_2(tree);
std::cout << "copy size : " << tree.size() << std::endl;
if ( tree.empty() )
std::cout << "Tree's copy is empty" << std::endl;
else
std::cout << "Tree's copy is not empty" << std::endl;
for (it = avl_2.begin(); it != avl_2.end(); ++it )
std::cout << " " << *it;
std::cout << std::endl;
std::cout << "Reverse scan" << std::endl;
it = avl_2.end();
while( it != avl_2.begin() )
{
--it;
std::cout << " " << *it;
}
std::cout << std::endl;
std::cout << "Assignment" << std::endl;
avl_2 = tree;
std::cout << "Clear" << std::endl;
avl_2.clear();
if ( avl_2.empty() )
std::cout << "Tree's is empty" << std::endl;
else
std::cout << "Tree's is not empty but should !" << std::endl;
return 0;
}
}
}
|