/* utils.c generated by valac 0.56.0, the Vala compiler
 * generated from utils.vala, do not modify */

#include "livechart_static.h"
#include <glib.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <gee.h>
#include <glib-object.h>

#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

VALA_EXTERN gint live_chart_num_of_digits (gint value);
static gfloat* _float_dup (gfloat* self);
static gint __lambda4_ (gint a,
                 gint b);
static gint ___lambda4__gcompare_data_func (gconstpointer a,
                                     gconstpointer b,
                                     gpointer self);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

gfloat
live_chart_cap (gfloat value)
{
	gint num_digits = 0;
	gdouble size = 0.0;
	gdouble integer_part = 0.0;
	gdouble fractional_part = 0.0;
	gdouble _tmp0_ = 0.0;
	gdouble _tmp1_;
	gdouble delta = 0.0;
	gfloat result;
#line 6 "../src/utils.vala"
	num_digits = live_chart_num_of_digits ((gint) value);
#line 8 "../src/utils.vala"
	size = pow ((gdouble) 10, ((gdouble) num_digits) - 1);
#line 10 "../src/utils.vala"
	if (fmod (value, size) == ((gdouble) 0)) {
#line 11 "../src/utils.vala"
		result = value;
#line 11 "../src/utils.vala"
		return result;
#line 48 "utils.c"
	}
#line 15 "../src/utils.vala"
	_tmp1_ = modf (value / size, &_tmp0_);
#line 15 "../src/utils.vala"
	integer_part = _tmp0_;
#line 15 "../src/utils.vala"
	fractional_part = _tmp1_ * size;
#line 17 "../src/utils.vala"
	delta = size - fractional_part;
#line 19 "../src/utils.vala"
	result = (gfloat) (value + delta);
#line 19 "../src/utils.vala"
	return result;
#line 62 "utils.c"
}

gboolean
live_chart_has_fractional_part (gfloat value)
{
	gboolean result;
#line 23 "../src/utils.vala"
	result = value != ((gfloat) ((gint) value));
#line 23 "../src/utils.vala"
	return result;
#line 73 "utils.c"
}

gchar*
live_chart_format_for_y_axis (const gchar* unit,
                              gfloat value)
{
	gchar* pattern = NULL;
	gchar* _tmp0_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* result;
#line 26 "../src/utils.vala"
	g_return_val_if_fail (unit != NULL, NULL);
#line 27 "../src/utils.vala"
	_tmp0_ = g_strdup ("%0.0f%s");
#line 27 "../src/utils.vala"
	pattern = _tmp0_;
#line 28 "../src/utils.vala"
	if (live_chart_has_fractional_part (value)) {
#line 93 "utils.c"
		gchar* _tmp1_;
#line 29 "../src/utils.vala"
		_tmp1_ = g_strdup ("%0.2f%s");
#line 29 "../src/utils.vala"
		_g_free0 (pattern);
#line 29 "../src/utils.vala"
		pattern = _tmp1_;
#line 101 "utils.c"
	}
#line 31 "../src/utils.vala"
	_tmp2_ = pattern;
#line 31 "../src/utils.vala"
	_tmp3_ = g_strdup_printf (_tmp2_, value, unit);
#line 31 "../src/utils.vala"
	result = _tmp3_;
#line 31 "../src/utils.vala"
	_g_free0 (pattern);
#line 31 "../src/utils.vala"
	return result;
#line 113 "utils.c"
}

static gfloat*
_float_dup (gfloat* self)
{
	gfloat* dup;
#line 37 "../src/utils.vala"
	dup = g_new0 (gfloat, 1);
#line 37 "../src/utils.vala"
	memcpy (dup, self, sizeof (gfloat));
#line 37 "../src/utils.vala"
	return dup;
#line 126 "utils.c"
}

static gint
__lambda4_ (gint a,
            gint b)
{
	gint result;
#line 57 "../src/utils.vala"
	result = a - b;
#line 57 "../src/utils.vala"
	return result;
#line 138 "utils.c"
}

static gint
___lambda4__gcompare_data_func (gconstpointer a,
                                gconstpointer b,
                                gpointer self)
{
	gint result;
	result = __lambda4_ ((gint) ((gintptr) a), (gint) ((gintptr) b));
#line 56 "../src/utils.vala"
	return result;
#line 150 "utils.c"
}

