diff options
author | Grigori Goronzy <greg@blackbox> | 2011-06-07 17:03:30 +0200 |
---|---|---|
committer | Grigori Goronzy <greg@blackbox> | 2011-06-07 17:10:30 +0200 |
commit | 988166104ed0cc6c27edee8ca60fbd549369d13c (patch) | |
tree | 092745a7075f17b70331ccac082426560d8e09e9 /libass/ass_cache.h | |
parent | 07ce715629e3b5b39e4a4def724d649222f53f2f (diff) | |
download | libass-988166104ed0cc6c27edee8ca60fbd549369d13c.tar.bz2 libass-988166104ed0cc6c27edee8ca60fbd549369d13c.tar.xz |
Much improved cache/hashmap implementation
- less code, cleaner
- decoupled from ASS_Library
- better data encapsulation
- simpler interface
- avoids a nasty hack
Diffstat (limited to 'libass/ass_cache.h')
-rw-r--r-- | libass/ass_cache.h | 88 |
1 files changed, 21 insertions, 67 deletions
diff --git a/libass/ass_cache.h b/libass/ass_cache.h index 472bf359..01cf4ad5 100644 --- a/libass/ass_cache.h +++ b/libass/ass_cache.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> + * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx> * * This file is part of libass. * @@ -23,49 +24,9 @@ #include "ass_font.h" #include "ass_bitmap.h" -typedef void (*HashmapItemDtor) (void *key, size_t key_size, - void *value, size_t value_size); -typedef int (*HashmapKeyCompare) (void *key1, void *key2, - size_t key_size); -typedef unsigned (*HashmapHash) (void *key, size_t key_size); +typedef struct cache Cache; -typedef struct hashmap_item { - void *key; - void *value; - struct hashmap_item *next; -} HashmapItem; -typedef HashmapItem *hashmap_item_p; - -typedef struct { - int nbuckets; - size_t key_size, value_size; - hashmap_item_p *root; - HashmapItemDtor item_dtor; // a destructor for hashmap key/value pairs - HashmapKeyCompare key_compare; - HashmapHash hash; - size_t cache_size; - // stats - int hit_count; - int miss_count; - int count; - ASS_Library *library; -} Hashmap; - -Hashmap *hashmap_init(ASS_Library *library, size_t key_size, - size_t value_size, int nbuckets, - HashmapItemDtor item_dtor, - HashmapKeyCompare key_compare, - HashmapHash hash); -void hashmap_done(Hashmap *map); -void *hashmap_insert(Hashmap *map, void *key, void *value); -void *hashmap_find(Hashmap *map, void *key); - -Hashmap *ass_font_cache_init(ASS_Library *library); -ASS_Font *ass_font_cache_find(Hashmap *, ASS_FontDesc *desc); -void *ass_font_cache_add(Hashmap *, ASS_Font *font); -void ass_font_cache_done(Hashmap *); - -// Create definitions for bitmap_hash_key and glyph_hash_key +// Create definitions for bitmap, glyph and composite hash keys #define CREATE_STRUCT_DEFINITIONS #include "ass_cache_template.h" @@ -75,29 +36,11 @@ typedef struct { Bitmap *bm_s; } BitmapHashValue; -Hashmap *ass_bitmap_cache_init(ASS_Library *library); -void *cache_add_bitmap(Hashmap *, BitmapHashKey *key, - BitmapHashValue *val); -BitmapHashValue *cache_find_bitmap(Hashmap *bitmap_cache, - BitmapHashKey *key); -Hashmap *ass_bitmap_cache_reset(Hashmap *bitmap_cache); -void ass_bitmap_cache_done(Hashmap *bitmap_cache); - - typedef struct { unsigned char *a; unsigned char *b; } CompositeHashValue; -Hashmap *ass_composite_cache_init(ASS_Library *library); -void *cache_add_composite(Hashmap *, CompositeHashKey *key, - CompositeHashValue *val); -CompositeHashValue *cache_find_composite(Hashmap *composite_cache, - CompositeHashKey *key); -Hashmap *ass_composite_cache_reset(Hashmap *composite_cache); -void ass_composite_cache_done(Hashmap *composite_cache); - - typedef struct { FT_Glyph glyph; FT_Glyph outline_glyph; @@ -106,12 +49,23 @@ typedef struct { int asc, desc; // ascender/descender of a drawing } GlyphHashValue; -Hashmap *ass_glyph_cache_init(ASS_Library *library); -void *cache_add_glyph(Hashmap *, GlyphHashKey *key, - GlyphHashValue *val); -GlyphHashValue *cache_find_glyph(Hashmap *glyph_cache, - GlyphHashKey *key); -Hashmap *ass_glyph_cache_reset(Hashmap *glyph_cache); -void ass_glyph_cache_done(Hashmap *glyph_cache); +// Type-specific function pointers +typedef unsigned(*HashFunction)(void *key, size_t key_size); +typedef size_t(*ItemSize)(void *value, size_t value_size); +typedef unsigned(*HashCompare)(void *a, void *b, size_t key_size); +typedef void(*CacheItemDestructor)(void *key, void *value); + +Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func, + CacheItemDestructor destruct_func, ItemSize size_func, + size_t key_size, size_t value_size); +void *ass_cache_put(Cache *cache, void *key, void *value); +void *ass_cache_get(Cache *cache, void *key); +size_t ass_cache_empty(Cache *cache, size_t max_size); +char *ass_cache_stats(Cache *cache); +void ass_cache_done(Cache *cache); +Cache *ass_font_cache_create(void); +Cache *ass_glyph_cache_create(void); +Cache *ass_bitmap_cache_create(void); +Cache *ass_composite_cache_create(void); #endif /* LIBASS_CACHE_H */ |