]> git.proxmox.com Git - libgit2.git/blob - src/vector.h
Merge pull request #1204 from arrbee/diff-blob-to-buffer
[libgit2.git] / src / vector.h
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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 */
7 #ifndef INCLUDE_vector_h__
8 #define INCLUDE_vector_h__
9
10 #include "git2/common.h"
11
12 typedef int (*git_vector_cmp)(const void *, const void *);
13
14 typedef struct git_vector {
15 size_t _alloc_size;
16 git_vector_cmp _cmp;
17 void **contents;
18 size_t length;
19 int sorted;
20 } git_vector;
21
22 #define GIT_VECTOR_INIT {0}
23
24 int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp);
25 void git_vector_free(git_vector *v);
26 void git_vector_clear(git_vector *v);
27 int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp);
28 void git_vector_swap(git_vector *a, git_vector *b);
29
30 void git_vector_sort(git_vector *v);
31
32 /** Linear search for matching entry using internal comparison function */
33 int git_vector_search(const git_vector *v, const void *entry);
34
35 /** Linear search for matching entry using explicit comparison function */
36 int git_vector_search2(const git_vector *v, git_vector_cmp cmp, const void *key);
37
38 /**
39 * Binary search for matching entry using explicit comparison function that
40 * returns position where item would go if not found.
41 */
42 int git_vector_bsearch3(
43 size_t *at_pos, git_vector *v, git_vector_cmp cmp, const void *key);
44
45 /** Binary search for matching entry using internal comparison function */
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
51 /** Binary search for matching entry using explicit comparison function */
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 }
57
58 GIT_INLINE(void *) git_vector_get(const git_vector *v, size_t position)
59 {
60 return (position < v->length) ? v->contents[position] : NULL;
61 }
62
63 #define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL)
64
65 GIT_INLINE(void *) git_vector_last(const git_vector *v)
66 {
67 return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL;
68 }
69
70 #define git_vector_foreach(v, iter, elem) \
71 for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ )
72
73 #define git_vector_rforeach(v, iter, elem) \
74 for ((iter) = (v)->length; (iter) > 0 && ((elem) = (v)->contents[(iter)-1], 1); (iter)-- )
75
76 int git_vector_insert(git_vector *v, void *element);
77 int git_vector_insert_sorted(git_vector *v, void *element,
78 int (*on_dup)(void **old, void *new));
79 int git_vector_remove(git_vector *v, size_t idx);
80 void git_vector_pop(git_vector *v);
81 void git_vector_uniq(git_vector *v);
82 void git_vector_remove_matching(
83 git_vector *v, int (*match)(const git_vector *v, size_t idx));
84
85 int git_vector_resize_to(git_vector *v, size_t new_length);
86 int git_vector_set(void **old, git_vector *v, size_t position, void *value);
87
88 #endif