zoukankan      html  css  js  c++  java
  • NSMapTable、NSHashTable与NSPointerArray的封装

    NSMapTable、NSHashTable与NSPointerArray的封装

    说明

    NSMapTable对应NSDictionary;NSHashTable对应NSSet;NSPointerArray对应NSArray,本人通过装饰设计模式对他们的使用进行了封装。

    源码

    https://github.com/YouXianMing/WeakList

    //
    //  WeakDictionary.h
    //  IteratorPattern
    //
    //  Created by YouXianMing on 15/9/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface WeakDictionary : NSObject
    
    /**
     *  元素个数
     */
    @property (readonly) NSUInteger count;
    
    /**
     *  获取对象
     *
     *  @param aKey
     *
     *  @return 对象
     */
    - (id)objectForKey:(id)aKey;
    
    /**
     *  根据键值移除对象
     *
     *  @param aKey 键值
     */
    - (void)removeObjectForKey:(id)aKey;
    
    /**
     *  添加对象
     *
     *  @param anObject 对象
     *  @param aKey     键值
     */
    - (void)setObject:(id)anObject forKey:(id)aKey;
    
    /**
     *  键值枚举器
     *
     *  @return 枚举器
     */
    - (NSEnumerator *)keyEnumerator;
    
    /**
     *  对象枚举器
     *
     *  @return 对象枚举器
     */
    - (NSEnumerator *)objectEnumerator;
    
    /**
     *  移除所有对象
     */
    - (void)removeAllObjects;
    
    /**
     *  返回字典
     *
     *  @return 字典
     */
    - (NSDictionary *)dictionaryRepresentation;
    
    @end
    //
    //  WeakDictionary.m
    //  IteratorPattern
    //
    //  Created by YouXianMing on 15/9/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import "WeakDictionary.h"
    
    @interface WeakDictionary () {
    
        NSMapTable  *_mapTable;
    }
    
    @end
    
    @implementation WeakDictionary
    
    - (instancetype)init {
        
        self = [super init];
        if (self) {
            
            _mapTable = [NSMapTable strongToWeakObjectsMapTable];
        }
        
        return self;
    }
    
    - (id)objectForKey:(id)aKey {
    
        return [_mapTable objectForKey:aKey];
    }
    
    - (void)removeObjectForKey:(id)aKey {
    
        [_mapTable removeObjectForKey:aKey];
    }
    
    - (void)setObject:(id)anObject forKey:(id)aKey {
    
        [_mapTable setObject:anObject forKey:aKey];
    }
    
    - (NSEnumerator *)keyEnumerator {
    
        return [_mapTable keyEnumerator];
    }
    
    - (NSEnumerator *)objectEnumerator {
    
        return [_mapTable objectEnumerator];
    }
    
    - (void)removeAllObjects {
    
        [_mapTable removeAllObjects];
    }
    
    - (NSDictionary *)dictionaryRepresentation {
    
        return [_mapTable dictionaryRepresentation];
    }
    
    @synthesize count = _count;
    - (NSUInteger)count {
    
        return _mapTable.count;
    }
    
    - (NSString *)description {
        
        return [NSString stringWithFormat:@"%@", _mapTable.dictionaryRepresentation];
    }
    
    @end
    //
    //  WeakSet.h
    //  IteratorPattern
    //
    //  Created by YouXianMing on 15/9/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface WeakSet : NSObject
    
    /**
     *  元素个数
     */
    @property (readonly)            NSUInteger  count;
    
    /**
     *  所有对象
     */
    @property (readonly, copy)      NSArray    *allObjects;
    
    /**
     *  获取一个对象
     */
    @property (readonly, nonatomic) id          anyObject;
    
    /**
     *  获取集合
     */
    @property (readonly, copy)      NSSet      *setRepresentation;
    
    - (id)member:(id)object;
    - (NSEnumerator *)objectEnumerator;
    - (void)addObject:(id)object;
    - (void)removeObject:(id)object;
    - (void)removeAllObjects;
    - (BOOL)containsObject:(id)anObject;
    
    @end
    //
    //  WeakSet.m
    //  IteratorPattern
    //
    //  Created by YouXianMing on 15/9/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import "WeakSet.h"
    
    @interface WeakSet () {
    
        NSHashTable  *_hashTable;
    }
    
    @end
    
    @implementation WeakSet
    
    - (instancetype)init {
        
        self = [super init];
        if (self) {
        
            _hashTable = [NSHashTable weakObjectsHashTable];
        }
        
        return self;
    }
    
    - (id)member:(id)object {
    
        return [_hashTable member:object];
    }
    
    - (NSEnumerator *)objectEnumerator {
    
        return [_hashTable objectEnumerator];
    }
    
    - (void)addObject:(id)object {
    
        [_hashTable addObject:object];
    }
    
    - (void)removeObject:(id)object {
    
        [_hashTable removeObject:object];
    }
    
    - (void)removeAllObjects {
    
        [_hashTable removeAllObjects];
    }
    
    - (BOOL)containsObject:(id)anObject {
    
        return [_hashTable containsObject:anObject];
    }
    
    @synthesize count = _count;
    - (NSUInteger)count {
    
        return _hashTable.count;
    }
    
    @synthesize allObjects = _allObjects;
    - (NSArray *)allObjects {
    
        return [_hashTable allObjects];
    }
    
    @synthesize anyObject = _anyObject;
    - (id)anyObject {
    
        return [_hashTable anyObject];
    }
    
    @synthesize setRepresentation = _setRepresentation;
    - (NSSet *)setRepresentation {
    
        return [_hashTable setRepresentation];
    }
    
    - (NSString *)description {
        
        return [NSString stringWithFormat:@"%@", _hashTable.allObjects];
    }
    
    @end
    //
    //  WeakArray.h
    //  IteratorPattern
    //
    //  Created by YouXianMing on 15/9/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface WeakArray : NSObject
    
    @property (readonly, copy) NSArray    *allObjects;
    @property (readonly)       NSUInteger  count;
    
    - (id)objectAtIndex:(NSUInteger)index;
    - (void)addObject:(id)object;
    - (void)removeObjectAtIndex:(NSUInteger)index;
    - (void)insertObject:(id)object atIndex:(NSUInteger)index;
    - (void)replaceObjectAtIndex:(NSUInteger)index withPointer:(id)object;
    - (void)compact;
    
    @end
    //
    //  WeakArray.m
    //  IteratorPattern
    //
    //  Created by YouXianMing on 15/9/12.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import "WeakArray.h"
    
    @interface WeakArray () {
    
        NSPointerArray *_pointerArray;
    }
    
    @end
    
    @implementation WeakArray
    
    - (instancetype)init {
        
        self = [super init];
        if (self) {
        
            _pointerArray = [NSPointerArray pointerArrayWithOptions:NSPointerFunctionsWeakMemory];
        }
        
        return self;
    }
    
    - (id)objectAtIndex:(NSUInteger)index {
    
        return [_pointerArray pointerAtIndex:index];
    }
    
    - (void)addObject:(id)object {
    
        [_pointerArray addPointer:(__bridge void *)(object)];
    }
    
    - (void)removeObjectAtIndex:(NSUInteger)index {
    
        [_pointerArray removePointerAtIndex:index];
    }
    
    - (void)insertObject:(id)object atIndex:(NSUInteger)index {
    
        [_pointerArray insertPointer:(__bridge void *)(object) atIndex:index];
    }
    
    - (void)replaceObjectAtIndex:(NSUInteger)index withPointer:(id)object {
    
        [_pointerArray replacePointerAtIndex:index withPointer:(__bridge void *)(object)];
    }
    
    - (void)compact {
    
        [_pointerArray compact];
    }
    
    @synthesize count = _count;
    - (NSUInteger)count {
    
        return _pointerArray.count;
    }
    
    - (NSString *)description {
        
        return [NSString stringWithFormat:@"%@", _pointerArray.allObjects];
    }
    
    @synthesize allObjects = _allObjects;
    - (NSArray *)allObjects {
    
        return _pointerArray.allObjects;
    }
    
    @end

    使用

  • 相关阅读:
    DSP28335 烧写到RAM和FLASH方法
    脉冲提取
    基于飞秒光频梳的正弦相位调制干涉绝对距离测量方法研究 -张世华
    DSP28335 IO口寄存器
    DSP28335 使用技巧
    freertos、UCos这种实时操作系统和Linux、Windows这种系统的 本质区别
    stm32的定时器输入捕获 与输出比较
    五大常用算法--回溯
    Dijkstra算法
    字典树-Trie
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4803290.html
Copyright © 2011-2022 走看看