Skip to contentSkip to conversation

sort: Add obj_sort() for sorting all kinds of random-accessible objects

PATCH In review Lai Jiangshan 2 participants 2 messages Sep 12, 2008 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 sort: Add obj_sort() for sorting all kinds of random-accessible objects

current sort can only sort objects on continuous memory, but sometimes we need to sort objects on noncontinuous memory, this patch provide obj_sort() for this.

Sometimes a C struct(the container of objects, or objects) may be designed with some high-level language's semantic meaning. obj_sort() can sort for them.

obj_sort() is need for sort pids for cgroup.tasks file.

---

include/linux/sort.h +4 -0
index d534da2..d1e2f71 100644
@@ -7,4 +7,8 @@ void sort(void *base, size_t num, size_t size,
 	  int (*cmp)(const void *, const void *),
 	  void (*swap)(void *, void *, int));
 
+void obj_sort(void *container, size_t begin, size_t end,
+		int (*cmp)(const void *container, size_t left, size_t right),
+		void (*swap)(void *container, size_t left, size_t right));
+
 #endif
lib/sort.c +61 -1

80 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Lai Jiangshan

current sort can only sort objects on continuous memory, but sometimes we need to sort objects on noncontinuous memory, this patch provide obj_sort() for this.

Sometimes a C struct(the container of objects, or objects) may be designed with some high-level language's semantic meaning. obj_sort() can sort for them.

obj_sort() is need for sort pids for cgroup.tasks file.

Ok, if I understand this correctly, your new function basically changes:

cmp(void *a, void *b) to cmp(void *obj, int a, int b)
swap(void *a, void *b, int size) to swap(void *obj, int a, int b)

Since neither function actually looks at the data directly, it seems we could make a unified sort function with:

cmp(void *base, int a, int b)
swap(void *base, int a, int b, int size)

What is the underlying data structure you're sorting by the way? Years ago, I wrote a very nice merge sort for linked lists that I never got around to submitting.

Signature · 1 line
Mathematics is the supreme nostalgia of our time.