OC实现LRU算法O(1)取,O(1)存,O(1)删

371 阅读2分钟

#import <Foundation/Foundation.h>
@class LRUNode;

@interface LRUObject : NSObject

- (LRUNode *)getLRUObject:(id)object;
- (void)removeLRUObject:(id)object;
- (void)addLRUObject:(id)object;
@end


#import "LRUObject.h"

@interface LRUNode : NSObject
@property(nonatomic, strong) id value;
@property(nonatomic, strong) LRUNode *pre;
@property(nonatomic, strong) LRUNode *next;
@end

@implementation LRUNode
@end

@interface LRUObject(){
    @package
    NSMutableDictionary *_nodeDic;
    LRUNode *_head;
    LRUNode *_tail;
    NSInteger _count;
}

@end

@implementation LRUObject

- (instancetype)init{
    if (self = [super init]) {
        _nodeDic = [NSMutableDictionary dictionary];
        _head = nil;
        _tail = nil;
        _count = 0;
    }
    return self;
}
//添加元素
- (void)addLRUObject:(id)object{
    if (object) {
        LRUNode *needObject = nil;
        NSString *key = [NSString stringWithFormat:@"%p",object];
        if ([_nodeDic.allKeys containsObject:key]) {
            needObject = [_nodeDic objectForKey:key];
            if (needObject == _head) {
                return;
            }
            if (needObject == _tail) {
                needObject.pre.next = nil;
                _tail = needObject.pre;
            }else{
                needObject.pre.next = needObject.next;
                needObject.next.pre = needObject.pre;
            }
            
            needObject.next = _head;
            _head.pre = needObject;
            _head = needObject;
            
        }else{
            LRUNode *needItem = [self createLRUNode:object];
            
            [_nodeDic setValue:needItem forKey:key];
            if (_head) {
                needItem.next = _head;
                _head.pre = needItem;
                _head = needItem;
            }else{
                _head = needItem;
                _head.next = nil;
                _tail = needItem;
                _tail.next = nil;
            }
        }
    }
}
//移除元素
- (void)removeLRUObject:(id)object{
    if (object) {
        LRUNode *needObject = nil;
        NSString *key = [NSString stringWithFormat:@"%p",object];
        if ([_nodeDic.allKeys containsObject:key]) {
            needObject = [_nodeDic objectForKey:key];
            [_nodeDic removeObjectForKey:key];
            if (needObject == _head) {
                _head = _head.next;
                _head.pre = nil;
                return;
            }
            if (needObject == _tail) {
                needObject.pre.next = nil;
                _tail = needObject.pre;
            }else{
                needObject.pre.next = needObject.next;
                needObject.next.pre = needObject.pre;
            }
        }
    }
}

//更新元素
- (LRUNode *)getLRUObject:(id)object{
    if (object) {
        LRUNode *needObject = nil;
        NSString *key = [NSString stringWithFormat:@"%p",object];
        if ([_nodeDic.allKeys containsObject:key]) {
            needObject = [_nodeDic objectForKey:key];
            if (needObject == _head) {
                return needObject;
            }
            if (needObject == _tail) {
                needObject.pre.next = nil;
                _tail = needObject.pre;
            }else{
                needObject.pre.next = needObject.next;
                needObject.next.pre = needObject.pre;
            }
            
            needObject.next = _head;
            _head.pre = needObject;
            _head = needObject;
            
        }else{
            LRUNode *needItem = [self createLRUNode:object];
            [_nodeDic setValue:needItem forKey:key];
            if (_head) {
                needItem.next = _head.next;
                needItem.pre = _head;
                _head = needItem;
            }else{
                _head = needItem;
                _head.next = nil;
                _tail = needItem;
                _tail.next = nil;
            }
            return needItem;
        }
        return needObject;
    }
    return nil;
}
//创建元素
- (LRUNode *)createLRUNode:(id)value{
    LRUNode *node = [LRUNode new];
    node.value = value;
    node.pre = nil;
    node.next = nil;
    return node;
}
//打印元素
- (NSString*)description{
    NSMutableString *result = [NSMutableString string];
    LRUNode *temp = _head;
    while (temp) {
        [result appendFormat:@"%@-",temp.value];
        temp = temp.next;
    }
    return result;
}
@end

//调用
LRUObject *hashTable = [LRUObject new];

    [hashTable addLRUObject:@"1"];

    [hashTable addLRUObject:@"2"];

    [hashTable addLRUObject:@"3"];

    NSLog(@"%@",hashTable);

    [hashTable getLRUObject:@"2"];

    NSLog(@"%@",hashTable);

    

    [hashTable removeLRUObject:@"1"];

    NSLog(@"%@",hashTable);

    

    [hashTable addLRUObject:@"5"];

    NSLog(@"%@",hashTable);

    

    [hashTable addLRUObject:@"4"];

    NSLog(@"%@",hashTable);

    

    [hashTable getLRUObject:@"2"];

    NSLog(@"%@",hashTable);

    

    [hashTable getLRUObject:@"3"];

    NSLog(@"%@",hashTable);

    

    [hashTable removeLRUObject:@"3"];

    NSLog(@"%@",hashTable);

    

    [hashTable getLRUObject:@"5"];

    NSLog(@"%@",hashTable);

//输出结果
2022-02-25 00:24:52.599105+0800 Test_2_24[2222:128336] 3-2-1-
2022-02-25 00:24:52.599253+0800 Test_2_24[2222:128336] 2-3-1-
2022-02-25 00:24:52.599364+0800 Test_2_24[2222:128336] 2-3-
2022-02-25 00:24:52.599464+0800 Test_2_24[2222:128336] 5-2-3-
2022-02-25 00:24:52.599560+0800 Test_2_24[2222:128336] 4-5-2-3-
2022-02-25 00:24:52.599676+0800 Test_2_24[2222:128336] 2-4-5-3-
2022-02-25 00:24:52.599779+0800 Test_2_24[2222:128336] 3-2-4-5-
2022-02-25 00:24:52.599910+0800 Test_2_24[2222:128336] 2-4-5-
2022-02-25 00:24:52.600052+0800 Test_2_24[2222:128336] 5-2-4-