src/work/bin_heap_demo.cc
author marci
Fri, 12 Mar 2004 16:37:08 +0000
changeset 179 91646df36ffc
parent 105 a3c73e9b9b2e
child 214 44f01e580f16
permissions -rw-r--r--
const
     1 #include <iostream>
     2 #include <bin_heap.hh>
     3 #include <string>
     4 #include <map>
     5 
     6 using namespace hugo;
     7 using namespace std;
     8 
     9 class string_int_map;
    10 
    11 // Egy binaris kupac, ami stringekhez rendelt double ertekeket tarol,
    12 // azaz mindig az a string van a tetejen, amihez a legkisebb szam tartozik.
    13 // A kupac egy string_int_map tipusu property_map segitsegevel tarolja
    14 // a stringek aktualis helyet sajatmagan belul.
    15 // Egy olyan stringhez, ami meg nincsen a kupac -1 -et kell rendelnunk.
    16 typedef BinHeap<string, double, string_int_map> StrDoubleHeap;
    17 
    18 class string_int_map : public map<string,int> {
    19 public:
    20   int get(const string &s) {
    21     // Bocs, ez igy gaaaany, de nem volt kedvem utananezni, hogy
    22     // hogy is mukodik ez a map :)
    23     if( count(s) == 0 ) {
    24       operator[](s) = StrDoubleHeap::PRE_HEAP;
    25     }
    26     return operator[](s);
    27   }
    28   void set(const string &s, int i) {
    29       operator[](s) = i;
    30   }
    31 };
    32 
    33 
    34 int main()
    35 {
    36   string_int_map sim;
    37   
    38   
    39   cout << "testing string_int_map default value:\n";
    40   cout << "  alma: " << sim.get("alma") << endl;
    41 
    42   cout << "creating the heap\n";
    43   StrDoubleHeap heap(sim);
    44 
    45   cout << "heap.push(\"alma\", 15);\n";
    46   heap.push("alma", 15);
    47 
    48   cout << "heap.set(\"korte\", 3.4);\n";
    49   heap.set("korte", 3.4);
    50 
    51   cout << "heap.get(\"alma\") = " 
    52        << heap.get("alma")
    53        << endl;
    54 
    55   cout << "heap.top() = "
    56        << heap.top() << endl;
    57   cout << "heap.topPrio() = "
    58        << heap.topPrio() << endl;
    59 
    60   cout << "heap.decrease(\"alma\", 1.2);\n";
    61   heap.set("alma", 1.2);
    62 
    63   cout << "heap.top() = "
    64        << heap.top() << endl;
    65   cout << "heap.topPrio() = "
    66        << heap.topPrio() << endl;
    67 
    68   cout << "heap.set(\"alma\", 22);\n";
    69   heap.set("alma", 22);
    70 
    71   cout << "heap.top() = "
    72        << heap.top() << endl;
    73   cout << "heap.topPrio() = "
    74        << heap.topPrio() << endl;
    75 
    76   cout << "heap.size() = "
    77        << heap.size() << endl;
    78   cout << "heap.pop();\n";
    79   heap.pop();
    80 
    81   cout << "heap.top() = "
    82        << heap.top() << endl;
    83   cout << "heap.topPrio() = "
    84        << heap.topPrio() << endl;
    85 
    86   cout << "heap.state(\"szilva\") = "
    87        << heap.state("szilva") << endl;
    88   cout << "heap.set(\"szilva\", 0.5);\n";
    89   heap.set("szilva", 0.5);
    90   cout << "heap.state(\"szilva\") = "
    91        << heap.state("szilva") << endl;
    92   cout << "heap.top() = "
    93        << heap.top() << endl;
    94   cout << "heap.pop();\n";
    95   heap.pop();
    96   cout << "heap.state(\"szilva\") = "
    97        << heap.state("szilva") << endl;
    98 
    99   cout << "heap.size() = "
   100        << heap.size() << endl;
   101   cout << "heap.pop();\n";
   102   heap.pop();
   103 
   104   cout << "heap.size() = "
   105        << heap.size() << endl;  
   106   cout << "heap.empty() = "
   107        << (heap.empty()?"true":"false") << endl;  
   108 }