51 return left_item.x < right_item.x;
55 typedef std::multiset<Item, ItemComperator> ItemSet;
76 my_bool
gini_init(UDF_INIT *initid, UDF_ARGS *args,
char *message) {
79 if (args->arg_count < 1 || args->arg_count > 2) {
80 strcpy(message,
"gini(x [,w]) requires one or two arguments");
83 args->arg_type[0] = REAL_RESULT;
84 if (args->arg_count > 1) {
85 args->arg_type[1] = REAL_RESULT;
90 strcpy(message,
"Couldn't allocate memory");
93 data->
argc = args->arg_count;
95 initid->maybe_null = 1;
97 initid->max_length = 13 + initid->decimals;
98 initid->ptr = (
char *) data;
99 initid->const_item = 0;
115 void gini_reset(UDF_INIT *initid, UDF_ARGS *args,
char *is_null,
char *error) {
117 gini_add(initid, args, is_null, error);
129 void gini_clear(UDF_INIT *initid,
char *is_null,
char *error) {
146 void gini_add(UDF_INIT *initid, UDF_ARGS *args,
char *is_null,
char *error) {
150 if (!args->args[0]) {
154 i.x = *((
double*) args->args[0]);
155 if (data->
argc > 1 && args->args[1]) {
156 i.w = *((
double*) args->args[1]);
161 data->
data.insert(i);
162 }
catch (std::bad_alloc e) {
176 double gini(UDF_INIT *initid, UDF_ARGS *args,
char *is_null,
char *error) {
178 sqlstat::ItemSet::iterator pos;
186 for(pos = data->
data.begin(); pos != data->
data.end(); ++pos) {
188 double x = w * (*pos).x;
189 ret += w * (2. * sumx + x);
194 if (sumx == 0 || sumw == 0) {
199 return 1. - ret / (sumx * sumw);
Definition of functions for UDFs and plugins.
double gini(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve gini. Called at end of group.
my_bool gini_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
int argc
number of arguments
bool operator()(const Item &left_item, const Item &right_item) const
void gini_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Reset function and add first group member Calls clear and add.
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
void gini_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Add a member of the group.
ItemSet data
collected data
void gini_deinit(UDF_INIT *initid)
Called after last access to function.
void gini_clear(UDF_INIT *initid, char *is_null, char *error)
Called at start of group.