+static void
+lookup_table_insert_raw(struct wim_lookup_table *table,
+ struct wim_lookup_table_entry *lte)
+{
+ size_t i = lte->hash_short % table->capacity;
+
+ hlist_add_head(<e->hash_list, &table->array[i]);
+}
+
+static void
+enlarge_lookup_table(struct wim_lookup_table *table)
+{
+ size_t old_capacity, new_capacity;
+ struct hlist_head *old_array, *new_array;
+ struct wim_lookup_table_entry *lte;
+ struct hlist_node *cur, *tmp;
+ size_t i;
+
+ old_capacity = table->capacity;
+ new_capacity = old_capacity * 2;
+ new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
+ if (new_array == NULL)
+ return;
+ old_array = table->array;
+ table->array = new_array;
+ table->capacity = new_capacity;
+
+ for (i = 0; i < old_capacity; i++) {
+ hlist_for_each_entry_safe(lte, cur, tmp, &old_array[i], hash_list) {
+ hlist_del(<e->hash_list);
+ lookup_table_insert_raw(table, lte);
+ }
+ }
+ FREE(old_array);
+}
+
+/* Inserts an entry into the lookup table. */
+void
+lookup_table_insert(struct wim_lookup_table *table,
+ struct wim_lookup_table_entry *lte)
+{
+ lookup_table_insert_raw(table, lte);
+ if (++table->num_entries > table->capacity)
+ enlarge_lookup_table(table);
+}
+
+/* Unlinks a lookup table entry from the table; does not free it. */
+void
+lookup_table_unlink(struct wim_lookup_table *table,
+ struct wim_lookup_table_entry *lte)
+{
+ wimlib_assert(!lte->unhashed);
+ wimlib_assert(table->num_entries != 0);
+
+ hlist_del(<e->hash_list);
+ table->num_entries--;
+}
+
+/* Given a SHA1 message digest, return the corresponding entry in the WIM's
+ * lookup table, or NULL if there is none. */
+struct wim_lookup_table_entry *
+lookup_stream(const struct wim_lookup_table *table, const u8 hash[])
+{
+ size_t i;
+ struct wim_lookup_table_entry *lte;
+ struct hlist_node *pos;
+
+ i = *(size_t*)hash % table->capacity;
+ hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
+ if (hashes_equal(hash, lte->hash))
+ return lte;
+ return NULL;
+}
+