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