|
| 1 | +/* |
| 2 | + * This file is free software; you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License, version 2, |
| 4 | + * as published by the Free Software Foundation. |
| 5 | + * |
| 6 | + * In addition to the permissions in the GNU General Public License, |
| 7 | + * the authors give you unlimited permission to link the compiled |
| 8 | + * version of this file into combinations with other programs, |
| 9 | + * and to distribute those combinations without any restriction |
| 10 | + * coming from the use of this file. (The General Public License |
| 11 | + * restrictions do apply in other respects; for example, they cover |
| 12 | + * modification of the file, and distribution when not linked into |
| 13 | + * a combined executable.) |
| 14 | + * |
| 15 | + * This file is distributed in the hope that it will be useful, but |
| 16 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + * General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; see the file COPYING. If not, write to |
| 22 | + * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
| 23 | + * Boston, MA 02110-1301, USA. |
| 24 | + */ |
| 25 | + |
| 26 | +#include "common.h" |
| 27 | +#include "revobject.h" |
| 28 | + |
| 29 | +const float max_load_factor = 0.65; |
| 30 | + |
| 31 | +unsigned int git_revpool_table__hash(const git_oid *id) |
| 32 | +{ |
| 33 | + const unsigned int m = 0x5bd1e995; |
| 34 | + const int r = 24; |
| 35 | + |
| 36 | + unsigned int h = 0xA8A3D5 ^ (unsigned int)id; |
| 37 | + int i; |
| 38 | + |
| 39 | + for (i = 0; i < GIT_OID_RAWSZ / 4; ++i) |
| 40 | + { |
| 41 | + unsigned int k = ((unsigned int *)id->id)[i]; |
| 42 | + |
| 43 | + k *= m; |
| 44 | + k ^= k >> r; |
| 45 | + k *= m; |
| 46 | + h *= m; |
| 47 | + h ^= k; |
| 48 | + } |
| 49 | + |
| 50 | + h ^= h >> 13; |
| 51 | + h *= m; |
| 52 | + h ^= h >> 15; |
| 53 | + |
| 54 | + return h; |
| 55 | +} |
| 56 | + |
| 57 | +git_revpool_table *git_revpool_table_create(unsigned int min_size) |
| 58 | +{ |
| 59 | + git_revpool_table *table; |
| 60 | + |
| 61 | + table = git__malloc(sizeof(table)); |
| 62 | + |
| 63 | + if (table == NULL) |
| 64 | + return NULL; |
| 65 | + |
| 66 | + // round up size to closest power of 2 |
| 67 | + min_size--; |
| 68 | + min_size |= min_size >> 1; |
| 69 | + min_size |= min_size >> 2; |
| 70 | + min_size |= min_size >> 4; |
| 71 | + min_size |= min_size >> 8; |
| 72 | + min_size |= min_size >> 16; |
| 73 | + |
| 74 | + table->size_mask = min_size; |
| 75 | + table->count = 0; |
| 76 | + table->max_count = (min_size + 1) * max_load_factor; |
| 77 | + |
| 78 | + table->nodes = git__malloc((min_size + 1) * sizeof(git_revpool_node *)); |
| 79 | + |
| 80 | + if (table->nodes == NULL) |
| 81 | + { |
| 82 | + free(table); |
| 83 | + return NULL; |
| 84 | + } |
| 85 | + |
| 86 | + memset(table->nodes, 0x0, (min_size + 1) * sizeof(git_revpool_node *)); |
| 87 | + |
| 88 | + return table; |
| 89 | +} |
| 90 | + |
| 91 | +int git_revpool_table_insert(git_revpool_table *table, git_revpool_object *object) |
| 92 | +{ |
| 93 | + git_revpool_node *node; |
| 94 | + unsigned int index, hash; |
| 95 | + |
| 96 | + if (table->count + 1 > table->max_count) |
| 97 | + git_revpool_table_resize(table); |
| 98 | + |
| 99 | + node = git__malloc(sizeof(git_revpool_node)); |
| 100 | + if (node == NULL) |
| 101 | + return -1; |
| 102 | + |
| 103 | + hash = git_revpool_table__hash(&object->id); |
| 104 | + index = (hash & table->size_mask); |
| 105 | + |
| 106 | + node->object = object; |
| 107 | + node->hash = hash; |
| 108 | + node->next = table->nodes[index]; |
| 109 | + |
| 110 | + table->nodes[index] = node; |
| 111 | + table->count++; |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
| 115 | + |
| 116 | +git_revpool_object *git_revpool_table_lookup(git_revpool_table *table, const git_oid *id) |
| 117 | +{ |
| 118 | + git_revpool_node *node; |
| 119 | + unsigned int index, hash; |
| 120 | + |
| 121 | + hash = git_revpool_table__hash(id); |
| 122 | + index = (hash & table->size_mask); |
| 123 | + node = table->nodes[index]; |
| 124 | + |
| 125 | + while (node != NULL) |
| 126 | + { |
| 127 | + if (node->hash == hash && git_oid_cmp(id, &node->object->id) == 0) |
| 128 | + return node->object; |
| 129 | + |
| 130 | + node = node->next; |
| 131 | + } |
| 132 | + |
| 133 | + return NULL; |
| 134 | +} |
| 135 | + |
| 136 | +void git_revpool_table_resize(git_revpool_table *table) |
| 137 | +{ |
| 138 | + git_revpool_node **new_nodes; |
| 139 | + unsigned int new_size, i; |
| 140 | + |
| 141 | + new_size = (table->size_mask + 1) * 2; |
| 142 | + |
| 143 | + new_nodes = git__malloc(new_size * sizeof(git_revpool_node *)); |
| 144 | + if (new_nodes == NULL) |
| 145 | + return; |
| 146 | + |
| 147 | + memset(new_nodes, 0x0, new_size * sizeof(git_revpool_node *)); |
| 148 | + |
| 149 | + for (i = 0; i <= table->size_mask; ++i) |
| 150 | + { |
| 151 | + git_revpool_node *n; |
| 152 | + unsigned int index; |
| 153 | + |
| 154 | + while ((n = table->nodes[i]) != NULL) |
| 155 | + { |
| 156 | + table->nodes[i] = n->next; |
| 157 | + index = n->hash & (new_size - 1); |
| 158 | + n->next = new_nodes[index]; |
| 159 | + new_nodes[index] = n; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + free(table->nodes); |
| 164 | + table->nodes = new_nodes; |
| 165 | + table->size_mask = (new_size - 1); |
| 166 | + table->max_count = new_size * max_load_factor; |
| 167 | +} |
0 commit comments