-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
157 lines (131 loc) · 3.42 KB
/
main.cpp
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
156
157
#include <map>
#include <string>
#include <string.h>
#include <my_global.h>
#include <my_sys.h>
#include <mysql_com.h>
extern "C" {
my_bool most_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void most_clear(UDF_INIT* initid, char* is_null, char *error);
void most_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error);
void most_deinit(UDF_INIT *initid);
char * most(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error);
my_bool most_times_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void most_times_clear(UDF_INIT* initid, char* is_null, char *error);
void most_times_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error);
void most_times_deinit(UDF_INIT *initid);
long long most_times(UDF_INIT *initid, UDF_ARGS *args,
char *is_null, char *error);
}
struct UDFMostData
{
std::map<std::string, int> hits;
};
////////////////////////////////////////////////////////////////////////////////
// most
my_bool most_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if (args->arg_count != 1) {
strcpy(message, "most must have exaclty one argument");
return 1;
}
args->arg_type[0] = STRING_RESULT;
initid->max_length = 65535;
initid->maybe_null = 1;
initid->ptr = (char*) new UDFMostData;
return 0;
}
void most_clear(UDF_INIT* initid, char* is_null, char *error)
{
UDFMostData * p = (UDFMostData *)initid->ptr;
p->hits.clear();
}
void most_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error)
{
if (args->args[0])
{
UDFMostData * p = (UDFMostData *)initid->ptr;
std::string k(args->args[0], args->lengths[0]);
std::map<std::string, int>::iterator it = p->hits.find(k);
if (it == p->hits.end())
{
p->hits[k] = 1;
}
else
{
++(*it).second;
}
}
}
void most_deinit(UDF_INIT *initid)
{
UDFMostData * p = (UDFMostData *)initid->ptr;
delete p;
}
char * most(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *length,
char *is_null, char *error)
{
UDFMostData * p = (UDFMostData *)initid->ptr;
if (p->hits.empty())
{
*is_null = 1;
return NULL;
}
else
{
std::map<std::string, int>::iterator itMost = p->hits.begin();
std::map<std::string, int>::iterator it = itMost;
++it;
for (; it != p->hits.end(); ++it)
{
if ((*it).second > (*itMost).second)
itMost = it;
}
result = (char *)(*itMost).first.c_str();
*length = (*itMost).first.size();
return result;
}
}
////////////////////////////////////////////////////////////////////////////////
// most_times
my_bool most_times_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
return most_init(initid, args, message);
}
void most_times_clear(UDF_INIT* initid, char* is_null, char *error)
{
return most_clear(initid, is_null, error);
}
void most_times_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error)
{
return most_add(initid, args, is_null, error);
}
void most_times_deinit(UDF_INIT *initid)
{
return most_deinit(initid);
}
long long most_times(UDF_INIT *initid, UDF_ARGS *args,
char *is_null, char *error)
{
UDFMostData * p = (UDFMostData *)initid->ptr;
if (p->hits.empty())
{
*is_null = 1;
return 0;
}
else
{
std::map<std::string, int>::iterator itMost = p->hits.begin();
std::map<std::string, int>::iterator it = itMost;
++it;
for (; it != p->hits.end(); ++it)
{
if ((*it).second > (*itMost).second)
itMost = it;
}
return (*itMost).second;
}
}