tsort.c 8.26 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
 */
schu committed
7 8

#include "common.h"
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * An array-of-pointers implementation of Python's Timsort
 * Based on code by Christopher Swenson under the MIT license
 *
 * Copyright (c) 2010 Christopher Swenson
 * Copyright (c) 2011 Vicent Marti
 */

#ifndef MAX
#	define MAX(x,y) (((x) > (y) ? (x) : (y)))
#endif

#ifndef MIN
#	define MIN(x,y) (((x) < (y) ? (x) : (y)))
#endif

26
static int binsearch(
27
	void **dst, const void *x, size_t size, git__sort_r_cmp cmp, void *payload)
28 29
{
	int l, c, r;
30
	void *lx, *cx;
31

32 33
	assert(size > 0);

34
	l = 0;
35
	r = (int)size - 1;
36 37 38 39
	c = r >> 1;
	lx = dst[l];

	/* check for beginning conditions */
40
	if (cmp(x, lx, payload) < 0)
41 42
		return 0;

43
	else if (cmp(x, lx, payload) == 0) {
44
		int i = 1;
45
		while (cmp(x, dst[i], payload) == 0)
46 47 48 49 50 51 52
			i++;
		return i;
	}

	/* guaranteed not to be >= rx */
	cx = dst[c];
	while (1) {
53
		const int val = cmp(x, cx, payload);
54 55 56 57 58 59 60 61 62 63
		if (val < 0) {
			if (c - l <= 1) return c;
			r = c;
		} else if (val > 0) {
			if (r - c <= 1) return c + 1;
			l = c;
			lx = cx;
		} else {
			do {
				cx = dst[++c];
64
			} while (cmp(x, cx, payload) == 0);
65 66 67 68 69 70 71
			return c;
		}
		c = l + ((r - l) >> 1);
		cx = dst[c];
	}
}

Vicent Marti committed
72
/* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */
73
static void bisort(
74
	void **dst, size_t start, size_t size, git__sort_r_cmp cmp, void *payload)
75 76
{
	size_t i;
77 78
	void *x;
	int location;
79 80 81 82

	for (i = start; i < size; i++) {
		int j;
		/* If this entry is already correct, just move along */
83
		if (cmp(dst[i - 1], dst[i], payload) <= 0)
84 85 86
			continue;

		/* Else we need to find the right place, shift everything over, and squeeze in */
87
		x = dst[i];
88
		location = binsearch(dst, x, i, cmp, payload);
89
		for (j = (int)i - 1; j >= location; j--) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
			dst[j + 1] = dst[j];
		}
		dst[location] = x;
	}
}


/* timsort implementation, based on timsort.txt */
struct tsort_run {
	ssize_t start;
	ssize_t length;
};

struct tsort_store {
	size_t alloc;
105
	git__sort_r_cmp cmp;
106
	void *payload;
107 108 109
	void **storage;
};

110
static void reverse_elements(void **dst, ssize_t start, ssize_t end)
111 112 113 114 115 116 117 118 119 120 121
{
	while (start < end) {
		void *tmp = dst[start];
		dst[start] = dst[end];
		dst[end] = tmp;

		start++;
		end--;
	}
}

122 123
static ssize_t count_run(
	void **dst, ssize_t start, ssize_t size, struct tsort_store *store)
124 125 126 127 128 129 130
{
	ssize_t curr = start + 2;

	if (size - start == 1)
		return 1;

	if (start >= size - 2) {
131
		if (store->cmp(dst[size - 2], dst[size - 1], store->payload) > 0) {
132 133 134 135 136 137 138 139
			void *tmp = dst[size - 1];
			dst[size - 1] = dst[size - 2];
			dst[size - 2] = tmp;
		}

		return 2;
	}

140 141 142
	if (store->cmp(dst[start], dst[start + 1], store->payload) <= 0) {
		while (curr < size - 1 &&
				store->cmp(dst[curr - 1], dst[curr], store->payload) <= 0)
143 144 145 146
			curr++;

		return curr - start;
	} else {
147 148
		while (curr < size - 1 &&
				store->cmp(dst[curr - 1], dst[curr], store->payload) > 0)
149 150 151 152 153 154 155 156
			curr++;

		/* reverse in-place */
		reverse_elements(dst, start, curr - 1);
		return curr - start;
	}
}

157
static size_t compute_minrun(size_t n)
158 159 160 161 162 163 164 165 166
{
	int r = 0;
	while (n >= 64) {
		r |= n & 1;
		n >>= 1;
	}
	return n + r;
}

167
static int check_invariant(struct tsort_run *stack, ssize_t stack_curr)
168 169 170 171 172
{
	if (stack_curr < 2)
		return 1;

	else if (stack_curr == 2) {
173 174
		const ssize_t A = stack[stack_curr - 2].length;
		const ssize_t B = stack[stack_curr - 1].length;
175 176
		return (A > B);
	} else {
177 178 179
		const ssize_t A = stack[stack_curr - 3].length;
		const ssize_t B = stack[stack_curr - 2].length;
		const ssize_t C = stack[stack_curr - 1].length;
180 181 182 183 184 185 186
		return !((A <= B + C) || (B <= C));
	}
}

static int resize(struct tsort_store *store, size_t new_size)
{
	if (store->alloc < new_size) {
187
		void **tempstore = git__realloc(store->storage, new_size * sizeof(void *));
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

		/**
		 * Do not propagate on OOM; this will abort the sort and
		 * leave the array unsorted, but no error code will be
		 * raised
		 */
		if (tempstore == NULL)
			return -1;

		store->storage = tempstore;
		store->alloc = new_size;
	}

	return 0;
}

