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