2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef _PriorityQueue_
22 #define _PriorityQueue_
26 // #include <stdexcept>
30 #if defined(_WIN32) && !defined(__MINGW32__)
31 const int64 kMaxInt64
= 0x7FFFFFFFFFFFFFFF;
33 const int64 kMaxInt64
= ~(1LL<<63);
36 template <class Event
, int N
>
44 bool Add(Event
& inEvent
)
46 if (mSize
>= N
) return false;
49 for (; mom
>0;) { /* percolate up heap */
51 if (inEvent
.mTime
< mEvents
[mom
].mTime
) {
52 mEvents
[me
] = mEvents
[mom
];
56 mEvents
[me
] = inEvent
;
62 void Perform(int64 inTime
)
64 while (NextTime() <= inTime
) {
65 Event event
= Remove();
69 int64
NextTime() { return mEvents
[0].mTime
; }
70 bool Ready(int64 inTime
) { return NextTime() <= inTime
; }
71 void Flush() { Perform(kMaxInt64
); }
72 void Empty() { mSize
= 0; SetEmptyTime(); }
73 void SetEmptyTime() { mEvents
[0].mTime
= kMaxInt64
; }
74 int Size() { return mSize
; }
78 Event event
= mEvents
[0];
79 if (--mSize
== 0) SetEmptyTime();
81 Event temp
= mEvents
[mSize
];
84 for (;me
< mSize
;) { /* demote heap */
85 if (me
+1 < mSize
&& mEvents
[me
].mTime
> mEvents
[me
+1].mTime
) {
88 if (temp
.mTime
> mEvents
[me
].mTime
) {
89 mEvents
[mom
] = mEvents
[me
];
103 for (int i
=0; i
<mSize
; ++i
)
106 //if (j<mSize && mEvents[i].mTime > mEvents[j].mTime) throw std::runtime_error("priority queue unsorted");
107 //if (k<mSize && mEvents[i].mTime > mEvents[k].mTime) throw std::runtime_error("priority queue unsorted");
112 for (int i
=0; i
<mSize
; ++i
)
114 printf("%d %016llX\n", i
, mEvents
[i
].mTime
);