File: sort-utils.h

package info (click to toggle)
btrfs-progs 6.16-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,504 kB
  • sloc: ansic: 126,181; sh: 7,642; python: 1,386; makefile: 900; asm: 296
file content (105 lines) | stat: -rw-r--r-- 3,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#ifndef __COMMON_SORT_UTILS_H__
#define __COMMON_SORT_UTILS_H__

#include <stdbool.h>
#include "common/compat.h"

/*
 * Example:

struct entry {
	int id;
	long size;
};

static int cmp_entry_id(const struct entry *a, const struct entry *b)
{
	return (a->id < b->id ? -1 :
		a->id > b->id ?  1 : 0);
}

static int cmp_entry_size(const struct entry *a, const struct entry *b)
{
	return (a->size < b->size ? -1 :
		a->size > b->size ?  1 : 0);
}

void test() {
	// User data
	struct entry entries[SIZE];
	// Comparator structure
	struct compare comp = { 0 };
	// Keys, item comparators, help text definitions
	struct sortdef sortit[] = {
		{ .name = "id",   .comp = (sort_cmp_t)cmp_entry_id,
		  .desc = "sort by id" },
		{ .name = "size", .comp = (sort_cmp_t)cmp_entry_size,
		  .desc = "sort by entry size" },
		SORTDEF_END
	};
	// List of keys to use for sort (e.g. from command line options)
	const char *sortby[] = { "size", "id" };

	compare_init(&comp, sortit);
	for (i = 0; i < sizeof(sortby) / sizeof(sortby[0]); i++) {
		ret = compare_add_sort_key(&comp, sortby[i]);
		if (ret < 0) {
			printf("ERROR adding sort key %s\n", sortby[i]);
			break;
		}
	}
	qsort_r(entries, SIZE, sizeof(struct entry), (sort_r_cmp_t)compare_cmp_multi,
		&comp);
}
 */

#define SORTDEF_END		{ .name = NULL, .comp = NULL }
#define SORT_MAX_KEYS		32

typedef int (*sort_cmp_t)(const void *a, const void *b);
typedef int (*sort_r_cmp_t)(const void *a, const void *b, void *data);

#define SORTDEF_END { .name = NULL,   .comp = NULL }

struct sortdef {
	const char *name;
	const char *desc;
	sort_cmp_t comp;
	/* User defined identifier of this sort key. */
	int id;
};

struct compare {
	sort_cmp_t comp[SORT_MAX_KEYS];
	unsigned long invert_map;
	int count;
	const struct sortdef *sortdef;
};

int compare_init(struct compare *comp, const struct sortdef *sortdef);
int compare_cmp_multi(const void *a, const void *b, const struct compare *comp);
int compare_add_sort_key(struct compare *comp, const char *key);
int compare_parse_key_to_id(const struct compare *comp, const char **next);
int compare_add_sort_id(struct compare *comp, int id);
int compare_key_id(const struct compare *comp, const char *key);
const char *compare_id_name(const struct compare *comp, int id);
bool compare_has_id(const struct compare *comp, int id);
int compare_setup_sort(struct compare *comp, const struct sortdef *sdef, const char *def);

#endif