Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOPriorityQueue.h
Go to the documentation of this file.
1/*
2
3OOPriorityQueue.h
4
5A prority queue is a collection into which objects may be inserted in any
6order, but (primarily) extracted in sorted order. The order is defined by the
7comparison selector specified at creation time, which is assumed to have the
8same signature as a compare method used for array sorting:
9- (NSComparisonResult)compare:(id)other
10and must define a partial order on the objects in the priority queue. The
11behaviour when provided with an inconsistent comparison method is undefined.
12
13The implementation is the standard one, a binary heap. It is described in
14detail in most algorithm textbooks.
15
16This collection is *not* thread-safe.
17
18
19Copyright (C) 2007-2013 Jens Ayton
20
21Permission is hereby granted, free of charge, to any person obtaining a copy
22of this software and associated documentation files (the "Software"), to deal
23in the Software without restriction, including without limitation the rights
24to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25copies of the Software, and to permit persons to whom the Software is
26furnished to do so, subject to the following conditions:
27
28The above copyright notice and this permission notice shall be included in all
29copies or substantial portions of the Software.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37SOFTWARE.
38
39*/
40
41#import "OOCocoa.h"
42
43#ifndef OO_PQ_STRONG
44#if __has_feature(objc_arc)
45#define OO_PQ_STRONG __strong
46#else
47#define OO_PQ_STRONG
48#endif
49#endif
50
51
52@interface OOPriorityQueue: NSObject <NSCopying>
53{
54@private
55 SEL _comparator;
56 OO_PQ_STRONG id *_heap;
57 NSUInteger _count,
60
61// Note: -init is equivalent to -initWithComparator:@selector(compare:)
62+ (instancetype) queueWithComparator:(SEL)comparator;
63- (id) initWithComparator:(SEL)comparator;
64
65- (void) addObject:(id)object; // May throw NSInvalidArgumentException or NSMallocException.
66- (void) removeObject:(id)object; // Uses comparator (looking for NSOrderedEqual) to find object. Note: relatively expensive.
67- (void) removeExactObject:(id)object; // Uses pointer comparison to find object. Note: still relatively expensive.
68
69- (NSUInteger) count;
70
71- (id) nextObject;
72- (id) peekAtNextObject; // Returns next object without removing it.
73- (void) removeNextObject;
74
75- (void) addObjects:(id)collection; // collection must respond to -nextObject, or implement -objectEnumerator to return something that implements -nextObject -- such as an NSEnumerator.
76
77- (NSArray *) sortedObjects; // Returns the objects in -nextObject order and empties the heap. To get the objects without emptying the heap, copy the priority queue first.
78- (NSEnumerator *) objectEnumerator; // Enumerator which pulls objects off the heap until it's empty. Note however that the queue itself behaves like an enumerator, as -nextObject has similar semantics (except that the enumerator's -nextObject can never start returning objects after it returns nil).
79
80@end
unsigned count
OO_PQ_STRONG id * _heap
NSUInteger _capacity