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

使用


目录
相关文章
|
8月前
|
存储 NoSQL MongoDB
mongdb如何查询数据库表的创建时间
【6月更文挑战第29天】mongdb如何查询数据库表的创建时间
175 2
|
Java 测试技术 Apache
|
8月前
|
SQL 缓存 数据管理
数据管理DMS产品使用合集之打开多个SQL窗口,在关闭浏览器重新登录只剩第一个窗口且部分脚本丢失,是什么导致的
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
85 0
代码随想录刷题|LeetCode 704 二分查找、27 移除元素
代码随想录刷题|LeetCode 704 二分查找、27 移除元素
|
3天前
|
人工智能 自然语言处理 Shell
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。
仅用3分钟,百炼调用满血版Deepseek-r1 API,享受百万免费Token。阿里云提供零门槛、快速部署的解决方案,支持云控制台和Cloud Shell两种方式,操作简便。Deepseek-r1满血版在推理能力上表现出色,尤其擅长数学、代码和自然语言处理任务,使用过程中无卡顿,体验丝滑。结合Chatbox工具,用户可轻松掌控模型,提升工作效率。阿里云大模型服务平台百炼不仅速度快,还确保数据安全,值得信赖。
141499 24
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。
|
5天前
|
人工智能 API 网络安全
用DeepSeek,就在阿里云!四种方式助您快速使用 DeepSeek-R1 满血版!更有内部实战指导!
DeepSeek自发布以来,凭借卓越的技术性能和开源策略迅速吸引了全球关注。DeepSeek-R1作为系列中的佼佼者,在多个基准测试中超越现有顶尖模型,展现了强大的推理能力。然而,由于其爆火及受到黑客攻击,官网使用受限,影响用户体验。为解决这一问题,阿里云提供了多种解决方案。
16495 37
|
13天前
|
机器学习/深度学习 人工智能 自然语言处理
PAI Model Gallery 支持云上一键部署 DeepSeek-V3、DeepSeek-R1 系列模型
DeepSeek 系列模型以其卓越性能在全球范围内备受瞩目,多次评测中表现优异,性能接近甚至超越国际顶尖闭源模型(如OpenAI的GPT-4、Claude-3.5-Sonnet等)。企业用户和开发者可使用 PAI 平台一键部署 DeepSeek 系列模型,实现 DeepSeek 系列模型与现有业务的高效融合。
|
5天前
|
并行计算 PyTorch 算法框架/工具
本地部署DeepSeek模型
要在本地部署DeepSeek模型,需准备Linux(推荐Ubuntu 20.04+)或兼容的Windows/macOS环境,配备NVIDIA GPU(建议RTX 3060+)。安装Python 3.8+、PyTorch/TensorFlow等依赖,并通过官方渠道下载模型文件。配置模型后,编写推理脚本进行测试,可选使用FastAPI服务化部署或Docker容器化。注意资源监控和许可协议。
1274 8
|
13天前
|
人工智能 搜索推荐 Docker
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
DeepSeek R1 + LobeChat + Ollama:快速本地部署模型,创建个性化 AI 助手
3401 117
手把手教你使用 Ollama 和 LobeChat 快速本地部署 DeepSeek R1 模型,创建个性化 AI 助手
|
8天前
|
人工智能 自然语言处理 API
DeepSeek全尺寸模型上线阿里云百炼!
阿里云百炼平台近日上线了DeepSeek-V3、DeepSeek-R1及其蒸馏版本等六款全尺寸AI模型,参数量达671B,提供高达100万免费tokens。这些模型在数学、代码、自然语言推理等任务上表现出色,支持灵活调用和经济高效的解决方案,助力开发者和企业加速创新与数字化转型。示例代码展示了如何通过API使用DeepSeek-R1模型进行推理,用户可轻松获取思考过程和最终答案。

热门文章

最新文章