summaryrefslogtreecommitdiffstats
path: root/runtime/map.c
diff options
context:
space:
mode:
authorhunt <hunt>2005-09-23 08:14:22 +0000
committerhunt <hunt>2005-09-23 08:14:22 +0000
commitfbb24b89ec28da5754719732edec3f2dc5bc773e (patch)
tree9c5b48c25171d48d0605d1fe8b130af054e43a16 /runtime/map.c
parent4a61d60063c01fc969887125868a484c0b1b7e8f (diff)
downloadsystemtap-steved-fbb24b89ec28da5754719732edec3f2dc5bc773e.tar.gz
systemtap-steved-fbb24b89ec28da5754719732edec3f2dc5bc773e.tar.xz
systemtap-steved-fbb24b89ec28da5754719732edec3f2dc5bc773e.zip
2005-09-23 Martin Hunt <hunt@redhat.com>
* map.c (_stp_map_sortn): Call _stp_map_sort() when n is 0.
Diffstat (limited to 'runtime/map.c')
-rw-r--r--runtime/map.c122
1 files changed, 63 insertions, 59 deletions
diff --git a/runtime/map.c b/runtime/map.c
index a6f334ff..3fc2bb47 100644
--- a/runtime/map.c
+++ b/runtime/map.c
@@ -347,65 +347,6 @@ static inline void _stp_swap (struct list_head *a, struct list_head *b)
b->next = a;
}
-/** Get the top values from an array.
- * Sorts an array such that the start of the array contains the top
- * or bottom 'n' values. Use this when sorting the entire array
- * would be too time-consuming and you are only interested in the
- * highest or lowest values.
- *
- * @param map Map
- * @param n Top (or bottom) number of elements
- * @param keynum 0 for the value, or a positive number for the key number to sort on.
- * @param dir Sort Direction. -1 for low-to-high. 1 for high-to-low.
- * @sa _stp_map_sort()
- */
-void _stp_map_sortn(MAP map, int n, int keynum, int dir)
-{
- struct list_head *head = &map->head;
- struct list_head *c, *a, *last, *tmp;
- int type, num, swaps = 1;
-
- if (list_empty(head))
- return;
-
- if (keynum)
- (*map->get_key)((struct map_node *)head->next, keynum, &type);
- else
- type = ((struct map_node *)head->next)->map->type;
-
- /* start off with a modified bubble sort of the first n elements */
- while (swaps) {
- num = n;
- swaps = 0;
- a = head->next;
- c = a->next->next;
- while ((a->next != head) && (--num > 0)) {
- if (_stp_cmp(a, a->next, keynum, dir, type)) {
- swaps++;
- _stp_swap(a, a->next);
- }
- a = c->prev;
- c = c->next;
- }
- }
-
- /* Now use a kind of insertion sort for the rest of the array. */
- /* Each element is tested to see if it should be be in the top 'n' */
- last = a;
- a = a->next;
- while (a != head) {
- tmp = a->next;
- c = last;
- while (c != head && _stp_cmp(c, a, keynum, dir, type))
- c = c->prev;
- if (c != last) {
- list_del(a);
- list_add(a, c);
- last = last->prev;
- }
- a = tmp;
- }
-}
/** Sort an entire array.
* Sorts an entire array using merge sort.
@@ -469,6 +410,69 @@ void _stp_map_sort (MAP map, int keynum, int dir)
} while (nmerges > 1);
}
+/** Get the top values from an array.
+ * Sorts an array such that the start of the array contains the top
+ * or bottom 'n' values. Use this when sorting the entire array
+ * would be too time-consuming and you are only interested in the
+ * highest or lowest values.
+ *
+ * @param map Map
+ * @param n Top (or bottom) number of elements. 0 sorts the entire array.
+ * @param keynum 0 for the value, or a positive number for the key number to sort on.
+ * @param dir Sort Direction. -1 for low-to-high. 1 for high-to-low.
+ * @sa _stp_map_sort()
+ */
+void _stp_map_sortn(MAP map, int n, int keynum, int dir)
+{
+ if (n == 0) {
+ _stp_map_sort(map, keynum, dir);
+ } else {
+ struct list_head *head = &map->head;
+ struct list_head *c, *a, *last, *tmp;
+ int type, num, swaps = 1;
+
+ if (list_empty(head))
+ return;
+
+ if (keynum)
+ (*map->get_key)((struct map_node *)head->next, keynum, &type);
+ else
+ type = ((struct map_node *)head->next)->map->type;
+
+ /* start off with a modified bubble sort of the first n elements */
+ while (swaps) {
+ num = n;
+ swaps = 0;
+ a = head->next;
+ c = a->next->next;
+ while ((a->next != head) && (--num > 0)) {
+ if (_stp_cmp(a, a->next, keynum, dir, type)) {
+ swaps++;
+ _stp_swap(a, a->next);
+ }
+ a = c->prev;
+ c = c->next;
+ }
+ }
+
+ /* Now use a kind of insertion sort for the rest of the array. */
+ /* Each element is tested to see if it should be be in the top 'n' */
+ last = a;
+ a = a->next;
+ while (a != head) {
+ tmp = a->next;
+ c = last;
+ while (c != head && _stp_cmp(c, a, keynum, dir, type))
+ c = c->prev;
+ if (c != last) {
+ list_del(a);
+ list_add(a, c);
+ last = last->prev;
+ }
+ a = tmp;
+ }
+ }
+}
static int print_keytype (char *fmt, int type, key_data *kd)
{