http://stackoverflow.com/questions/17684170/objective-c-priority-queue

PriorityQueue.h

//
// PriorityQueue.h
// #import <Foundation/Foundation.h>
#import "comparable.h" //Implements a priority queue. All objects in queue must implement the comparable protocol and must be all of the same type. The queue can be explicity typed at initialization, otherwise the type of the first object entered will be the type of the queue
@interface PriorityQueue : NSObject{
NSMutableArray *queue;
Class type;
} - (id)init;
- (id)initWithObjects:(NSSet *)objects;
- (id)initWithCapacity:(int)capacity;
- (id)initWithCapacity:(int)capacity andType:(Class)oType; //Queue will reject objects not of that type #pragma mark - Useful information
- (BOOL)isEmpty;
- (BOOL)contains:(id<comparable, NSObject>)object;
- (Class)typeOfAllowedObjects; //Returns the type of objects allowed to be stored in the queue
- (int) size; #pragma mark - Mutation
- (void)clear;
- (BOOL)add:(id<comparable, NSObject>)object;
- (void)remove:(id<comparable, NSObject>)object; #pragma mark - Getting things out
- (id)peek;
- (id)poll;
- (id)objectMatchingObject:(id<comparable, NSObject>)object;
- (NSArray *)toArray; #pragma mark -
- (void)print; @end

PriorityQueue.m

//
// PriorityQueue.m
// #import "PriorityQueue.h" #define INITIAL_CAPACITY 50
@implementation PriorityQueue #pragma mark - Initialization
- (id)init{
return [self initWithCapacity:INITIAL_CAPACITY andType:nil];
} - (id)initWithObjects:(NSSet *)objects{
self = [self initWithCapacity:INITIAL_CAPACITY andType:nil];
for (id<comparable, NSObject>object in objects){
[self add:object];
}
return self;
} - (id)initWithCapacity:(int)capacity{
return [self initWithCapacity:capacity andType:nil];
} - (id)initWithCapacity:(int)capacity andType:(Class)oType{
self = [super init];
if(self){
queue = [[NSMutableArray alloc] init];
type = oType;
}
return self;
} #pragma mark - Useful information
- (BOOL)isEmpty{
if(queue.count == ){
return YES;
}
else{ return NO;}
} - (BOOL)contains:(id<comparable, NSObject>)object{
//Search the array to see if the object is already there
for(id<comparable> o in queue){
if([o isEqual:object]){
return YES;
}
}
return NO;
} - (Class)typeOfAllowedObjects{
NSLog(@"Allowed Types: %@", type);
return type;
} - (int) size{
return [queue count];
} #pragma mark - Mutation
//Mutation
- (void)clear{
[queue removeAllObjects];
} //A "greater" object (compareTo returns 1) is at the end of the queue.
- (BOOL)add:(id<comparable, NSObject>)object{
//Make sure the object's type is the same as the type of the queue
if(type == nil){
// NSLog(@"Type is nil");
type = [object class];
}
if([object class] != type){
NSLog(@"ERROR: Trying to add incorrect object");
return NO;
} if([queue count] == ){
[queue addObject:object];
return YES;
}
for(int i = ; i < [queue count]; i++){
if([object compareTo:queue[i]] < ){
[queue insertObject:object atIndex:i];
return YES;
}
}
[queue addObject:object];
return YES;
} - (void)remove:(id<comparable, NSObject>)object{
[queue removeObject:object];
} #pragma mark - Getting things out
- (id)peek{
return queue[];
} - (id)poll{
//Get the object at the front
id head = queue[]; //Remove and return that object
[queue removeObject:head];
return head;
} - (id)objectMatchingObject:(id<comparable, NSObject>)object{
//Search the array to see if the object is already there
for(id<comparable> o in queue){
if([o isEqual:object]){
return o;
}
}
return nil;
} - (NSArray *)toArray{
return [[NSArray alloc] initWithArray:queue];
} #pragma mark -
- (NSString *)description{
return [NSString stringWithFormat:@"PriorityQueue: %@ allows objects of type %@", queue, type];
} - (void)print{
NSLog(@"%@", [self description]);
} @end

