File: MKLRUCache.h

package info (click to toggle)
popplerkit.framework 0.0.20051227svn-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,632 kB
  • sloc: objc: 2,303; cpp: 672; sh: 475; ansic: 55; makefile: 29
file content (107 lines) | stat: -rw-r--r-- 3,591 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2005  Stefan Kleine Stegemann
 *
 * The sources from MissigKit are not released under any particular
 * license. Do with them what ever you want to do. Include them in
 * your projects, use MissingKit standalone or simply ignore it.
 * Whatever you do, keep in mind the this piece of software may
 * have errors and that it is distributed WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 */

#ifndef _H_MKLRUCACHE
#define _H_MKLRUCACHE

#import "MKLinkedList.h"
#import <Foundation/Foundation.h>

/**
 * A cache with a restricted size. When the sum of the sizes of the
 * objects in the cache becomes bigger than the allowed size, objects
 * are removed from the cache until the sum of the size of the remaining
 * objects is smaller or equal than the allowed size. Objects are 
 * selected for removal from the cache using the least recently used
 * strategy.
 *
 * Objects that are stored in an MKLRUCache must implement the formal
 * protocol MKLRUCachable.
 *
 * Internally, the cache uses a linked list to keep track of the
 * object usages. Whenever an object is fetched from the cache, it is
 * put at the end of the list. When the cache needs to remove objects,
 * it selects objects from the beginning of the list. The time required
 * for the cache operations is the time for storing/retrieving objects
 * from an NSDictionary plus a constant amount of time for maitaining
 * the history list.
 */
@interface MKLRUCache : NSObject
{
   unsigned long         maxSize;
   unsigned long         size;
   NSMutableDictionary*  map;
   MKLinkedList*         history;
}

/** Intializes the receiver, a freshly allocated MKLRUCache with the
 *  specified maximum size.  */
- (id) initWithMaxSize: (unsigned long)aMaxSize;

/** Add an object to the cache using the specified key. Neither key nor
 *  object may be nil. Adding an object to the cache marks the object as
 *  recently used. If another object is currently associated with the
 *  specified key this object is replaced by anObject.  */
- (void) putObject: (id)anObject forKey: (id)aKey;

/** Fetch the object for the specified key from the cache. If the object
 *  was found, it is marked as recently used and returned. Returns nil
 *  if the cache doesn't have an object for the key.  */
- (id) objectForKey: (id)aKey;

/** Check if the receiver contains an object that is associated with the
 *  specified key.  */
- (BOOL) containsObjectForKey: (id)aKey;

/** Remove an object from the cache. Returns the object that has been removed
 *  or nil if no object is associated with the specified key.  */
- (id) removeObjectForKey: (id)aKey;

/** Remove all objects from the cache.  */
- (void) clear;

/** Set the maximum size for this cache. If necessary, objects are
 *  removed from the cache until the new maximum size is reached.  */
- (void) setMaximumSize: (long)aMaxSize;

/** Get the maximum size for this cache  */
- (unsigned long) maximumSize;

/** Get the number of objects in the cache.  */
- (unsigned) countObjects;

/** Get the current size of the cache. This is the sum of the
 *  sizes of the objects in the receiver.  */
- (unsigned long) size;

@end


/**
 * Informal Protocol for objects that are stored in a MKLRUCache.
 */
@interface NSObject (MKLRUCachable)

/** Get the size for the receiver in bytes . This is the size that is
 *  considered by MKLRUCaches.  */
- (unsigned long) sizeInLRUCache;

@end


/**
 * Some methods to verify the state of an MKLRUCache.
 */
@interface MKLRUCache (Testing)
@end

#endif