]>
git.proxmox.com Git - libgit2.git/blob - src/vector.h
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_vector_h__
8 #define INCLUDE_vector_h__
10 #include "git2/common.h"
12 typedef int (*git_vector_cmp
)(const void *, const void *);
14 typedef struct git_vector
{
22 #define GIT_VECTOR_INIT {0}
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
);
30 void git_vector_sort(git_vector
*v
);
32 /** Linear search for matching entry using internal comparison function */
33 int git_vector_search(size_t *at_pos
, const git_vector
*v
, const void *entry
);
35 /** Linear search for matching entry using explicit comparison function */
36 int git_vector_search2(size_t *at_pos
, const git_vector
*v
, git_vector_cmp cmp
, const void *key
);
39 * Binary search for matching entry using explicit comparison function that
40 * returns position where item would go if not found.
42 int git_vector_bsearch2(
43 size_t *at_pos
, git_vector
*v
, git_vector_cmp cmp
, const void *key
);
45 /** Binary search for matching entry using internal comparison function */
46 GIT_INLINE(int) git_vector_bsearch(size_t *at_pos
, git_vector
*v
, const void *key
)
48 return git_vector_bsearch2(at_pos
, v
, v
->_cmp
, key
);
51 GIT_INLINE(void *) git_vector_get(const git_vector
*v
, size_t position
)
53 return (position
< v
->length
) ? v
->contents
[position
] : NULL
;
56 #define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL)
58 GIT_INLINE(size_t) git_vector_length(const git_vector
*v
)
63 GIT_INLINE(void *) git_vector_last(const git_vector
*v
)
65 return (v
->length
> 0) ? git_vector_get(v
, v
->length
- 1) : NULL
;
68 #define git_vector_foreach(v, iter, elem) \
69 for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ )
71 #define git_vector_rforeach(v, iter, elem) \
72 for ((iter) = (v)->length - 1; (iter) < SIZE_MAX && ((elem) = (v)->contents[(iter)], 1); (iter)-- )
74 int git_vector_insert(git_vector
*v
, void *element
);
75 int git_vector_insert_sorted(git_vector
*v
, void *element
,
76 int (*on_dup
)(void **old
, void *new));
77 int git_vector_remove(git_vector
*v
, size_t idx
);
78 void git_vector_pop(git_vector
*v
);
79 void git_vector_uniq(git_vector
*v
, void (*git_free_cb
)(void *));
80 void git_vector_remove_matching(
81 git_vector
*v
, int (*match
)(const git_vector
*v
, size_t idx
));
83 int git_vector_resize_to(git_vector
*v
, size_t new_length
);
84 int git_vector_set(void **old
, git_vector
*v
, size_t position
, void *value
);
86 /** Set the comparison function used for sorting the vector */
87 GIT_INLINE(void) git_vector_set_cmp(git_vector
*v
, git_vector_cmp cmp
)