]> git.proxmox.com Git - libgit2.git/blame - src/vector.h
Update branching information in d/gbp.conf
[libgit2.git] / src / vector.h
CommitLineData
bb742ede 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
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
72556cc6 10#include "common.h"
c4034e63 11
c4034e63 12typedef int (*git_vector_cmp)(const void *, const void *);
c4034e63 13
882c7742
RB
14enum {
15 GIT_VECTOR_SORTED = (1u << 0),
16 GIT_VECTOR_FLAG_MAX = (1u << 1),
17};
18
c4034e63 19typedef struct git_vector {
b8457baa 20 size_t _alloc_size;
c4034e63 21 git_vector_cmp _cmp;
c4034e63 22 void **contents;
b8457baa 23 size_t length;
882c7742 24 uint32_t flags;
c4034e63
VM
25} git_vector;
26
ee1f0b1a
RB
27#define GIT_VECTOR_INIT {0}
28
b8457baa 29int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp);
c4034e63 30void git_vector_free(git_vector *v);
9cfce273 31void git_vector_free_deep(git_vector *v); /* free each entry and self */
c4034e63 32void git_vector_clear(git_vector *v);
16248ee2 33int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp);
74fa4bfa 34void git_vector_swap(git_vector *a, git_vector *b);
d7d46cfb 35int git_vector_size_hint(git_vector *v, size_t size_hint);
c4034e63 36
25e0b157
RB
37void **git_vector_detach(size_t *size, size_t *asize, git_vector *v);
38
41a82592
RB
39void git_vector_sort(git_vector *v);
40
bad68c0a 41/** Linear search for matching entry using internal comparison function */
11d9f6b3 42int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry);
bad68c0a
RB
43
44/** Linear search for matching entry using explicit comparison function */
11d9f6b3 45int git_vector_search2(size_t *at_pos, const git_vector *v, git_vector_cmp cmp, const void *key);
86d7e1ca 46
bad68c0a
RB
47/**
48 * Binary search for matching entry using explicit comparison function that
49 * returns position where item would go if not found.
50 */
11d9f6b3 51int git_vector_bsearch2(
16248ee2 52 size_t *at_pos, git_vector *v, git_vector_cmp cmp, const void *key);
86d7e1ca 53
bad68c0a 54/** Binary search for matching entry using internal comparison function */
11d9f6b3 55GIT_INLINE(int) git_vector_bsearch(size_t *at_pos, git_vector *v, const void *key)
41a82592 56{
11d9f6b3 57 return git_vector_bsearch2(at_pos, v, v->_cmp, key);
41a82592 58}
c4034e63 59
54b2a37a 60GIT_INLINE(void *) git_vector_get(const git_vector *v, size_t position)
86194b24
VM
61{
62 return (position < v->length) ? v->contents[position] : NULL;
63}
c4034e63 64
a48ea31d
RB
65#define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL)
66
0b7cdc02
RB
67GIT_INLINE(size_t) git_vector_length(const git_vector *v)
68{
69 return v->length;
70}
71
16248ee2 72GIT_INLINE(void *) git_vector_last(const git_vector *v)
b6c93aef
RB
73{
74 return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL;
75}
76
bcf21c55
CMN
77#define git_vector_foreach(v, iter, elem) \
78 for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ )
79
ee1f0b1a 80#define git_vector_rforeach(v, iter, elem) \
df93a681 81 for ((iter) = (v)->length - 1; (iter) < SIZE_MAX && ((elem) = (v)->contents[(iter)], 1); (iter)-- )
ee1f0b1a 82
c4034e63 83int git_vector_insert(git_vector *v, void *element);
bd370b14
RB
84int git_vector_insert_sorted(git_vector *v, void *element,
85 int (*on_dup)(void **old, void *new));
16248ee2 86int git_vector_remove(git_vector *v, size_t idx);
0534641d 87void git_vector_pop(git_vector *v);
191adce8 88void git_vector_uniq(git_vector *v, void (*git_free_cb)(void *));
c67fd4c9 89
16248ee2 90void git_vector_remove_matching(
c67fd4c9
RB
91 git_vector *v,
92 int (*match)(const git_vector *v, size_t idx, void *payload),
93 void *payload);
bd370b14 94
db106d01 95int git_vector_resize_to(git_vector *v, size_t new_length);
53571f2f
ET
96int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len);
97int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len);
7cb904ba 98
db106d01
RB
99int git_vector_set(void **old, git_vector *v, size_t position, void *value);
100
882c7742
RB
101/** Check if vector is sorted */
102#define git_vector_is_sorted(V) (((V)->flags & GIT_VECTOR_SORTED) != 0)
103
104/** Directly set sorted state of vector */
105#define git_vector_set_sorted(V,S) do { \
106 (V)->flags = (S) ? ((V)->flags | GIT_VECTOR_SORTED) : \
107 ((V)->flags & ~GIT_VECTOR_SORTED); } while (0)
108
22b6b82f
RB
109/** Set the comparison function used for sorting the vector */
110GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp)
111{
112 if (cmp != v->_cmp) {
113 v->_cmp = cmp;
882c7742 114 git_vector_set_sorted(v, 0);
22b6b82f
RB
115 }
116}
117
c67fd4c9
RB
118/* Just use this in tests, not for realz. returns -1 if not sorted */
119int git_vector_verify_sorted(const git_vector *v);
120
0bd43371
CMN
121/**
122 * Reverse the vector in-place.
123 */
124void git_vector_reverse(git_vector *v);
125
c4034e63 126#endif