summaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 36568641bc4ef4e69713125daf60ad82f1f43204 (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
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
158
159
160
161
162
163
164
/*
   This file is part of odin, a memory profiler with fragmentation analysis.

   Copyright (C) 2007 Chris Wilson <chris@chris-wilson.co.uk>

   odin is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   odin 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with odin. If not, see <http://www.gnu.org/licenses/>/

   The GNU General Public License is contained in the file COPYING.
*/

#include "odin.h"

#include <math.h>

void
pretty_print_number (const char *number, gint len, char *output)
{
    gint i;

    output += len + (len - 1) / 3;
    number += len;
    *output = '\0';

    i = -1;
    while (len--) {
	if (++i == 3) {
	    *--output = ',';
	    i = 0;
	}

	*--output = *--number;
    }
}

void
cell_layout_pretty_print_uint (GtkCellLayout   *layout,
	                       GtkCellRenderer *cell,
			       GtkTreeModel    *model,
			       GtkTreeIter     *iter,
			       gpointer         data)
{
    guint v;
    char buf[80];
    gint len;

    gtk_tree_model_get (model, iter, GPOINTER_TO_UINT (data), &v, -1);

    len = g_snprintf (buf + 40, 40, "%u", v);
    pretty_print_number (buf + 40, len, buf);

    g_object_set (G_OBJECT (cell),
	          "text", buf,
		  "xalign", (gfloat) 1.0,
		  NULL);
}

void
cell_layout_pretty_print_uint64 (GtkCellLayout   *layout,
	                         GtkCellRenderer *cell,
				 GtkTreeModel    *model,
				 GtkTreeIter     *iter,
				 gpointer         data)
{
    guint64 v;
    char buf[160];
    gint len;

    gtk_tree_model_get (model, iter, GPOINTER_TO_UINT (data), &v, -1);

    len = g_snprintf (buf + 80, 80, "%" G_GUINT64_FORMAT, v);
    pretty_print_number (buf + 80, len, buf);

    g_object_set (G_OBJECT (cell),
	          "text", buf,
		  "xalign", (gfloat) 1.0,
		  NULL);
}

void
hsv_to_rgb (gdouble h, gdouble s, gdouble v, gdouble *rgb)
{
    gdouble m, n, f;
    gint i;

    while (h < 0)
	h += 6.;
    while (h > 6.)
	h -= 6.;

    if (s < 0.)
	s = 0.;
    if (s > 1.)
	s = 1.;

    if (v < 0.)
	v = 0.;
    if (v > 1.)
	v = 1.;

    i = floor (h);
    f = h - i;
    if ((i & 1) == 0)
	f = 1 - f;

    m = v * (1 - s);
    n = v * (1 - s * f);
    switch(i){
    case 6:
    case 0: rgb[0] = v; rgb[1] = n; rgb[2] = m; break;
    case 1: rgb[0] = n; rgb[1] = v; rgb[2] = m; break;
    case 2: rgb[0] = m; rgb[1] = v; rgb[2] = n; break;
    case 3: rgb[0] = m; rgb[1] = n; rgb[2] = v; break;
    case 4: rgb[0] = n; rgb[1] = m; rgb[2] = v; break;
    case 5: rgb[0] = v; rgb[1] = m; rgb[2] = n; break;
    }
}


#define swap(a,b) G_STMT_START{ t=v[a]; v[a]=v[b]; v[b]=t; }G_STMT_END
#define MEDIAN(type) \
g##type median_##type(g##type *v, guint n)\
{\
	guint low=0, high=n-1;\
	guint median=(low+high)/2;\
	guint middle, ll, hh;\
	type t;\
	g_assert(n!=0);\
	do{\
		if(high<=low) return v[median];\
		if(high==low+1){\
			if(v[low]>v[high]) swap(low, high);\
			return v[median];\
		}\
		middle=(low+high)/2;\
		if(v[middle]>v[high]) swap(middle, high);\
		if(v[low]>v[high]) swap(low, high);\
		if(v[middle]>v[low]) swap(middle, low);\
		swap(middle, low+1);\
		ll=low+1;\
		hh=high;\
		for(;;){\
			do ll++; while(v[low]>v[ll]);\
			do hh--; while(v[hh]>v[low]);\
			if(hh<ll) break;\
			swap(ll,hh);\
		}\
		swap(low, hh);\
		if(hh<=median) low=ll;\
		if(hh>=median) high=hh-1;\
	}while(1);\
}

MEDIAN(double)