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. redis 控制调用频率

    redis提供了rate limit demo 如下所示: INCR key Available since 1.0.0. Time complexity: O(1) Increments the n ...

  2. JAVA - 回调机制

    参考例子:android的Button OnClickListener接口.<第一行代码>中的回调例子   定义接口 public interface HttpCallbackListen ...

  3. linux下查看文件系统类型

    1. df -hT命令   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G) -T, --pr ...

  4. 零基础学习云计算及大数据DBA集群架构师【Linux Bash Shell编程及系统自动化2015年1月21日周四】

    lvy老师教项目课程,以及代课了shell部分课程,大家都觉得这位老师不行,上课时做的操作很多都是错误的,觉得她基础不好.而且,她不能解释原因,学生问为什么,她不知道.崩溃啊.向xx培训机构反应后,说 ...

  5. HTML基础总结<段落>

    HTML 段落 段落是通过 <p> 标签定义的. 实例 <p>This is a paragraph </p><p>This is another pa ...

  6. 自己动手写easyui的checkbox

    最近项目中用到了easyui这个框架,找了一圈也没有找到checkbox list控件,被迫只能自己实现了,为了便于复用,自己封装了下,有需要的,直接拿去用吧.有意见或建议的,欢迎指教啊. 调用示例 ...

  7. mongodb的副本集总结

    主节点A.备份节点B.仲裁者C.是否真的需求仲裁者? 需要. 怎样才算大多数表格如下: 怎样才算大多数表格 副本集中的成员总数 副本集中的大多数 1 1 2 2 3 2 4 3 5 3 6 4 7 4 ...

  8. nginx轮询配置详解

    nginx轮询配置详解... Nginx配置文件详细说明转载

  9. DotNet中的计时器线程计时器

    转载自:http://hi.baidu.com/wingingbob/item/9f1c9615f3b24d5f2b3e225c 基于多线程设计,计时器工作在ThreadPool线程上,存在事件的重入 ...

  10. mysql 自带全文检索

    对于一些简单的检索可以通过mysql自带的全文索引及 MATCH AGAINST 查询语句实现.实验步骤如下.1.建表DROP table IF exists con_video_file_des_t ...