]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
5e0de328 | 2 | * Copyright (C) 2009-2012 the libgit2 contributors |
bb742ede VM |
3 | * |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
c4034e63 VM |
7 | #ifndef INCLUDE_vector_h__ |
8 | #define INCLUDE_vector_h__ | |
9 | ||
44908fe7 | 10 | #include "git2/common.h" |
c4034e63 | 11 | |
c4034e63 | 12 | typedef int (*git_vector_cmp)(const void *, const void *); |
c4034e63 VM |
13 | |
14 | typedef struct git_vector { | |
b8457baa | 15 | size_t _alloc_size; |
c4034e63 | 16 | git_vector_cmp _cmp; |
c4034e63 | 17 | void **contents; |
b8457baa | 18 | size_t length; |
86d7e1ca | 19 | int sorted; |
c4034e63 VM |
20 | } git_vector; |
21 | ||
ee1f0b1a RB |
22 | #define GIT_VECTOR_INIT {0} |
23 | ||
b8457baa | 24 | int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp); |
c4034e63 VM |
25 | void git_vector_free(git_vector *v); |
26 | void git_vector_clear(git_vector *v); | |
ec40b7f9 | 27 | int git_vector_dup(git_vector *v, git_vector *src, git_vector_cmp cmp); |
74fa4bfa | 28 | void git_vector_swap(git_vector *a, git_vector *b); |
c4034e63 | 29 | |
41a82592 RB |
30 | void git_vector_sort(git_vector *v); |
31 | ||
bad68c0a | 32 | /** Linear search for matching entry using internal comparison function */ |
86d7e1ca | 33 | int git_vector_search(git_vector *v, const void *entry); |
bad68c0a RB |
34 | |
35 | /** Linear search for matching entry using explicit comparison function */ | |
86d7e1ca VM |
36 | int git_vector_search2(git_vector *v, git_vector_cmp cmp, const void *key); |
37 | ||
bad68c0a RB |
38 | /** |
39 | * Binary search for matching entry using explicit comparison function that | |
40 | * returns position where item would go if not found. | |
41 | */ | |
41a82592 RB |
42 | int git_vector_bsearch3( |
43 | unsigned int *at_pos, git_vector *v, git_vector_cmp cmp, const void *key); | |
86d7e1ca | 44 | |
bad68c0a | 45 | /** Binary search for matching entry using internal comparison function */ |
41a82592 RB |
46 | GIT_INLINE(int) git_vector_bsearch(git_vector *v, const void *key) |
47 | { | |
48 | return git_vector_bsearch3(NULL, v, v->_cmp, key); | |
49 | } | |
50 | ||
bad68c0a | 51 | /** Binary search for matching entry using explicit comparison function */ |
41a82592 RB |
52 | GIT_INLINE(int) git_vector_bsearch2( |
53 | git_vector *v, git_vector_cmp cmp, const void *key) | |
54 | { | |
55 | return git_vector_bsearch3(NULL, v, cmp, key); | |
56 | } | |
c4034e63 | 57 | |
b8457baa | 58 | GIT_INLINE(void *) git_vector_get(git_vector *v, size_t position) |
86194b24 VM |
59 | { |
60 | return (position < v->length) ? v->contents[position] : NULL; | |
61 | } | |
c4034e63 | 62 | |
b8457baa | 63 | GIT_INLINE(const void *) git_vector_get_const(const git_vector *v, size_t position) |
74fa4bfa RB |
64 | { |
65 | return (position < v->length) ? v->contents[position] : NULL; | |
66 | } | |
67 | ||
a48ea31d RB |
68 | #define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL) |
69 | ||
b6c93aef RB |
70 | GIT_INLINE(void *) git_vector_last(git_vector *v) |
71 | { | |
72 | return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL; | |
73 | } | |
74 | ||
bcf21c55 CMN |
75 | #define git_vector_foreach(v, iter, elem) \ |
76 | for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ ) | |
77 | ||
ee1f0b1a RB |
78 | #define git_vector_rforeach(v, iter, elem) \ |
79 | for ((iter) = (v)->length; (iter) > 0 && ((elem) = (v)->contents[(iter)-1], 1); (iter)-- ) | |
80 | ||
c4034e63 | 81 | int git_vector_insert(git_vector *v, void *element); |
bd370b14 RB |
82 | int git_vector_insert_sorted(git_vector *v, void *element, |
83 | int (*on_dup)(void **old, void *new)); | |
c4034e63 | 84 | int git_vector_remove(git_vector *v, unsigned int idx); |
0534641d | 85 | void git_vector_pop(git_vector *v); |
476c42ac | 86 | void git_vector_uniq(git_vector *v); |
f45ec1a0 | 87 | void git_vector_remove_matching(git_vector *v, int (*match)(git_vector *v, size_t idx)); |
bd370b14 | 88 | |
db106d01 RB |
89 | int git_vector_resize_to(git_vector *v, size_t new_length); |
90 | int git_vector_set(void **old, git_vector *v, size_t position, void *value); | |
91 | ||
c4034e63 | 92 | #endif |