Blame src/vector.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_vector_h__
Packit Service 20376f
#define INCLUDE_vector_h__
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
typedef int (*git_vector_cmp)(const void *, const void *);
Packit Service 20376f
Packit Service 20376f
enum {
Packit Service 20376f
	GIT_VECTOR_SORTED = (1u << 0),
Packit Service 20376f
	GIT_VECTOR_FLAG_MAX = (1u << 1),
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
typedef struct git_vector {
Packit Service 20376f
	size_t _alloc_size;
Packit Service 20376f
	git_vector_cmp _cmp;
Packit Service 20376f
	void **contents;
Packit Service 20376f
	size_t length;
Packit Service 20376f
	uint32_t flags;
Packit Service 20376f
} git_vector;
Packit Service 20376f
Packit Service 20376f
#define GIT_VECTOR_INIT {0}
Packit Service 20376f
Packit Service 20376f
int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp);
Packit Service 20376f
void git_vector_free(git_vector *v);
Packit Service 20376f
void git_vector_free_deep(git_vector *v); /* free each entry and self */
Packit Service 20376f
void git_vector_clear(git_vector *v);
Packit Service 20376f
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp);
Packit Service 20376f
void git_vector_swap(git_vector *a, git_vector *b);
Packit Service 20376f
int git_vector_size_hint(git_vector *v, size_t size_hint);
Packit Service 20376f
Packit Service 20376f
void **git_vector_detach(size_t *size, size_t *asize, git_vector *v);
Packit Service 20376f
Packit Service 20376f
void git_vector_sort(git_vector *v);
Packit Service 20376f
Packit Service 20376f
/** Linear search for matching entry using internal comparison function */
Packit Service 20376f
int git_vector_search(size_t *at_pos, const git_vector *v, const void *entry);
Packit Service 20376f
Packit Service 20376f
/** Linear search for matching entry using explicit comparison function */
Packit Service 20376f
int git_vector_search2(size_t *at_pos, const git_vector *v, git_vector_cmp cmp, const void *key);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Binary search for matching entry using explicit comparison function that
Packit Service 20376f
 * returns position where item would go if not found.
Packit Service 20376f
 */
Packit Service 20376f
int git_vector_bsearch2(
Packit Service 20376f
	size_t *at_pos, git_vector *v, git_vector_cmp cmp, const void *key);
Packit Service 20376f
Packit Service 20376f
/** Binary search for matching entry using internal comparison function */
Packit Service 20376f
GIT_INLINE(int) git_vector_bsearch(size_t *at_pos, git_vector *v, const void *key)
Packit Service 20376f
{
Packit Service 20376f
	return git_vector_bsearch2(at_pos, v, v->_cmp, key);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git_vector_get(const git_vector *v, size_t position)
Packit Service 20376f
{
Packit Service 20376f
	return (position < v->length) ? v->contents[position] : NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL)
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(size_t) git_vector_length(const git_vector *v)
Packit Service 20376f
{
Packit Service 20376f
	return v->length;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void *) git_vector_last(const git_vector *v)
Packit Service 20376f
{
Packit Service 20376f
	return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define git_vector_foreach(v, iter, elem)	\
Packit Service 20376f
	for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ )
Packit Service 20376f
Packit Service 20376f
#define git_vector_rforeach(v, iter, elem)	\
Packit Service 20376f
	for ((iter) = (v)->length - 1; (iter) < SIZE_MAX && ((elem) = (v)->contents[(iter)], 1); (iter)-- )
Packit Service 20376f
Packit Service 20376f
int git_vector_insert(git_vector *v, void *element);
Packit Service 20376f
int git_vector_insert_sorted(git_vector *v, void *element,
Packit Service 20376f
	int (*on_dup)(void **old, void *new));
Packit Service 20376f
int git_vector_remove(git_vector *v, size_t idx);
Packit Service 20376f
void git_vector_pop(git_vector *v);
Packit Service 20376f
void git_vector_uniq(git_vector *v, void  (*git_free_cb)(void *));
Packit Service 20376f
Packit Service 20376f
void git_vector_remove_matching(
Packit Service 20376f
	git_vector *v,
Packit Service 20376f
	int (*match)(const git_vector *v, size_t idx, void *payload),
Packit Service 20376f
	void *payload);
Packit Service 20376f
Packit Service 20376f
int git_vector_resize_to(git_vector *v, size_t new_length);
Packit Service 20376f
int git_vector_insert_null(git_vector *v, size_t idx, size_t insert_len);
Packit Service 20376f
int git_vector_remove_range(git_vector *v, size_t idx, size_t remove_len);
Packit Service 20376f
Packit Service 20376f
int git_vector_set(void **old, git_vector *v, size_t position, void *value);
Packit Service 20376f
Packit Service 20376f
/** Check if vector is sorted */
Packit Service 20376f
#define git_vector_is_sorted(V) (((V)->flags & GIT_VECTOR_SORTED) != 0)
Packit Service 20376f
Packit Service 20376f
/** Directly set sorted state of vector */
Packit Service 20376f
#define git_vector_set_sorted(V,S) do { \
Packit Service 20376f
	(V)->flags = (S) ? ((V)->flags | GIT_VECTOR_SORTED) : \
Packit Service 20376f
		((V)->flags & ~GIT_VECTOR_SORTED); } while (0)
Packit Service 20376f
Packit Service 20376f
/** Set the comparison function used for sorting the vector */
Packit Service 20376f
GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp)
Packit Service 20376f
{
Packit Service 20376f
	if (cmp != v->_cmp) {
Packit Service 20376f
		v->_cmp = cmp;
Packit Service 20376f
		git_vector_set_sorted(v, 0);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* Just use this in tests, not for realz. returns -1 if not sorted */
Packit Service 20376f
int git_vector_verify_sorted(const git_vector *v);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Reverse the vector in-place.
Packit Service 20376f
 */
Packit Service 20376f
void git_vector_reverse(git_vector *v);
Packit Service 20376f
Packit Service 20376f
#endif