test/heap_test.h
author alpar
Fri, 03 Feb 2006 09:18:17 +0000
changeset 1949 5db4ff8d69de
parent 1745 d356e54bdafc
child 1956 a055123339d5
permissions -rw-r--r--
Fight with Doxygen.
Victory hasn't been reached yet, but it's on the horizon.
deba@1728
     1
// -*- c++ -*-
deba@1206
     2
deba@1187
     3
#include <vector>
deba@1187
     4
#include <algorithm>
deba@1187
     5
deba@1187
     6
#include <lemon/dijkstra.h>
deba@1187
     7
deba@1187
     8
class IntIntMap : public std::vector<int> {
deba@1187
     9
public:
deba@1187
    10
  typedef std::vector<int> Parent;
deba@1187
    11
deba@1187
    12
  IntIntMap() : Parent() {}
deba@1187
    13
  IntIntMap(int n) : Parent(n) {}
deba@1187
    14
  IntIntMap(int n, int v) : Parent(n, v) {}
deba@1187
    15
deba@1187
    16
  void set(int key, int value) {
deba@1187
    17
    Parent::operator[](key) = value;
deba@1187
    18
  }
deba@1187
    19
};
deba@1187
    20
deba@1187
    21
deba@1187
    22
template <typename _Heap>
deba@1187
    23
void heapSortTest(int n) {
deba@1187
    24
  typedef _Heap Heap;
deba@1187
    25
  IntIntMap map(n, -1);
deba@1187
    26
deba@1187
    27
  Heap heap(map);
deba@1187
    28
  
deba@1187
    29
  std::vector<int> v(n);
deba@1187
    30
deba@1187
    31
  for (int i = 0; i < n; ++i) {
deba@1187
    32
    v[i] = rand() % 1000;
deba@1187
    33
    heap.push(i, v[i]);
deba@1187
    34
  }
deba@1187
    35
  std::sort(v.begin(), v.end());
deba@1187
    36
  for (int i = 0; i < n; ++i) {
deba@1187
    37
    check(v[i] == heap.prio() ,"Wrong order in heap sort.");
deba@1187
    38
    heap.pop();
deba@1187
    39
  }
deba@1187
    40
}
deba@1187
    41
deba@1206
    42
template <typename _Heap>
deba@1206
    43
void heapIncreaseTest(int n) {
deba@1206
    44
  typedef _Heap Heap;
deba@1206
    45
  IntIntMap map(n, -1);
deba@1206
    46
deba@1206
    47
  Heap heap(map);
deba@1206
    48
  
deba@1206
    49
  std::vector<int> v(n);
deba@1206
    50
deba@1206
    51
  for (int i = 0; i < n; ++i) {
deba@1206
    52
    v[i] = rand() % 1000;
deba@1206
    53
    heap.push(i, v[i]);
deba@1206
    54
  }
deba@1206
    55
  for (int i = 0; i < n; ++i) {
deba@1206
    56
    v[i] += rand() % 1000;
deba@1206
    57
    heap.increase(i, v[i]);
deba@1206
    58
  }
deba@1206
    59
  std::sort(v.begin(), v.end());
deba@1206
    60
  for (int i = 0; i < n; ++i) {
deba@1206
    61
    check(v[i] == heap.prio() ,"Wrong order in heap increase test.");
deba@1206
    62
    heap.pop();
deba@1206
    63
  }
deba@1206
    64
}
deba@1206
    65
deba@1206
    66
deba@1206
    67
deba@1187
    68
template <typename _Graph, typename _LengthMap, typename _Heap>
deba@1187
    69
void dijkstraHeapTest(_Graph& graph, _LengthMap& length,
deba@1187
    70
		      typename _Graph::Node& start) {
deba@1187
    71
deba@1187
    72
  typedef _Heap Heap;
deba@1187
    73
  typedef _Graph Graph;
deba@1187
    74
  typedef _LengthMap LengthMap;
deba@1187
    75
deba@1187
    76
  typedef typename Graph::Node Node;
deba@1187
    77
  typedef typename Graph::Edge Edge;
deba@1187
    78
  typedef typename Graph::NodeIt NodeIt;
deba@1187
    79
  typedef typename Graph::EdgeIt EdgeIt;
deba@1187
    80
deba@1745
    81
  typename Dijkstra<Graph, LengthMap>::template DefStandardHeap<Heap>::
deba@1728
    82
    Create dijkstra(graph, length);
deba@1187
    83
deba@1187
    84
  dijkstra.run(start);
deba@1187
    85
deba@1187
    86
  for(EdgeIt e(graph); e!=INVALID; ++e) {
deba@1187
    87
    Node u=graph.source(e); 
deba@1187
    88
    Node v=graph.target(e);
deba@1187
    89
    if (dijkstra.reached(u)) {
deba@1187
    90
      check( dijkstra.dist(v) - dijkstra.dist(u) <= length[e],
deba@1728
    91
      	     "Error in a shortest path tree edge!");
deba@1187
    92
    }
deba@1187
    93
  }
deba@1187
    94
deba@1187
    95
  for(NodeIt v(graph); v!=INVALID; ++v) {
deba@1763
    96
    if ( dijkstra.reached(v) && dijkstra.predEdge(v) != INVALID ) {
deba@1763
    97
      Edge e=dijkstra.predEdge(v);
deba@1187
    98
      Node u=graph.source(e);
deba@1187
    99
      check( dijkstra.dist(v) - dijkstra .dist(u) == length[e],
deba@1187
   100
	     "Error in a shortest path tree edge!");
deba@1187
   101
    }
deba@1187
   102
  }
deba@1187
   103
deba@1187
   104
}