]>
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 { | |
15 | unsigned int _alloc_size; | |
16 | git_vector_cmp _cmp; | |
c4034e63 VM |
17 | void **contents; |
18 | unsigned int length; | |
86d7e1ca | 19 | int sorted; |
c4034e63 VM |
20 | } git_vector; |
21 | ||
ee1f0b1a RB |
22 | #define GIT_VECTOR_INIT {0} |
23 | ||
86d7e1ca | 24 | int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp); |
c4034e63 VM |
25 | void git_vector_free(git_vector *v); |
26 | void git_vector_clear(git_vector *v); | |
74fa4bfa | 27 | void git_vector_swap(git_vector *a, git_vector *b); |
c4034e63 | 28 | |
86d7e1ca VM |
29 | int git_vector_search(git_vector *v, const void *entry); |
30 | int git_vector_search2(git_vector *v, git_vector_cmp cmp, const void *key); | |
31 | ||
32 | int git_vector_bsearch(git_vector *v, const void *entry); | |
33 | int git_vector_bsearch2(git_vector *v, git_vector_cmp cmp, const void *key); | |
34 | ||
c4034e63 VM |
35 | void git_vector_sort(git_vector *v); |
36 | ||
86194b24 VM |
37 | GIT_INLINE(void *) git_vector_get(git_vector *v, unsigned int position) |
38 | { | |
39 | return (position < v->length) ? v->contents[position] : NULL; | |
40 | } | |
c4034e63 | 41 | |
74fa4bfa RB |
42 | GIT_INLINE(const void *) git_vector_get_const(const git_vector *v, unsigned int position) |
43 | { | |
44 | return (position < v->length) ? v->contents[position] : NULL; | |
45 | } | |
46 | ||
b6c93aef RB |
47 | GIT_INLINE(void *) git_vector_last(git_vector *v) |
48 | { | |
49 | return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL; | |
50 | } | |
51 | ||
bcf21c55 CMN |
52 | #define git_vector_foreach(v, iter, elem) \ |
53 | for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ ) | |
54 | ||
ee1f0b1a RB |
55 | #define git_vector_rforeach(v, iter, elem) \ |
56 | for ((iter) = (v)->length; (iter) > 0 && ((elem) = (v)->contents[(iter)-1], 1); (iter)-- ) | |
57 | ||
c4034e63 | 58 | int git_vector_insert(git_vector *v, void *element); |
bd370b14 RB |
59 | int git_vector_insert_sorted(git_vector *v, void *element, |
60 | int (*on_dup)(void **old, void *new)); | |
c4034e63 | 61 | int git_vector_remove(git_vector *v, unsigned int idx); |
0534641d | 62 | void git_vector_pop(git_vector *v); |
476c42ac | 63 | void git_vector_uniq(git_vector *v); |
bd370b14 | 64 | |
c4034e63 | 65 | #endif |