COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/bin_heap_demo.cc @ 261:796101caedb7

Last change on this file since 261:796101caedb7 was 258:94bafec4f56f, checked in by Mihaly Barasz, 20 years ago

bin_heap.hh atnevezese

File size: 2.9 KB
RevLine 
[37]1#include <iostream>
[258]2#include <bin_heap.h>
[37]3#include <string>
4#include <map>
5
[105]6using namespace hugo;
[37]7using namespace std;
8
9class string_int_map;
10
[39]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.
[37]16typedef BinHeap<string, double, string_int_map> StrDoubleHeap;
17
18class string_int_map : public map<string,int> {
[214]19  typedef map<string,int> parent;
[37]20public:
21  int get(const string &s) {
22    // Bocs, ez igy gaaaany, de nem volt kedvem utananezni, hogy
23    // hogy is mukodik ez a map :)
24    if( count(s) == 0 ) {
[214]25      parent::operator[](s) = StrDoubleHeap::PRE_HEAP;
[37]26    }
[214]27    return parent::operator[](s);
28  }
29  int operator[](const string &s) {
30    return get(s);
[37]31  }
[172]32  void set(const string &s, int i) {
[214]33      parent::operator[](s) = i;
[37]34  }
35};
36
37
38int main()
39{
40  string_int_map sim;
41 
42 
43  cout << "testing string_int_map default value:\n";
[214]44  cout << "  sim.get(\"alma\"): " << sim.get("alma") << endl;
45  cout << "  sim[\"alma\"]: " << sim["alma"] << endl;
[37]46
47  cout << "creating the heap\n";
48  StrDoubleHeap heap(sim);
49
50  cout << "heap.push(\"alma\", 15);\n";
51  heap.push("alma", 15);
52
[172]53  cout << "heap.set(\"korte\", 3.4);\n";
54  heap.set("korte", 3.4);
[37]55
56  cout << "heap.get(\"alma\") = "
57       << heap.get("alma")
58       << endl;
[214]59  cout << "heap[\"alma\"] = "
60       << heap["alma"]
61       << endl;
[37]62
63  cout << "heap.top() = "
64       << heap.top() << endl;
[172]65  cout << "heap.topPrio() = "
66       << heap.topPrio() << endl;
[37]67
68  cout << "heap.decrease(\"alma\", 1.2);\n";
[172]69  heap.set("alma", 1.2);
[37]70
71  cout << "heap.top() = "
72       << heap.top() << endl;
[172]73  cout << "heap.topPrio() = "
74       << heap.topPrio() << endl;
[37]75
[172]76  cout << "heap.set(\"alma\", 22);\n";
77  heap.set("alma", 22);
[37]78
79  cout << "heap.top() = "
80       << heap.top() << endl;
[172]81  cout << "heap.topPrio() = "
82       << heap.topPrio() << endl;
[37]83
84  cout << "heap.size() = "
85       << heap.size() << endl;
86  cout << "heap.pop();\n";
87  heap.pop();
88
89  cout << "heap.top() = "
90       << heap.top() << endl;
[172]91  cout << "heap.topPrio() = "
92       << heap.topPrio() << endl;
[37]93
[39]94  cout << "heap.state(\"szilva\") = "
95       << heap.state("szilva") << endl;
[172]96  cout << "heap.set(\"szilva\", 0.5);\n";
97  heap.set("szilva", 0.5);
[39]98  cout << "heap.state(\"szilva\") = "
99       << heap.state("szilva") << endl;
100  cout << "heap.top() = "
101       << heap.top() << endl;
102  cout << "heap.pop();\n";
103  heap.pop();
104  cout << "heap.state(\"szilva\") = "
105       << heap.state("szilva") << endl;
106
[37]107  cout << "heap.size() = "
108       << heap.size() << endl;
109  cout << "heap.pop();\n";
110  heap.pop();
111
112  cout << "heap.size() = "
113       << heap.size() << endl; 
114  cout << "heap.empty() = "
115       << (heap.empty()?"true":"false") << endl; 
116}
Note: See TracBrowser for help on using the repository browser.