diff options
| author | Vicent Marti <tanoku@gmail.com> | 2011-02-28 12:46:13 +0200 |
|---|---|---|
| committer | Vicent Marti <tanoku@gmail.com> | 2011-03-03 20:23:52 +0200 |
| commit | 86d7e1ca6f54161a9e4d1ebe7a2f8e4802dc9639 (patch) | |
| tree | 88d9d0fc4a7dbeec37f863b47200c0c55e8ffcf4 /src/index.c | |
| parent | 5de079b86dcf8744f71fa3d0bb496a2cf9b20c0d (diff) | |
| download | libgit2-86d7e1ca6f54161a9e4d1ebe7a2f8e4802dc9639.tar.gz | |
Fix searching in git_vector
We now store only one sorting callback that does entry comparison. This
is used when sorting the entries using a quicksort, and when looking for
a specific entry with the new search methods.
The following search methods now exist:
git_vector_search(vector, entry)
git_vector_search2(vector, custom_search_callback, key)
git_vector_bsearch(vector, entry)
git_vector_bsearch2(vector, custom_search_callback, key)
The sorting state of the vector is now stored internally.
Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src/index.c')
| -rw-r--r-- | src/index.c | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/src/index.c b/src/index.c index e83d11583..46bbafd3f 100644 --- a/src/index.c +++ b/src/index.c @@ -141,10 +141,7 @@ static int index_initialize(git_index **index_out, git_repository *owner, const index->repository = owner; - git_vector_init(&index->entries, 32, index_cmp, index_srch); - - /* the index is empty; the index is sorted */ - index->sorted = 1; + git_vector_init(&index->entries, 32, index_cmp); /* Check if index file is stored on disk already */ if (gitfo_exists(index->index_file_path) == 0) @@ -209,7 +206,6 @@ void git_index_clear(git_index *index) git_vector_clear(&index->entries); index->last_modified = 0; - index->sorted = 1; free_tree(index->tree); index->tree = NULL; @@ -259,8 +255,7 @@ int git_index_write(git_index *index) struct stat indexst; int error; - if (!index->sorted) - sort_index(index); + sort_index(index); if ((error = git_filebuf_open(&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < GIT_SUCCESS) return error; @@ -340,10 +335,7 @@ int git_index_add(git_index *index, const char *rel_path, int stage) void sort_index(git_index *index) { - if (index->sorted == 0) { - git_vector_sort(&index->entries); - index->sorted = 1; - } + git_vector_sort(&index->entries); } int git_index_insert(git_index *index, const git_index_entry *source_entry) @@ -388,8 +380,6 @@ int git_index_insert(git_index *index, const git_index_entry *source_entry) if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS) return GIT_ENOMEM; - index->sorted = 0; - /* if a previous entry exists, replace it */ } else { git_index_entry **entry_array = (git_index_entry **)index->entries.contents; @@ -413,7 +403,7 @@ int git_index_remove(git_index *index, int position) int git_index_find(git_index *index, const char *path) { sort_index(index); - return git_vector_search(&index->entries, path); + return git_vector_bsearch2(&index->entries, index_srch, path); } static git_index_tree *read_tree_internal( @@ -678,6 +668,9 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) #undef seek_forward + /* force sorting in the vector: the entries are + * assured to be sorted on the index */ + index->entries.sorted = 1; return GIT_SUCCESS; } |