204
static void merge(void **dst, const struct tsort_run *stack, ssize_t stack_curr, struct tsort_store *store)
205 206 207 208 209 210 211 212 213 214 215
{
	const ssize_t A = stack[stack_curr - 2].length;
	const ssize_t B = stack[stack_curr - 1].length;
	const ssize_t curr = stack[stack_curr - 2].start;

	void **storage;
	ssize_t i, j, k;

	if (resize(store, MIN(A, B)) < 0)
		return;

Vicent Marti committed
216
	storage = store->storage;
217 218 219 220 221 222 223 224 225

	/* left merge */
	if (A < B) {
		memcpy(storage, &dst[curr], A * sizeof(void *));
		i = 0;
		j = curr + A;

		for (k = curr; k < curr + A + B; k++) {
			if ((i < A) && (j < curr + A + B)) {
226
				if (store->cmp(storage[i], dst[j], store->payload) <= 0)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
					dst[k] = storage[i++];
				else
					dst[k] = dst[j++];
			} else if (i < A) {
				dst[k] = storage[i++];
			} else
				dst[k] = dst[j++];
		}
	} else {
		memcpy(storage, &dst[curr + A], B * sizeof(void *));
		i = B - 1;
		j = curr + A - 1;

		for (k = curr + A + B - 1; k >= curr; k--) {
			if ((i >= 0) && (j >= curr)) {
242
				if (store->cmp(dst[j], storage[i], store->payload) > 0)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
					dst[k] = dst[j--];
				else
					dst[k] = storage[i--];
			} else if (i >= 0)
				dst[k] = storage[i--];
			else
				dst[k] = dst[j--];
		}
	}
}

static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr, struct tsort_store *store, ssize_t size)
{
	ssize_t A, B, C;

	while (1) {
		/* if the stack only has one thing on it, we are done with the collapse */
		if (stack_curr <= 1)
			break;

		/* if this is the last merge, just do it */
		if ((stack_curr == 2) && (stack[0].length + stack[1].length == size)) {
			merge(dst, stack, stack_curr, store);
			stack[0].length += stack[1].length;
			stack_curr--;
			break;
		}

		/* check if the invariant is off for a stack of 2 elements */
		else if ((stack_curr == 2) && (stack[0].length <= stack[1].length)) {
			merge(dst, stack, stack_curr, store);
			stack[0].length += stack[1].length;
			stack_curr--;
			break;
		}
		else if (stack_curr == 2)
			break;

		A = stack[stack_curr - 3].length;
		B = stack[stack_curr - 2].length;
		C = stack[stack_curr - 1].length;

		/* check first invariant */
		if (A <= B + C) {
			if (A < C) {
				merge(dst, stack, stack_curr - 1, store);
				stack[stack_curr - 3].length += stack[stack_curr - 2].length;
				stack[stack_curr - 2] = stack[stack_curr - 1];
				stack_curr--;
			} else {
				merge(dst, stack, stack_curr, store);
				stack[stack_curr - 2].length += stack[stack_curr - 1].length;
				stack_curr--;
			}
		} else if (B <= C) {
			merge(dst, stack, stack_curr, store);
			stack[stack_curr - 2].length += stack[stack_curr - 1].length;
			stack_curr--;
		} else
			break;
	}

	return stack_curr;
}

#define PUSH_NEXT() do {\
	len = count_run(dst, curr, size, store);\
	run = minrun;\
	if (run < minrun) run = minrun;\
	if (run > (ssize_t)size - curr) run = size - curr;\
	if (run > len) {\
314
		bisort(&dst[curr], len, run, cmp, payload);\
315 316
		len = run;\
	}\
317 318
	run_stack[stack_curr].start = curr;\
	run_stack[stack_curr++].length = len;\
319 320 321 322 323 324 325 326 327
	curr += len;\
	if (curr == (ssize_t)size) {\
		/* finish up */ \
		while (stack_curr > 1) { \
			merge(dst, run_stack, stack_curr, store); \
			run_stack[stack_curr - 2].length += run_stack[stack_curr - 1].length; \
			stack_curr--; \
		} \
		if (store->storage != NULL) {\
328
			git__free(store->storage);\
329 330 331 332 333 334 335
			store->storage = NULL;\
		}\
		return;\
	}\
}\
while (0)

336
void git__tsort_r(
337
	void **dst, size_t size, git__sort_r_cmp cmp, void *payload)
338 339 340 341 342 343 344 345 346 347
{
	struct tsort_store _store, *store = &_store;
	struct tsort_run run_stack[128];

	ssize_t stack_curr = 0;
	ssize_t len, run;
	ssize_t curr = 0;
	ssize_t minrun;

	if (size < 64) {
348
		bisort(dst, 1, size, cmp, payload);
349 350 351 352
		return;
	}

	/* compute the minimum run length */
353
	minrun = (ssize_t)compute_minrun(size);
354 355 356 357 358

	/* temporary storage for merges */
	store->alloc = 0;
	store->storage = NULL;
	store->cmp = cmp;
359
	store->payload = payload;
360 361 362 363 364 365 366 367 368 369 370 371 372 373

	PUSH_NEXT();
	PUSH_NEXT();
	PUSH_NEXT();

	while (1) {
		if (!check_invariant(run_stack, stack_curr)) {
			stack_curr = collapse(dst, run_stack, stack_curr, store, size);
			continue;
		}

		PUSH_NEXT();
	}
}
374 375 376 377 378 379 380 381 382 383

static int tsort_r_cmp(const void *a, const void *b, void *payload)
{
	return ((git__tsort_cmp)payload)(a, b);
}

void git__tsort(void **dst, size_t size, git__tsort_cmp cmp)
{
	git__tsort_r(dst, size, tsort_r_cmp, cmp);
}