public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
* array sorting checked in
@ 2005-09-23  8:36 Martin Hunt
  2005-09-23 17:57 ` Frank Ch. Eigler
  2005-09-24  6:25 ` James Dickens
  0 siblings, 2 replies; 8+ messages in thread
From: Martin Hunt @ 2005-09-23  8:36 UTC (permalink / raw)
  To: systemtap

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()
}




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-09-25  6:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-23  8:36 array sorting checked in Martin Hunt
2005-09-23 17:57 ` Frank Ch. Eigler
2005-09-23 19:44   ` Martin Hunt
2005-09-23 22:20     ` Frank Ch. Eigler
2005-09-24  6:25 ` James Dickens
2005-09-24 12:29   ` Frank Ch. Eigler
2005-09-24 15:29     ` James Dickens
2005-09-25  6:21     ` Martin Hunt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).