Repository navigation
Expand file tree
/
Copy pathudf_bitarray.c
More file actions
156 lines (133 loc) · 4.47 KB
/
Copy pathudf_bitarray.c
File metadata and controls
156 lines (133 loc) · 4.47 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
udf_bitarray - aggregate ints into a bitarray
Copyright (C) 2014 Timothy Caro-Bruce
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP
#endif
#ifdef STANDARD
#include <string.h>
#include <stdlib.h>
#include <time.h>
#else
#include <my_global.h>
#include <my_sys.h>
#endif
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef HAVE_DLOPEN
#define BITARRAY_DEFAULT_SIZE 1024*1024
/**
* udf_bitarray
*/
#ifdef __cplusplus
extern "C" {
#endif
my_bool bitarray_init(UDF_INIT* initid, UDF_ARGS* args, char* message);
void bitarray_deinit(UDF_INIT* initid);
void bitarray_clear(UDF_INIT* initid, char* is_null, char *error);
void bitarray_reset(UDF_INIT* initid, UDF_ARGS* args, char* is_null,
char *error);
void bitarray_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error);
char *bitarray(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *length, char *is_null, char *error);
#ifdef __cplusplus
}
#endif
typedef struct st_bitarray_container {
long long size;
char *bitarray;
} bitarray_container;
my_bool bitarray_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
bitarray_container *container;
long long size = BITARRAY_DEFAULT_SIZE;
if(args->arg_count < 1 || args->arg_count > 2){
strcpy(message, "Expect one or two arguments (udf: bitarray)");
return 1;
}
if (args->arg_count == 2)
{
if(args->arg_type[1] != INT_RESULT) {
strcpy(message, "Second argument must be an int (udf: bitarray)");
return 1;
} else {
size = *((long long*) args->args[1]) / 8 + 1;
}
}
container = malloc(sizeof(bitarray_container));
if (! container) {
container = NULL;
strcpy(message, "Could not allocate memory (udf: bitarray)");
return 1;
}
container->size = size;
container->bitarray = malloc(size * sizeof(char));
if (!container->bitarray) {
container->bitarray = NULL;
strcpy(message, "Could not allocate memory (udf: bitarray)");
return 1;
}
initid->ptr = (char *) container;
initid->maybe_null = 0;
initid->max_length = size;
args->arg_type[0] = INT_RESULT;
return 0;
}
void bitarray_clear( UDF_INIT* initid, char* is_null, char *error ) {
bitarray_container *container = (bitarray_container *)initid->ptr;
for (int i=0; i < container->size; i++) {
container->bitarray[i] = 0;
}
}
void bitarray_reset(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error) {
bitarray_clear(initid, is_null, error);
bitarray_add(initid, args, is_null, error);
}
void bitarray_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error)
{
bitarray_container *container = (bitarray_container *)initid->ptr;
long long idx, offset, id;
if(args->args[0] != NULL){
id = *((long long*) args->args[0]);
idx = id / 8;
if (idx >= container->size) {
*error = 1;
return;
}
offset = id % 8;
container->bitarray[idx] |= 1 << offset;
}
}
char *bitarray(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error) {
bitarray_container *container = (bitarray_container *)initid->ptr;
*length = container->size;
return container->bitarray;
}
void bitarray_deinit(UDF_INIT *initid){
bitarray_container *container = (bitarray_container *)initid->ptr;
if (container != NULL) {
if (container->bitarray != NULL) {
free(container->bitarray);
}
free(container);
}
}
#endif /* HAVE_DLOPEN */