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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| typedef struct lru_node { void *key; void *value; struct lru_node *prev; struct lru_node *next; } lru_node_t;
typedef struct lru_cache { lru_node_t *head; lru_node_t *tail; node_t **hash_table; size_t capacity; size_t size; size_t hash_size; hash_func_t hash; compare_func_t compare; } lru_cache_t;
lru_cache_t* lru_create(size_t capacity, hash_func_t hash, compare_func_t compare) { lru_cache_t *cache = calloc(1, sizeof(lru_cache_t)); if (!cache) return NULL; cache->capacity = capacity; cache->hash_size = capacity * 2; cache->hash = hash; cache->compare = compare; cache->head = calloc(1, sizeof(lru_node_t)); cache->tail = calloc(1, sizeof(lru_node_t)); if (!cache->head || !cache->tail) { lru_destroy(cache); return NULL; } cache->head->next = cache->tail; cache->tail->prev = cache->head; cache->hash_table = calloc(cache->hash_size, sizeof(node_t*)); if (!cache->hash_table) { lru_destroy(cache); return NULL; } return cache; }
static void move_to_head(lru_cache_t *cache, lru_node_t *node) { node->prev->next = node->next; node->next->prev = node->prev; node->next = cache->head->next; node->prev = cache->head; cache->head->next->prev = node; cache->head->next = node; }
static lru_node_t* remove_tail(lru_cache_t *cache) { lru_node_t *last = cache->tail->prev; last->prev->next = cache->tail; cache->tail->prev = last->prev; return last; }
void* lru_get(lru_cache_t *cache, void *key) { if (!cache || !key) return NULL; size_t hash_index = cache->hash(key) % cache->hash_size; node_t *hash_node = cache->hash_table[hash_index]; while (hash_node) { if (cache->compare(hash_node->key, key) == 0) { lru_node_t *lru_node = (lru_node_t*)hash_node->value; move_to_head(cache, lru_node); return lru_node->value; } hash_node = hash_node->next; } return NULL; }
int lru_put(lru_cache_t *cache, void *key, void *value) { if (!cache || !key) return -1; void *existing = lru_get(cache, key); if (existing) { size_t hash_index = cache->hash(key) % cache->hash_size; node_t *hash_node = cache->hash_table[hash_index]; while (hash_node) { if (cache->compare(hash_node->key, key) == 0) { lru_node_t *lru_node = (lru_node_t*)hash_node->value; lru_node->value = value; return 0; } hash_node = hash_node->next; } } lru_node_t *new_lru_node = calloc(1, sizeof(lru_node_t)); if (!new_lru_node) return -1; new_lru_node->key = key; new_lru_node->value = value; size_t hash_index = cache->hash(key) % cache->hash_size; node_t *new_hash_node = calloc(1, sizeof(node_t)); if (!new_hash_node) { free(new_lru_node); return -1; } new_hash_node->key = key; new_hash_node->value = new_lru_node; new_hash_node->next = cache->hash_table[hash_index]; cache->hash_table[hash_index] = new_hash_node; new_lru_node->next = cache->head->next; new_lru_node->prev = cache->head; cache->head->next->prev = new_lru_node; cache->head->next = new_lru_node; cache->size++; if (cache->size > cache->capacity) { lru_node_t *tail_node = remove_tail(cache); free(tail_node); cache->size--; } return 0; }
|