Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RoutePlanning/HeuristicLab.Algorithms.GraphRouting/3.3/PriorityQueueOld.cs @ 8481

Last change on this file since 8481 was 8350, checked in by spimming, 12 years ago

#1894:

  • new implementation for priority queue
  • based on heap data structure
  • allow multiple keys
  • adapted test program
File size: 1.1 KB
Line 
1
2using System;
3using System.Collections.Generic;
4namespace HeuristicLab.Algorithms.GraphRouting {
5  public class PriorityQueueOld<P, T> where P : IComparable {
6    SortedList<Pair<P>, T> list;
7    P dummy;
8
9    public PriorityQueueOld() {
10      list = new SortedList<Pair<P>, T>(new PairComparer<P>());
11    }
12
13    public int Count {
14      get { return list.Count; }
15    }
16
17    public void Enqueue(T item, P priority) {
18      list.Add(new Pair<P>(priority, dummy), item);
19      //count++;
20    }
21
22    public T Dequeue() {
23      T item = list[list.Keys[0]];
24      list.RemoveAt(0);
25      return item;
26    }
27
28    public bool Contains(T item) {
29      return list.ContainsValue(item);
30    }
31
32    public bool Remove(T item) {
33      bool delete = false;
34      KeyValuePair<Pair<P>, T> toDelete = new KeyValuePair<Pair<P>, T>();
35      foreach (var pair in list) {
36        if (pair.Value.Equals(item)) {
37          delete = true;
38          toDelete = pair;
39        }
40      }
41      if (delete) {
42        return list.Remove(toDelete.Key);
43      }
44      return false;
45    }
46
47  }
48}
Note: See TracBrowser for help on using the repository browser.