summaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2007-11-27 10:49:38 +0000
committerChris Wilson <chris@chris-wilson.co.uk>2007-11-27 10:49:38 +0000
commit40c801d9d167460fffb63168b99a5a2ca098749a (patch)
tree8ce1d837e0691a192845b33de1c65b3632a09d02 /src/utils.c
parent6f9b43bae9a67c25d0d931dddab2de38a24d314f (diff)
Add labels to the ring view.
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index a815563..f34b49c 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -19,3 +19,40 @@ pretty_print_number (const char *number, gint len, char *output)
*--output = *--number;
}
}
+
+#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)
+