Remove dynamic exceptions
[anna.git] / example / core / sortName / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <iostream>
10
11 #include <functional>
12
13 #include <anna/core/core.hpp>
14
15 using namespace std;
16
17 class Object {
18 public:
19    Object ();
20
21    const std::string& getName () const { return a_name; }
22
23 private:
24    std::string a_name;
25 };
26
27 struct SortByName {
28    static const std::string& value (const Object* oo) { return oo->getName (); }
29 };
30
31 string generate ()
32 {
33    string result;
34    char aux [34];
35
36    int len = 1 + rand () % 32;
37
38    int ii;
39    for (ii = 0; ii < len; ii ++)
40       aux [ii] = char (rand () % 28 + 'A');
41    aux [ii] = 0;
42
43    return result = aux;
44 }
45
46 Object::Object ()
47 {
48    a_name = generate ();
49 }
50
51 void run () 
52    noexcept(false)
53 {
54    typedef SortedVector <Object, SortByName, std::string> container;
55    typedef container::iterator iterator;
56    container sorted;
57
58    static const int MaxIndex = 64;
59
60    Object** object;
61
62    object = new Object* [MaxIndex];
63
64    for (int ii = 0; ii < MaxIndex; ii ++) {
65       object [ii] = new Object;
66       sorted.add (object [ii]);
67    }
68
69    for (iterator ii = sorted.begin (), maxii = sorted.end (); ii != maxii; ii ++) 
70        cout << container::data (ii)->getName () << endl;
71    cout << endl << endl;
72
73    string value;
74    int counter;
75    int found;
76
77    counter = found = 0;
78
79    for (int loop = 0; loop < 1000; loop ++) {
80       for (int ii = 0; ii < MaxIndex; ii ++, counter ++) {
81         value = ((rand () % 100) > 50) ? object [ii]->getName (): generate ();
82         if (sorted.find (value) != NULL)
83            found ++;
84       }
85    }
86
87    cout << "Counter: " << counter << " | Found: " << found << endl << endl;
88 }
89
90 int main (int argc, const char** argv)
91 {
92    try {
93       Logger::setLevel (Logger::Debug); 
94       Logger::initialize ("testfunctions", new TraceWriter ("file.trace", 2048000));
95   
96       run ();
97    }
98    catch (Exception& ex) {
99       cout << ex.asString () << endl;
100    }
101    
102    return 0;
103 }
104