<commit_before>/*
 * This file is part of Kotaka, a mud library for DGD
 * http://github.com/shentino/kotaka
 *
 * Copyright (C) 2013, 2014  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <kotaka/paths/bigstruct.h>
#include <type.h>

private void aswap(mixed arr, int a, int b)
{
	mixed tmp;

	if (typeof(arr) == T_ARRAY) {
		tmp = arr[a];
		arr[a] = arr[b];
		arr[b] = tmp;
	} else {
		tmp = arr->query_element(a);
		arr->set_element(a, arr->query_element(b));
		arr->set_element(b, tmp);
	}
}

void qsort(mixed arr, int begin, int end, varargs string compfunc)
{
	int type;

	type = typeof(arr);

	while (begin < end) {
		int low, mid, high, sign;
		mixed pivot;

		low = begin;
		mid = (begin + end) / 2;
		high = end - 1;

		pivot = (type == T_ARRAY) ? arr[mid] : arr->query_element(mid);
		aswap(arr, mid, high);

		while (low < high) {
			mixed le;

			le = (type == T_ARRAY) ? arr[low] : arr->query_element(low);

			if (compfunc) {
				sign = call_other(previous_object(), compfunc, le, pivot);
			} else if (le > pivot) {
				sign = 1;
			} else {
				sign = 0;
			}
			if (sign > 0) {
				aswap(arr, low, --high);
			} else {
				low++;
			}
		}

		mid = low;
		aswap(arr, end - 1, mid);

		if (mid - begin < end - mid) {
			qsort(arr, begin, mid, compfunc);
			begin = mid + 1;
		} else {
			qsort(arr, mid + 1, end, compfunc);
			end = mid;
		}<commit_msg>Add some spacing for easier readability<commit_after>/*
 * This file is part of Kotaka, a mud library for DGD
 * http://github.com/shentino/kotaka
 *
 * Copyright (C) 2013, 2014  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <kotaka/paths/bigstruct.h>
#include <type.h>

private void aswap(mixed arr, int a, int b)
{
	mixed tmp;

	if (typeof(arr) == T_ARRAY) {
		tmp = arr[a];
		arr[a] = arr[b];
		arr[b] = tmp;
	} else {
		tmp = arr->query_element(a);
		arr->set_element(a, arr->query_element(b));
		arr->set_element(b, tmp);
	}
}

void qsort(mixed arr, int begin, int end, varargs string compfunc)
{
	int type;

	type = typeof(arr);

	while (begin < end) {
		int low, mid, high, sign;
		mixed pivot;

		low = begin;
		mid = (begin + end) / 2;
		high = end - 1;

		pivot = (type == T_ARRAY) ? arr[mid] : arr->query_element(mid);
		aswap(arr, mid, high);

		while (low < high) {
			mixed le;

			le = (type == T_ARRAY) ? arr[low] : arr->query_element(low);

			if (compfunc) {
				sign = call_other(previous_object(), compfunc, le, pivot);
			} else if (le > pivot) {
				sign = 1;
			} else {
				sign = 0;
			}

			if (sign > 0) {
				aswap(arr, low, --high);
			} else {
				low++;
			}
		}

		mid = low;
		aswap(arr, end - 1, mid);

		if (mid - begin < end - mid) {
			qsort(arr, begin, mid, compfunc);
			begin = mid + 1;
		} else {
			qsort(arr, mid + 1, end, compfunc);
			end = mid;
		}
{'fraction_non_alphanumeric': 0.13237836502840208, 'fraction_numerical': 0.008397135095085207, 'mean_word_length': 2.998025666337611, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5434324', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1401, 'n_words': 507}