int read_cache(void) { FILE *fr; LIST_MEMBER *pentry; char Line[LINESIZE + 1]; int result = 0; Line[LINESIZE] = '\0'; fr = fopen(cache_name, "r"); if (!fr) { l2l_dbg(1, "Open %s failed\n", cache_name); return 2; } cache.phead = cache.ptail = NULL; while (fgets(Line, LINESIZE, fr) != NULL) { pentry = cache_entry_create(Line); if (!pentry) { l2l_dbg(2, "** Create entry failed of: %s\n", Line); } else entry_insert(&cache, pentry); } fclose(fr); return result; }
StringRef StringMap::insert(const char* str) { // Hash the string and count the length unsigned int l; hash_t hash = get_hash(str, &l); // Find the entry from the map BaseEntry* b = entries.get() + hash; Entry& e = entry_insert(b, str, l); // If the entry is empty store the word if (!e.ptr) { e.ptr = page.insert(str, l); e.len = l; } // If the word has higher frequency than the fast BaseEntry swap the more // frequent word to be the fast default entry if (b->count < ++e.count) { b->swap(e); } return e.ptr; }