vector.h 2.27 KB
Newer Older
Vicent Marti committed
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8 9
#ifndef INCLUDE_vector_h__
#define INCLUDE_vector_h__

10
#include "git2/common.h"
11 12 13 14 15 16 17 18

typedef int (*git_vector_cmp)(const void *, const void *);

typedef struct git_vector {
	unsigned int _alloc_size;
	git_vector_cmp _cmp;
	void **contents;
	unsigned int length;
19
	int sorted;
20 21
} git_vector;

22 23
#define GIT_VECTOR_INIT {0}

24
int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp);
25 26
void git_vector_free(git_vector *v);
void git_vector_clear(git_vector *v);
27
void git_vector_swap(git_vector *a, git_vector *b);
28

29 30
void git_vector_sort(git_vector *v);

31 32 33
int git_vector_search(git_vector *v, const void *entry);
int git_vector_search2(git_vector *v, git_vector_cmp cmp, const void *key);

34 35
int git_vector_bsearch3(
	unsigned int *at_pos, git_vector *v, git_vector_cmp cmp, const void *key);
36

37 38 39 40 41 42 43 44 45 46
GIT_INLINE(int) git_vector_bsearch(git_vector *v, const void *key)
{
	return git_vector_bsearch3(NULL, v, v->_cmp, key);
}

GIT_INLINE(int) git_vector_bsearch2(
	git_vector *v, git_vector_cmp cmp, const void *key)
{
	return git_vector_bsearch3(NULL, v, cmp, key);
}
47

48 49 50 51
GIT_INLINE(void *) git_vector_get(git_vector *v, unsigned int position)
{
	return (position < v->length) ? v->contents[position] : NULL;
}
52

53 54 55 56 57
GIT_INLINE(const void *) git_vector_get_const(const git_vector *v, unsigned int position)
{
	return (position < v->length) ? v->contents[position] : NULL;
}

58 59
#define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL)

60 61 62 63 64
GIT_INLINE(void *) git_vector_last(git_vector *v)
{
	return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL;
}

65 66 67
#define git_vector_foreach(v, iter, elem)	\
	for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ )

68 69 70
#define git_vector_rforeach(v, iter, elem)	\
	for ((iter) = (v)->length; (iter) > 0 && ((elem) = (v)->contents[(iter)-1], 1); (iter)-- )

71
int git_vector_insert(git_vector *v, void *element);
72 73
int git_vector_insert_sorted(git_vector *v, void *element,
	int (*on_dup)(void **old, void *new));
74
int git_vector_remove(git_vector *v, unsigned int idx);
75
void git_vector_pop(git_vector *v);
76
void git_vector_uniq(git_vector *v);
77

78
#endif