初始化提交
This commit is contained in:
66
WebDriverAgentLib/Utilities/LRUCache/LRUCache.h
Normal file
66
WebDriverAgentLib/Utilities/LRUCache/LRUCache.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface LRUCache : NSObject
|
||||
|
||||
/*! Maximum cache capacity. Could only be set in the constructor */
|
||||
@property (nonatomic, readonly) NSUInteger capacity;
|
||||
|
||||
/**
|
||||
Constructs a new LRU cache instance with the given capacity
|
||||
|
||||
@param capacity Maximum cache capacity
|
||||
*/
|
||||
- (instancetype)initWithCapacity:(NSUInteger)capacity;
|
||||
|
||||
/**
|
||||
Puts a new object into the cache. nil cannot be stored in the cache.
|
||||
|
||||
@param object Object to put
|
||||
@param key Object's key
|
||||
*/
|
||||
- (void)setObject:(id)object forKey:(id<NSCopying>)key;
|
||||
|
||||
/**
|
||||
Retrieves an object from the cache. Every time this method is called the matched
|
||||
object is bumped in the cache (if exists)
|
||||
|
||||
@param key Object's key
|
||||
@returns Either the stored instance or nil if the object does not exist or has expired
|
||||
*/
|
||||
- (nullable id)objectForKey:(id<NSCopying>)key;
|
||||
|
||||
/**
|
||||
Retrieves all values from the cache ORDERED by recent bump. No bump is performed
|
||||
|
||||
@return Array of all cache values ordred by recent usage (oldest items are at the tail)
|
||||
*/
|
||||
- (NSArray *)allObjects;
|
||||
|
||||
/**
|
||||
Removes the object associated with the specified key from the cache.
|
||||
|
||||
@param key The key identifying the object to remove.
|
||||
*/
|
||||
- (void)removeObjectForKey:(id<NSCopying>)key;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
146
WebDriverAgentLib/Utilities/LRUCache/LRUCache.m
Normal file
146
WebDriverAgentLib/Utilities/LRUCache/LRUCache.m
Normal file
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#import "LRUCache.h"
|
||||
#import "LRUCacheNode.h"
|
||||
|
||||
@interface LRUCache ()
|
||||
@property (nonatomic) NSMutableDictionary *store;
|
||||
@property (nonatomic, nullable) LRUCacheNode *headNode;
|
||||
@property (nonatomic, nullable) LRUCacheNode *tailNode;
|
||||
@end
|
||||
|
||||
@implementation LRUCache
|
||||
|
||||
- (instancetype)initWithCapacity:(NSUInteger)capacity
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_store = [NSMutableDictionary dictionary];
|
||||
_capacity = capacity;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setObject:(id)object forKey:(id<NSCopying>)key
|
||||
{
|
||||
NSAssert(nil != object && nil != key, @"LRUCache cannot store nil objects");
|
||||
|
||||
LRUCacheNode *previousNode = self.store[key];
|
||||
if (nil != previousNode) {
|
||||
[self removeNode:previousNode];
|
||||
}
|
||||
|
||||
LRUCacheNode *newNode = [LRUCacheNode nodeWithValue:object key:key];
|
||||
self.store[key] = newNode;
|
||||
[self addNodeToHead:newNode];
|
||||
if (nil == previousNode) {
|
||||
[self alignSize];
|
||||
}
|
||||
}
|
||||
|
||||
- (id)objectForKey:(id<NSCopying>)key
|
||||
{
|
||||
LRUCacheNode *node = self.store[key];
|
||||
return [self moveNodeToHead:node].value;
|
||||
}
|
||||
|
||||
- (NSArray *)allObjects
|
||||
{
|
||||
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:self.store.count];
|
||||
LRUCacheNode *node = self.headNode;
|
||||
while (node != nil) {
|
||||
[result addObject:node.value];
|
||||
node = node.next;
|
||||
}
|
||||
return result.copy;
|
||||
}
|
||||
|
||||
- (nullable LRUCacheNode *)moveNodeToHead:(nullable LRUCacheNode *)node
|
||||
{
|
||||
if (nil == node || node == self.headNode) {
|
||||
return node;
|
||||
}
|
||||
|
||||
LRUCacheNode *previousNode = node.prev;
|
||||
if (nil != previousNode) {
|
||||
previousNode.next = node.next;
|
||||
}
|
||||
LRUCacheNode *nextNode = node.next;
|
||||
if (nil != nextNode) {
|
||||
nextNode.prev = node.prev;
|
||||
}
|
||||
if (node == self.tailNode) {
|
||||
self.tailNode = previousNode;
|
||||
}
|
||||
return [self addNodeToHead:node];
|
||||
}
|
||||
|
||||
- (void)removeNode:(nullable LRUCacheNode *)node
|
||||
{
|
||||
if (nil == node) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (nil != node.next) {
|
||||
node.next.prev = node.prev;
|
||||
}
|
||||
if (node == self.headNode) {
|
||||
self.headNode = node.next;
|
||||
}
|
||||
if (nil != node.prev) {
|
||||
node.prev.next = node.next;
|
||||
}
|
||||
if (node == self.tailNode) {
|
||||
self.tailNode = node.prev;
|
||||
}
|
||||
[self.store removeObjectForKey:(id)node.key];
|
||||
}
|
||||
|
||||
- (nullable LRUCacheNode *)addNodeToHead:(nullable LRUCacheNode *)node
|
||||
{
|
||||
if (nil == node || node == self.headNode) {
|
||||
return node;
|
||||
}
|
||||
|
||||
LRUCacheNode *previousHead = self.headNode;
|
||||
if (nil != previousHead) {
|
||||
previousHead.prev = node;
|
||||
}
|
||||
node.next = previousHead;
|
||||
node.prev = nil;
|
||||
self.headNode = node;
|
||||
if (nil == self.tailNode) {
|
||||
self.tailNode = previousHead ?: node;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
- (void)alignSize
|
||||
{
|
||||
if (self.store.count > self.capacity && nil != self.tailNode) {
|
||||
[self removeNode:self.tailNode];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)removeObjectForKey:(id<NSCopying>)key
|
||||
{
|
||||
LRUCacheNode *node = self.store[key];
|
||||
if (node != nil) {
|
||||
[self removeNode:node];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
43
WebDriverAgentLib/Utilities/LRUCache/LRUCacheNode.h
Normal file
43
WebDriverAgentLib/Utilities/LRUCache/LRUCacheNode.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface LRUCacheNode : NSObject
|
||||
|
||||
/*! Node value */
|
||||
@property (nonatomic, readonly) id value;
|
||||
/*! Node key */
|
||||
@property (nonatomic, readonly) id<NSCopying> key;
|
||||
/*! Pointer to the next node */
|
||||
@property (nonatomic, nullable) LRUCacheNode *next;
|
||||
/*! Pointer to the previous node */
|
||||
@property (nonatomic, nullable) LRUCacheNode *prev;
|
||||
|
||||
/**
|
||||
Factory method to create a new cache node with the given value and key
|
||||
|
||||
@param value Node value
|
||||
@param key Node key
|
||||
@returns Cache node instance
|
||||
*/
|
||||
+ (instancetype)nodeWithValue:(id)value key:(id<NSCopying>)key;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
51
WebDriverAgentLib/Utilities/LRUCache/LRUCacheNode.m
Normal file
51
WebDriverAgentLib/Utilities/LRUCache/LRUCacheNode.m
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#import "LRUCacheNode.h"
|
||||
|
||||
@interface LRUCacheNode ()
|
||||
@property (nonatomic, readwrite) id value;
|
||||
@property (nonatomic, readwrite) id<NSCopying> key;
|
||||
|
||||
- (instancetype)initWithValue:(id)value key:(id<NSCopying>)key;
|
||||
@end
|
||||
|
||||
@implementation LRUCacheNode
|
||||
|
||||
- (instancetype)initWithValue:(id)value key:(id<NSCopying>)key
|
||||
{
|
||||
if (nil == value) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
if ((self = [super init])) {
|
||||
_value = value;
|
||||
_key = key;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (instancetype)nodeWithValue:(id)value key:(id<NSCopying>)key
|
||||
{
|
||||
return [[LRUCacheNode alloc] initWithValue:value key:key];
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"%@ %@", self.value, self.next];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user