summaryrefslogtreecommitdiff
path: root/src/data_count.c
blob: 8d36c8b35fa2d4b0860628ef0f6051f909e50100 (plain)
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
#include "array.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static data_unset *data_count_copy(const data_unset *s) {
	data_count *src = (data_count *)s;
	data_count *ds = data_count_init();

	buffer_copy_string_buffer(ds->key, src->key);
	ds->count = src->count;
	ds->is_index_key = src->is_index_key;
	return (data_unset *)ds;
}

static void data_count_free(data_unset *d) {
	data_count *ds = (data_count *)d;

	buffer_free(ds->key);

	free(d);
}

static void data_count_reset(data_unset *d) {
	data_count *ds = (data_count *)d;

	buffer_reset(ds->key);

	ds->count = 0;
}

static int data_count_insert_dup(data_unset *dst, data_unset *src) {
	data_count *ds_dst = (data_count *)dst;
	data_count *ds_src = (data_count *)src;

	ds_dst->count += ds_src->count;

	src->free(src);

	return 0;
}

static void data_count_print(const data_unset *d, int depth) {
	data_count *ds = (data_count *)d;
	UNUSED(depth);

	fprintf(stdout, "count(%d)", ds->count);
}


data_count *data_count_init(void) {
	data_count *ds;

	ds = calloc(1, sizeof(*ds));

	ds->key = buffer_init();
	ds->count = 1;

	ds->copy = data_count_copy;
	ds->free = data_count_free;
	ds->reset = data_count_reset;
	ds->insert_dup = data_count_insert_dup;
	ds->print = data_count_print;
	ds->type = TYPE_COUNT;

	return ds;
}