From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6959 invoked by alias); 23 Sep 2005 08:36:21 -0000 Mailing-List: contact systemtap-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Post: List-Help: , Sender: systemtap-owner@sources.redhat.com Received: (qmail 6013 invoked by uid 22791); 23 Sep 2005 08:35:47 -0000 Subject: array sorting checked in From: Martin Hunt To: systemtap@sources.redhat.com Content-Type: text/plain Organization: Red Hat Inc. Date: Fri, 23 Sep 2005 08:36:00 -0000 Message-Id: <1127464449.7927.10.camel@dragon> Mime-Version: 1.0 X-Mailer: Evolution 2.2.3 (2.2.3-2.fc4) Content-Transfer-Encoding: 7bit X-SW-Source: 2005-q3/txt/msg00575.txt.bz2 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() }