line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright (C) the libgit2 contributors. All rights reserved. |
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 "common.h" |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
typedef int (*git_vector_cmp)(const void *, const void *); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
enum { |
15
|
|
|
|
|
|
|
GIT_VECTOR_SORTED = (1u << 0), |
16
|
|
|
|
|
|
|
GIT_VECTOR_FLAG_MAX = (1u << 1), |
17
|
|
|
|
|
|
|
}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
typedef struct git_vector { |
20
|
|
|
|
|
|
|
size_t _alloc_size; |
21
|
|
|
|
|
|
|
git_vector_cmp _cmp; |
22
|
|
|
|
|
|
|
void **contents; |
23
|
|
|
|
|
|
|
size_t length; |
24
|
|
|
|
|
|
|
uint32_t flags; |
25
|
|
|
|
|
|
|
} git_vector; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#define GIT_VECTOR_INIT {0} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp); |
30
|
|
|
|
|
|
|
void git_vector_free(git_vector *v); |
31
|
|
|
|
|
|
|
void git_vector_free_deep(git_vector *v); /* free each entry and self */ |
32
|
|
|
|
|
|
|
void git_vector_clear(git_vector *v); |
33
|
|
|
|
|
|
|
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp); |
34
|
|
|
|
|
|
|
void git_vector_swap(git_vector *a, git_vector *b); |
35
|
|
|
|
|
|
|
int git_vector_size_hint(git_vector *v, size_t size_hint); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
void **git_vector_detach(size_t *size, size_t *asize, git_vector *v); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
void git_vector_sort(git_vector *v); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
/** Linear search for matching entry using internal comparison function */ |
42
|
|
|
|
|
|
|
int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
/** Linear search for matching entry using explicit comparison function */ |
45
|
|
|
|
|
|
|
int git_vector_search2(size_t *at_pos, const git_vector *v, git_vector_cmp cmp, const void *key); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
/** |
48
|
|
|
|
|
|
|
* Binary search for matching entry using explicit comparison function that |
49
|
|
|
|
|
|
|
* returns position where item would go if not found. |
50
|
|
|
|
|
|
|
*/ |
51
|
|
|
|
|
|
|
int git_vector_bsearch2( |
52
|
|
|
|
|
|
|
size_t *at_pos, git_vector *v, git_vector_cmp cmp, const void *key); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
/** Binary search for matching entry using internal comparison function */ |
55
|
|
|
|
|
|
|
GIT_INLINE(int) git_vector_bsearch(size_t *at_pos, git_vector *v, const void *key) |
56
|
|
|
|
|
|
|
{ |
57
|
|
|
|
|
|
|
return git_vector_bsearch2(at_pos, v, v->_cmp, key); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
GIT_INLINE(void *) git_vector_get(const git_vector *v, size_t position) |
61
|
|
|
|
|
|
|
{ |
62
|
|
|
|
|
|
|
return (position < v->length) ? v->contents[position] : NULL; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
#define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL) |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
GIT_INLINE(size_t) git_vector_length(const git_vector *v) |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
|
|
|
return v->length; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
GIT_INLINE(void *) git_vector_last(const git_vector *v) |
73
|
|
|
|
|
|
|
{ |
74
|
|
|
|
|
|
|
return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
#define git_vector_foreach(v, iter, elem) \ |
78
|
|
|
|
|
|
|
for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ ) |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
#define git_vector_rforeach(v, iter, elem) \ |
81
|
|
|
|
|
|
|
for ((iter) = (v)->length - 1; (iter) < SIZE_MAX && ((elem) = (v)->contents[(iter)], 1); (iter)-- ) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
int git_vector_insert(git_vector *v, void *element); |
84
|
|
|
|
|
|
|
int git_vector_insert_sorted(git_vector *v, void *element, |
85
|
|
|
|
|
|
|
int (*on_dup)(void **old, void *new)); |
86
|
|
|
|
|
|
|
int git_vector_remove(git_vector *v, size_t idx); |
87
|
|
|
|
|
|
|
void git_vector_pop(git_vector *v); |
88
|
|
|
|
|
|
|
void git_vector_uniq(git_vector *v, void (*git_free_cb)(void *)); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
void git_vector_remove_matching( |
91
|
|
|
|
|
|
|
git_vector *v, |
92
|
|
|
|
|
|
|
int (*match)(const git_vector *v, size_t idx, void *payload), |
93
|
|
|
|
|
|
|
void *payload); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
int git_vector_resize_to(git_vector *v, size_t new_length); |
96
|
|
|
|
|
|
|
int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len); |
97
|
|
|
|
|
|
|
int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
int git_vector_set(void **old, git_vector *v, size_t position, void *value); |
100
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
/** Set the comparison function used for sorting the vector */ |
110
|
|
|
|
|
|
|
GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp) |
111
|
|
|
|
|
|
|
{ |
112
|
|
|
|
|
|
|
if (cmp != v->_cmp) { |
113
|
|
|
|
|
|
|
v->_cmp = cmp; |
114
|
|
|
|
|
|
|
git_vector_set_sorted(v, 0); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
/* Just use this in tests, not for realz. returns -1 if not sorted */ |
119
|
|
|
|
|
|
|
int git_vector_verify_sorted(const git_vector *v); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
/** |
122
|
|
|
|
|
|
|
* Reverse the vector in-place. |
123
|
|
|
|
|
|
|
*/ |
124
|
|
|
|
|
|
|
void git_vector_reverse(git_vector *v); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#endif |