summaryrefslogtreecommitdiff
path: root/src/itoheap.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-02-19 21:51:48 +0000
committerMike Buland <eichlan@xagasoft.com>2008-02-19 21:51:48 +0000
commita4c22303b1044eee5ccbf0766895a879a8f4810e (patch)
treebe6563dfca81f57e4883a55c4517f5878c40dd4e /src/itoheap.h
parent62287ea894a1587553f017d0f6a09d4c87ad3b1f (diff)
downloadlibbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.tar.gz
libbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.tar.bz2
libbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.tar.xz
libbu++-a4c22303b1044eee5ccbf0766895a879a8f4810e.zip
Oops. I made the Bu::Heap API look like a stack, not a queue, that's been
fixed, and the Bu::ItoHeap is working and tested. Note that when multiple items have the same sort order, they will come out in random order.
Diffstat (limited to 'src/itoheap.h')
-rw-r--r--src/itoheap.h154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/itoheap.h b/src/itoheap.h
index e69de29..59a8158 100644
--- a/src/itoheap.h
+++ b/src/itoheap.h
@@ -0,0 +1,154 @@
1/*
2 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_ITO_HEAP_H
9#define BU_ITO_HEAP_H
10
11#include "bu/heap.h"
12#include "bu/itomutex.h"
13#include "bu/itocondition.h"
14
15namespace Bu
16{
17 class ItoMutex;
18 class ItoCondition;
19
20 template<typename item, typename cmpfunc=__basicLTCmp<item>,
21 typename itemalloc=std::allocator<item> >
22 class ItoHeap
23 {
24 public:
25 ItoHeap()
26 {
27 }
28
29 virtual ~ItoHeap()
30 {
31 }
32
33 void enqueue( item i )
34 {
35 imData.lock();
36 hData.enqueue( i );
37 icBlock.signal();
38 imData.unlock();
39 }
40
41 item dequeue( bool bBlock=false )
42 {
43 imData.lock();
44 if( hData.isEmpty() )
45 {
46 imData.unlock();
47
48 if( bBlock )
49 {
50 icBlock.lock();
51
52 while( hData.isEmpty() )
53 icBlock.wait();
54
55 imData.lock();
56 try
57 {
58 item iRet = hData.dequeue();
59 imData.unlock();
60 icBlock.unlock();
61 return iRet;
62 }
63 catch(...)
64 {
65 imData.unlock();
66 icBlock.unlock();
67 throw;
68 }
69 }
70 throw HeapException("Heap empty.");
71 }
72 else
73 {
74 try
75 {
76 item iRet = hData.dequeue();
77 imData.unlock();
78 return iRet;
79 }
80 catch(...)
81 {
82 imData.unlock();
83 throw;
84 }
85 }
86 }
87
88 item dequeue( int iSec, int iUSec )
89 {
90 imData.lock();
91 if( hData.isEmpty() )
92 {
93 imData.unlock();
94
95 icBlock.lock();
96
97 icBlock.wait( iSec, iUSec );
98
99 imData.lock();
100 try
101 {
102 item iRet = hData.dequeue();
103 imData.unlock();
104 icBlock.unlock();
105 return iRet;
106 }
107 catch(...)
108 {
109 imData.unlock();
110 icBlock.unlock();
111 throw;
112 }
113 }
114 else
115 {
116 try
117 {
118 item iRet = hData.dequeue();
119 imData.unlock();
120 return iRet;
121 }
122 catch(...)
123 {
124 imData.unlock();
125 throw;
126 }
127 }
128 }
129
130 bool isEmpty()
131 {
132 imData.lock();
133 bool bRet = hData.isEmpty();
134 imData.unlock();
135 return bRet;
136 }
137
138 int getSize()
139 {
140 imData.lock();
141 int iRet = hData.getSize();
142 imData.unlock();
143 return iRet;
144 }
145
146 private:
147 Heap< item, cmpfunc, itemalloc > hData;
148 ItoMutex imData;
149 ItoCondition icBlock;
150 };
151};
152
153#endif
154