COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/bin_heap_demo.cc @ 675:38755a4d4b51

Last change on this file since 675:38755a4d4b51 was 618:e944d741f472, checked in by Mihaly Barasz, 21 years ago

#include <hugo/...>

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