src/work/bin_heap_demo.cc
author marci
Mon, 16 Feb 2004 11:38:19 +0000
changeset 76 d9650659a6ee
parent 37 e0e41f9e2be5
child 105 a3c73e9b9b2e
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
klao@37
     6
using namespace marci;
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@37
    28
  void put(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@37
    48
  cout << "heap.put(\"korte\", 3.4);\n";
klao@37
    49
  heap.put("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@37
    57
  cout << "heap.topValue() = "
klao@37
    58
       << heap.topValue() << endl;
klao@37
    59
klao@37
    60
  cout << "heap.decrease(\"alma\", 1.2);\n";
klao@37
    61
  heap.put("alma", 1.2);
klao@37
    62
klao@37
    63
  cout << "heap.top() = "
klao@37
    64
       << heap.top() << endl;
klao@37
    65
  cout << "heap.topValue() = "
klao@37
    66
       << heap.topValue() << endl;
klao@37
    67
klao@37
    68
  cout << "heap.put(\"alma\", 22);\n";
klao@37
    69
  heap.put("alma", 22);
klao@37
    70
klao@37
    71
  cout << "heap.top() = "
klao@37
    72
       << heap.top() << endl;
klao@37
    73
  cout << "heap.topValue() = "
klao@37
    74
       << heap.topValue() << 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@37
    83
  cout << "heap.topValue() = "
klao@37
    84
       << heap.topValue() << endl;
klao@37
    85
klao@39
    86
  cout << "heap.state(\"szilva\") = "
klao@39
    87
       << heap.state("szilva") << endl;
klao@39
    88
  cout << "heap.put(\"szilva\", 0.5);\n";
klao@39
    89
  heap.put("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
}