Comparable.h

//
// comparable.h
// #import <Foundation/Foundation.h> //NOTE: Class must check to make sure it is the same class as whatever is passed in
@protocol comparable - (int)compareTo:(id<comparable, NSObject>)object;
- (BOOL)isEqual:(id<comparable, NSObject>)object; @end

Objective-C priority queue的更多相关文章

  1. STL-<queue>-priority queue的使用

    简介: 优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符).它是以堆为基础实现的一种数据结构. 成员函数(Member functions) (const ...

  2. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

  3. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...

  4. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅳ

    2.4.4 堆的算法 我们用长度为 N + 1的私有数组pq[]来表示一个大小为N的堆,我们不会使用pq[0],堆元素放在pq[1]至pq[N]中.在排序算法中,我们只能通过私有辅助函数less()和 ...

  5. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ

    许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...

  6. 什么是优先级队列(priority queue)?

    有时候我们需要在某个元素集合中找到最小值和最大值 .优先级队列抽象数据(Priority Queue ADT)模型是我们能够使用的方法之一,这是一种支持插入和删除最小值(DeleteMin)或者最大值 ...

  7. 优先队列Priority Queue和堆Heap

    对COMP20003中的Priority queue部分进行总结.图片来自于COMP20003 queue队列,顾名思义特点先进先出 priority queue优先队列,出来的顺序按照优先级prio ...

  8. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  9. Priority Queue

    优先队列 集合性质的数据类型离不开插入删除这两操作,主要区别就在于删除的时候删哪个,像栈删最晚插入的,队列删最早插入的,随机队列就随便删,而优先队列删除当前集合里最大(或最小)的元素.优先队列有很多应 ...

随机推荐

  1. win7 无线网络无法启动

    开始菜单-运行输入services.msc然后确定!找到WLAN Autoconfig这一项,启动此项服务,一切就OK了

  2. linux怎么给一个普通用户reboot权限?

    分四种情况讨论:1.让任何人(包括根本不拥有系统帐号的人)都可以通过控制台reboot在/etc/inittab文件中保留ca::ctrlaltdel:/sbin/shutdown -t3 -r no ...

  3. 字符串格式化 String.format() 案例

    转换符 转换符      说    明 %s            字符串类型 %c            字符类型 %b            布尔类型 %d            整数类型(十进制 ...

  4. GridView下DropDownList 的选择方法onselectedindexchanged 实现方法

    在GridView下面绑定好了下拉框,我们常常会遇到一个问题, 选择方法怎么实现呢,用js总是难的去算是在GridView的第几行第几个元素,因为服务器的id和客户端的id经常变化让js根本无从找起, ...

  5. ADB错误“more than one device and emulator”(转)

    当我连着手机充电的时候,启动模拟器调试,执行ADB指令时,报错.C:\Users\gaojs>adb shellerror: more than one device and emulatorC ...

  6. CDN的全称是Content Delivery Network,即内容分发网络

    CDN的全称是Content Delivery Network,即内容分发网络 http://baike.baidu.com/link?url=Wd-IGGgslfJemdpuT3Y0BUi88RPQ ...

  7. iOS判断当前控制器是否正在显示

    +(BOOL)isCurrentViewControllerVisible:(UIViewController *)viewController { return (viewController.is ...

  8. c 中可变参数的实现

    我们在C语言编程中有时会遇到一些参数个数可变的函数,例如printf()函数,其函数原型为:     例一: int   printf(   const   char*   format,   ... ...

  9. hdu 1241

    1.题目大意:给定一个图,上边有*和@两种标记,其中@表示石油,好多@连在一起可以看成一个大的石油区,问在这个区域中有多少个石油区 #include<iostream> using nam ...

  10. Oracle数据库之创建表空间与用户

    Oracle数据库之创建表空间与用户 一.创建表空间 基本语法表述: CREATE TABLESPACE tablespace_name [DATAFILE datafile_spec1 [,data ...