zoukankan      html  css  js  c++  java
  • Objective-C priority queue

    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 == 0){
            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] == 0){
            [queue addObject:object];
            return YES;
        }
        for(int i = 0; i < [queue count]; i++){
            if([object compareTo:queue[i]] < 0){
                [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[0];
    }
    
    - (id)poll{
        //Get the object at the front
        id head = queue[0];
    
        //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
  • 相关阅读:
    [百度百科]PCI-E的速度
    Oracle 高版本往低版本备份恢复的方法
    PHP-Java-Bridge使用笔记
    Delphi获取其它进程窗口句柄的3种方法
    二层交换机当三层交换机,使用单臂路由实现二层交换机上的VLAN互通
    Python下科学计算包numpy和SciPy的安装【原创】
    OpenGL: 你不知道的左右手坐标系
    Delphi XE7的安卓程序如何调用JAVA的JAR,使用JAVA的类?
    科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系
    phpmyadmin #1045 #2002 无法登录 MySQL 服务器的解决方
  • 原文地址:https://www.cnblogs.com/hbf369/p/3529581.html
Copyright © 2011-2022 走看看