| 1 | /*
|
|---|
| 2 | * synergy -- mouse and keyboard sharing utility
|
|---|
| 3 | * Copyright (C) 2003 Chris Schoeneman
|
|---|
| 4 | *
|
|---|
| 5 | * This package is free software; you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU General Public License
|
|---|
| 7 | * found in the file COPYING that should have accompanied this file.
|
|---|
| 8 | *
|
|---|
| 9 | * This package is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | #ifndef CPRIORITYQUEUE_H
|
|---|
| 16 | #define CPRIORITYQUEUE_H
|
|---|
| 17 |
|
|---|
| 18 | #include "stdvector.h"
|
|---|
| 19 | #include <algorithm>
|
|---|
| 20 | #include <functional>
|
|---|
| 21 |
|
|---|
| 22 | //! A priority queue with an iterator
|
|---|
| 23 | /*!
|
|---|
| 24 | This priority queue is the same as a standard priority queue except:
|
|---|
| 25 | it sorts by std::greater, it has a forward iterator through the elements
|
|---|
| 26 | (which can appear in any order), and its contents can be swapped.
|
|---|
| 27 | */
|
|---|
| 28 | template <class T, class Container = std::vector<T>,
|
|---|
| 29 | #if defined(_MSC_VER)
|
|---|
| 30 | class Compare = std::greater<Container::value_type> >
|
|---|
| 31 | #else
|
|---|
| 32 | class Compare = std::greater<typename Container::value_type> >
|
|---|
| 33 | #endif
|
|---|
| 34 | class CPriorityQueue {
|
|---|
| 35 | public:
|
|---|
| 36 | typedef typename Container::value_type value_type;
|
|---|
| 37 | typedef typename Container::size_type size_type;
|
|---|
| 38 | typedef typename Container::iterator iterator;
|
|---|
| 39 | typedef typename Container::const_iterator const_iterator;
|
|---|
| 40 | typedef Container container_type;
|
|---|
| 41 |
|
|---|
| 42 | CPriorityQueue() { }
|
|---|
| 43 | CPriorityQueue(Container& swappedIn) { swap(swappedIn); }
|
|---|
| 44 | ~CPriorityQueue() { }
|
|---|
| 45 |
|
|---|
| 46 | //! @name manipulators
|
|---|
| 47 | //@{
|
|---|
| 48 |
|
|---|
| 49 | //! Add element
|
|---|
| 50 | void push(const value_type& v)
|
|---|
| 51 | {
|
|---|
| 52 | c.push_back(v);
|
|---|
| 53 | std::push_heap(c.begin(), c.end(), comp);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | //! Remove head element
|
|---|
| 57 | void pop()
|
|---|
| 58 | {
|
|---|
| 59 | std::pop_heap(c.begin(), c.end(), comp);
|
|---|
| 60 | c.pop_back();
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | //! Erase element
|
|---|
| 64 | void erase(iterator i)
|
|---|
| 65 | {
|
|---|
| 66 | c.erase(i);
|
|---|
| 67 | std::make_heap(c.begin(), c.end(), comp);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | //! Get start iterator
|
|---|
| 71 | iterator begin()
|
|---|
| 72 | {
|
|---|
| 73 | return c.begin();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | //! Get end iterator
|
|---|
| 77 | iterator end()
|
|---|
| 78 | {
|
|---|
| 79 | return c.end();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | //! Swap contents with another priority queue
|
|---|
| 83 | void swap(CPriorityQueue<T, Container, Compare>& q)
|
|---|
| 84 | {
|
|---|
| 85 | c.swap(q.c);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | //! Swap contents with another container
|
|---|
| 89 | void swap(Container& c2)
|
|---|
| 90 | {
|
|---|
| 91 | c.swap(c2);
|
|---|
| 92 | std::make_heap(c.begin(), c.end(), comp);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | //@}
|
|---|
| 96 | //! @name accessors
|
|---|
| 97 | //@{
|
|---|
| 98 |
|
|---|
| 99 | //! Returns true if there are no elements
|
|---|
| 100 | bool empty() const
|
|---|
| 101 | {
|
|---|
| 102 | return c.empty();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | //! Returns the number of elements
|
|---|
| 106 | size_type size() const
|
|---|
| 107 | {
|
|---|
| 108 | return c.size();
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | //! Returns the head element
|
|---|
| 112 | const value_type& top() const
|
|---|
| 113 | {
|
|---|
| 114 | return c.front();
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | //! Get start iterator
|
|---|
| 118 | const_iterator begin() const
|
|---|
| 119 | {
|
|---|
| 120 | return c.begin();
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | //! Get end iterator
|
|---|
| 124 | const_iterator end() const
|
|---|
| 125 | {
|
|---|
| 126 | return c.end();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | //@}
|
|---|
| 130 |
|
|---|
| 131 | private:
|
|---|
| 132 | Container c;
|
|---|
| 133 | Compare comp;
|
|---|
| 134 | };
|
|---|
| 135 |
|
|---|
| 136 | #endif
|
|---|