src/work/bin_heap_demo.cc
author marci
Wed, 04 Feb 2004 12:46:33 +0000
changeset 59 41c7f9c09a12
parent 37 e0e41f9e2be5
child 105 a3c73e9b9b2e
permissions -rw-r--r--
.
     1 #include <iostream>
     2 #include <bin_heap.hh>
     3 #include <string>
     4 #include <map>
     5 
     6 using namespace marci;
     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 put(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.put(\"korte\", 3.4);\n";
    49   heap.put("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.topValue() = "
    58        << heap.topValue() << endl;
    59 
    60   cout << "heap.decrease(\"alma\", 1.2);\n";
    61   heap.put("alma", 1.2);
    62 
    63   cout << "heap.top() = "
    64        << heap.top() << endl;
    65   cout << "heap.topValue() = "
    66        << heap.topValue() << endl;
    67 
    68   cout << "heap.put(\"alma\", 22);\n";
    69   heap.put("alma", 22);
    70 
    71   cout << "heap.top() = "
    72        << heap.top() << endl;
    73   cout << "heap.topValue() = "
    74        << heap.topValue() << 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.topValue() = "
    84        << heap.topValue() << endl;
    85 
    86   cout << "heap.state(\"szilva\") = "
    87        << heap.state("szilva") << endl;
    88   cout << "heap.put(\"szilva\", 0.5);\n";
    89   heap.put("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 }