GeeList*
live_chart_golden_divisors (gfloat value)
{
	gfloat _tmp1_ = 0.0F;
	gfloat factor = 0.0F;
	gfloat working_value = 0.0F;
	gfloat sqrt = 0.0F;
	GeeArrayList* divs = NULL;
	GeeArrayList* _tmp2_;
	GeeArrayList* _tmp7_;
	GeeArrayList* ndivs = NULL;
	GeeArrayList* _tmp8_;
	gint last_div = 0;
	GeeArrayList* _tmp9_;
	gpointer _tmp10_;
	GeeList* result;
#line 36 "../src/utils.vala"
	if (value == ((gfloat) 0)) {
#line 171 "utils.c"
		GeeArrayList* _tmp0_;
#line 37 "../src/utils.vala"
		_tmp0_ = gee_array_list_new (G_TYPE_FLOAT, (GBoxedCopyFunc) _float_dup, (GDestroyNotify) g_free, NULL, NULL, NULL);
#line 37 "../src/utils.vala"
		result = (GeeList*) _tmp0_;
#line 37 "../src/utils.vala"
		return result;
#line 179 "utils.c"
	}
#line 41 "../src/utils.vala"
	if (value < ((gfloat) 10)) {
#line 41 "../src/utils.vala"
		_tmp1_ = live_chart_cap (100 / value);
#line 185 "utils.c"
	} else {
#line 41 "../src/utils.vala"
		_tmp1_ = 1.f;
#line 189 "utils.c"
	}
#line 41 "../src/utils.vala"
	factor = _tmp1_;
#line 42 "../src/utils.vala"
	working_value = value * factor;
#line 44 "../src/utils.vala"
	sqrt = sqrtf (working_value);
#line 45 "../src/utils.vala"
	_tmp2_ = gee_array_list_new (G_TYPE_INT, NULL, NULL, NULL, NULL, NULL);
#line 45 "../src/utils.vala"
	divs = _tmp2_;
#line 201 "utils.c"
	{
		gint i = 0;
#line 47 "../src/utils.vala"
		i = 1;
#line 206 "utils.c"
		{
			gboolean _tmp3_ = FALSE;
#line 47 "../src/utils.vala"
			_tmp3_ = TRUE;
#line 47 "../src/utils.vala"
			while (TRUE) {
#line 47 "../src/utils.vala"
				if (!_tmp3_) {
#line 215 "utils.c"
					gint _tmp4_;
#line 47 "../src/utils.vala"
					_tmp4_ = i;
#line 47 "../src/utils.vala"
					i = _tmp4_ + 1;
#line 221 "utils.c"
				}
#line 47 "../src/utils.vala"
				_tmp3_ = FALSE;
#line 47 "../src/utils.vala"
				if (!(((gfloat) i) <= sqrt)) {
#line 47 "../src/utils.vala"
					break;
#line 229 "utils.c"
				}
#line 48 "../src/utils.vala"
				if (fmodf (working_value, i) == ((gfloat) 0)) {
#line 233 "utils.c"
					GeeArrayList* _tmp5_;
					gfloat tmp = 0.0F;
#line 49 "../src/utils.vala"
					_tmp5_ = divs;
#line 49 "../src/utils.vala"
					gee_abstract_collection_add ((GeeAbstractCollection*) _tmp5_, (gpointer) ((gintptr) i));
#line 50 "../src/utils.vala"
					tmp = working_value / i;
#line 51 "../src/utils.vala"
					if (tmp != ((gfloat) i)) {
#line 244 "utils.c"
						GeeArrayList* _tmp6_;
#line 52 "../src/utils.vala"
						_tmp6_ = divs;
#line 52 "../src/utils.vala"
						gee_abstract_collection_add ((GeeAbstractCollection*) _tmp6_, (gpointer) ((gintptr) tmp));
#line 250 "utils.c"
					}
				}
			}
		}
	}
#line 56 "../src/utils.vala"
	_tmp7_ = divs;
#line 56 "../src/utils.vala"
	gee_list_sort ((GeeList*) _tmp7_, ___lambda4__gcompare_data_func, NULL, NULL);
#line 60 "../src/utils.vala"
	_tmp8_ = gee_array_list_new (G_TYPE_FLOAT, (GBoxedCopyFunc) _float_dup, (GDestroyNotify) g_free, NULL, NULL, NULL);
#line 60 "../src/utils.vala"
	ndivs = _tmp8_;
#line 61 "../src/utils.vala"
	_tmp9_ = divs;
#line 61 "../src/utils.vala"
	_tmp10_ = gee_list_last ((GeeList*) _tmp9_);
#line 61 "../src/utils.vala"
	last_div = (gint) ((gintptr) _tmp10_);
#line 270 "utils.c"
	{
		gint i = 0;
		GeeArrayList* _tmp11_;
		gint _tmp12_;
		gint _tmp13_;
#line 62 "../src/utils.vala"
		_tmp11_ = divs;
#line 62 "../src/utils.vala"
		_tmp12_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp11_);
#line 62 "../src/utils.vala"
		_tmp13_ = _tmp12_;
#line 62 "../src/utils.vala"
		i = _tmp13_ - 1;
#line 284 "utils.c"
		{
			gboolean _tmp14_ = FALSE;
#line 62 "../src/utils.vala"
			_tmp14_ = TRUE;
#line 62 "../src/utils.vala"
			while (TRUE) {
#line 291 "utils.c"
				gint current = 0;
				GeeArrayList* _tmp16_;
				gpointer _tmp17_;
				GeeArrayList* _tmp18_;
				gfloat _tmp19_;
#line 62 "../src/utils.vala"
				if (!_tmp14_) {
#line 299 "utils.c"
					gint _tmp15_;
#line 62 "../src/utils.vala"
					_tmp15_ = i;
#line 62 "../src/utils.vala"
					i = _tmp15_ - 1;
#line 305 "utils.c"
				}
#line 62 "../src/utils.vala"
				_tmp14_ = FALSE;
#line 62 "../src/utils.vala"
				if (!(i >= 0)) {
#line 62 "../src/utils.vala"
					break;
#line 313 "utils.c"
				}
#line 63 "../src/utils.vala"
				_tmp16_ = divs;
#line 63 "../src/utils.vala"
				_tmp17_ = gee_abstract_list_get ((GeeAbstractList*) _tmp16_, i);
#line 63 "../src/utils.vala"
				current = (gint) ((gintptr) _tmp17_);
#line 64 "../src/utils.vala"
				if ((last_div / current) != 2) {
#line 65 "../src/utils.vala"
					continue;
#line 325 "utils.c"
				}
#line 67 "../src/utils.vala"
				_tmp18_ = ndivs;
#line 67 "../src/utils.vala"
				_tmp19_ = (gfloat) current;
#line 67 "../src/utils.vala"
				gee_abstract_collection_add ((GeeAbstractCollection*) _tmp18_, &_tmp19_);
#line 68 "../src/utils.vala"
				last_div = current;
#line 335 "utils.c"
			}
		}
	}
#line 71 "../src/utils.vala"
	result = (GeeList*) ndivs;
#line 71 "../src/utils.vala"
	_g_object_unref0 (divs);
#line 71 "../src/utils.vala"
	return result;
#line 345 "utils.c"
}

gint
live_chart_num_of_digits (gint value)
{
	gint num_digits = 0;
	gint result;
#line 75 "../src/utils.vala"
	num_digits = 0;
#line 76 "../src/utils.vala"
	while (TRUE) {
#line 357 "utils.c"
		gint _tmp0_;
#line 76 "../src/utils.vala"
		if (!(value != 0)) {
#line 76 "../src/utils.vala"
			break;
#line 363 "utils.c"
		}
#line 77 "../src/utils.vala"
		value = value / 10;
#line 78 "../src/utils.vala"
		num_digits = num_digits + 1;
#line 78 "../src/utils.vala"
		_tmp0_ = num_digits;
#line 371 "utils.c"
	}
#line 81 "../src/utils.vala"
	result = num_digits;
#line 81 "../src/utils.vala"
	return result;
#line 377 "utils.c"
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

