This is the mail archive of the
systemtap@sourceware.org
mailing list for the systemtap project.
array sorting checked in
- From: Martin Hunt <hunt at redhat dot com>
- To: systemtap at sources dot redhat dot com
- Date: Fri, 23 Sep 2005 01:34:09 -0700
- Subject: array sorting checked in
- Organization: Red Hat Inc.
I checked in several new functions to the runtime.
_stp_map_sort(MAP map, int keynum, int dir)
_stp_map_sortn(MAP map, int n, int keynum, int dir)
_stp_map_printn(MAP map, int n, const char *fmt)
keynum = 0 for value, or > 0 for the key number to sort on.
n = number of elements to print or sort. 0 means entire array.
dir = sorting direction. -1 or 1
_stp_map_sortn() will scan an array and put the top (or bottom) 'n'
elements at the start of the array. This is useful when you don't care
about the whole array, just the top values.
Here's a working example. (the current language can't pass array
references, therefore the awkward embedded C)
--------------------
global foo, moo
function print_foo (num:long) %{
_stp_map_printn(global_foo, THIS->num, "%1s is %s");
%}
function print_moo (num:long) %{
_stp_map_printn(global_moo, THIS->num, "%1d,%2d,%3d,%4d,%5d = %d");
%}
function sort_foo (num:long, key:long, dir:long) %{
_stp_map_sortn(global_foo, THIS->num, THIS->key, THIS->dir);
%}
function sort_moo (num:long, key:long, dir:long) %{
_stp_map_sortn(global_moo, THIS->num, THIS->key, THIS->dir);
%}
probe begin
{
foo["seattle"] = "sleepless"
foo["new orleans"] = "wet"
foo["florida"] = "hot"
foo["texas"] = "red"
foo["san francisco"] = "wired"
foo["galveston"] = "scared"
foo["miami"] = "viced"
foo["maine"] = "iced"
moo[1,2,3,4,5] = 1000
moo[1,12,3,1,6] = 1001
moo[2,2,3,4,5] = 1000
moo[10,20,30,40,50] = 10000
moo[8,2,3,4,7] = -1000
moo[-1,2,4,1,6] = 2001
moo[0,2,4,0,6] = 1002
log("sorted by value");
sort_foo(0,0,-1)
print_foo(0)
log("sorted by value (reverse)");
sort_foo(0,0,1)
print_foo(0)
log("sorted by key 1");
sort_foo(0,1,-1)
print_foo(0)
/* reverse the list again */
sort_foo(0,1,1)
log("Just the top three. Sorted by key 1");
sort_foo(3,1,-1)
print_foo(3)
print_moo(0)
log("sorted by value");
sort_moo(0,0,-1)
print_moo(0)
log("sorted by value (reverse)");
sort_moo(0,0,1)
print_moo(0)
log("sorted by key 1");
sort_moo(0,1,-1)
print_moo(0)
exit()
}