http://*.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的更多相关文章
-
STL-<;queue>;-priority queue的使用
简介: 优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符).它是以堆为基础实现的一种数据结构. 成员函数(Member functions) (const ...
-
优先队列(Priority Queue)
优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...
-
《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...
-
《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅳ
2.4.4 堆的算法 我们用长度为 N + 1的私有数组pq[]来表示一个大小为N的堆,我们不会使用pq[0],堆元素放在pq[1]至pq[N]中.在排序算法中,我们只能通过私有辅助函数less()和 ...
-
《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅰ
许多应用程序都需要处理有序的元素,但不一定要求他们全部有序,或者是不一定要以此就将他们排序.很多情况下我们会手机一些元素,处理当前键值最大的元素,然后再收集更多的元素,再处理当前键值最大的元素.如此这 ...
-
什么是优先级队列(priority queue)?
有时候我们需要在某个元素集合中找到最小值和最大值 .优先级队列抽象数据(Priority Queue ADT)模型是我们能够使用的方法之一,这是一种支持插入和删除最小值(DeleteMin)或者最大值 ...
-
优先队列Priority Queue和堆Heap
对COMP20003中的Priority queue部分进行总结.图片来自于COMP20003 queue队列,顾名思义特点先进先出 priority queue优先队列,出来的顺序按照优先级prio ...
-
STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
-
Priority Queue
优先队列 集合性质的数据类型离不开插入删除这两操作,主要区别就在于删除的时候删哪个,像栈删最晚插入的,队列删最早插入的,随机队列就随便删,而优先队列删除当前集合里最大(或最小)的元素.优先队列有很多应 ...
随机推荐
-
case使用例子
echo 'Input a number between 1 to 4' echo 'Your number is:\c' read aNum case $aNum in 1) echo 'You s ...
- unity, itween, closed path
-
ODBC操作数据库
/*ODBC使用步骤:(ODBC数据源由微软平台提供) * 1.配置ODBC数据源(控制面板->管理工具->ODBC数据源) * 2.加载并注册驱动程序,导入java.sql.*包 * 3 ...
-
1819: [JSOI]Word Query电子字典
1819: [JSOI]Word Query电子字典 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 729 Solved: 238[Submit][S ...
-
WebP 的前世今生
除了视频,图片占据了 PC 和 App 的大部分流量,为运营方带来高额的成本支出,同时过多的图片加载会影响到网站与 App 的加载速度.因此在保证图片质量的前提下缩小图片的体积就成了迫在眉睫的事情. ...
-
Kubernetes 网络排错指南
本文介绍各种常见的网络问题以及排错方法,包括 Pod 访问异常.Service 访问异常以及网络安全策略异常等. 说到 Kubernetes 的网络,其实无非就是以下三种情况之一 Pod 访问容器外部 ...
-
CodeForces 371C Hamburgers(经典)【二分答案】
<题目链接> 题目大意: 给以一段字符串,其中只包含"BSC"这三个字符,现在有一定量免费的'B','S','C‘,然后如果想再买这三个字符,就要付出相应的价格.现在总 ...
-
MATLAB 程序计算结果出现 复数(a+bi)问题
存在对负数开根号的情况了: >> (0.777)^0.1 ans = 0.9751 >> ( ans = 0.6037 >> (0.777)^2.1 ans = 0 ...
-
gnu make - 初学
因为要为Linux平台编译ACE,按照ACE的文档如何编译部分的说明,要求使用gnu make.其原文档说明如下: Using the Traditional ACE/GNU Configuratio ...
-
windows多线程(二) 等待线程返回
多线程编程中,有时我们需要等待某一线程完成了特定的操作后再继续做其他事情,要实现这个目的,可以使用Windows API函数WaitForSingleObject,或者WaitForMultipleO ...