]> mj.ucw.cz Git - libucw.git/commitdiff
ucw docs: Simple array sorter
authorMichal Vaner <vorner@ucw.cz>
Sun, 14 Dec 2008 10:17:28 +0000 (11:17 +0100)
committerMichal Vaner <vorner@ucw.cz>
Sun, 14 Dec 2008 10:17:28 +0000 (11:17 +0100)
ucw/doc/Makefile
ucw/doc/generic.txt
ucw/doc/index.txt
ucw/doc/sort.txt [new file with mode: 0644]
ucw/sorter/array-simple.h

index ecb22d5c046006dec925acf7056c4a94a839452c..b1364848e06bacf3097299b563ff0a22bc555161 100644 (file)
@@ -2,7 +2,7 @@
 
 DIRS+=ucw/doc
 
-UCW_DOCS=fastbuf index config configure install basecode hash docsys conf mempool eltpool mainloop generic growbuf unaligned lists chartype unicode prime binsearch heap binheap compress
+UCW_DOCS=fastbuf index config configure install basecode hash docsys conf mempool eltpool mainloop generic growbuf unaligned lists chartype unicode prime binsearch heap binheap compress sort
 UCW_INDEX=$(o)/ucw/doc/def_index.html
 UCW_DOCS_HTML=$(addprefix $(o)/ucw/doc/,$(addsuffix .html,$(UCW_DOCS)))
 
index ff61488a004fdde8ac0b70876bcb0c5872b2229c..2c73f94da7a3a304ca48387f9af083d34a31a20d 100644 (file)
@@ -11,6 +11,7 @@ libUCW, and also hints for use of these structures.
 - <<implement,How it is implemented>>
 - Modules with generics
   * <<growbuf:gbuf,`gbuf.h`>>
+  * <<sort:,Sorting>>
   * <<binheap:,`binheap.h`>>
 
 // TODO The module list
index 69823cd15ddf4339a31f266e3a2a52b7777820e2..946aff67b3f7e8d145c31baca380ef9e0e58dc33 100644 (file)
@@ -27,6 +27,7 @@ Modules
 - <<chartype:,Single-byte characters>>
 - <<unicode:,Multi-byte characters>>
 - <<prime:,Prime numbers>>
+- <<sort:,Sorting>>
 - <<binsearch:,Binary search>>
 - <<heap:,Binary heaps>>
 - <<binheap:,Binomial heaps>>
@@ -41,8 +42,6 @@ Other features
 
 Yet undocumented modules
 ------------------------
-- Sorting
-  * `sorter/`
 - Hash tables
   * `hashtable.h`
 - Trie
diff --git a/ucw/doc/sort.txt b/ucw/doc/sort.txt
new file mode 100644 (file)
index 0000000..0fbc1d2
--- /dev/null
@@ -0,0 +1,89 @@
+Sorting
+=======
+
+A very common need is sorting data. Therefore libUCW contains few
+routines to accomplish that task. They are much more universal than
+qsort(), since they allow you to sort structures indexed by a macro,
+sort data externally, if they do not fit into memory, merge data with
+the same keys and sort data of variable length.
+
+All routines described below are <<generic:,generic algorithms>>.
+
+- <<array-simple,Simple array sorting>>
+  * <<mandatory-simple,Mandatory macros>>
+  * <<optional-simple,Optional macros>>
+  * <<example-simple,Example>>
+- <<array,Advanced array sorting>>
+- <<external,External sorting>>
+
+[[array-simple]]
+Simple array sorting
+--------------------
+
+If you want to sort some data in memory and you aren't too picky about
+setting how, you just use the routine defined in
+`sorter/array-simple.h`. It is an optimised hybrid
+quick-sort/insert-sort algorithm (quick-sort is used to split the
+input into small parts, each is then sorted by insert-sort).
+
+You need to define few macros and include the header. You get a
+sorting function in return. It will be called
+<<fun__GENERIC_LINK_|ASORT_PREFIX|sort|,`ASORT_PREFIX(sort)`>>.
+
+[mandatory-simple]
+Mandatory macros
+~~~~~~~~~~~~~~~~
+- `ASORT_PREFIX(name)` -- The identifier generating macro.
+- `ASORT_KEY_TYPE` -- Data type of a single array entry key.
+
+[optional-simple]
+Optional macros
+~~~~~~~~~~~~~~~
+- `ASORT_ELT(i)` -- Indexing macro. Returns the key of the
+  corresponding entry. If not provided, usual array with sequential
+  indexing is assumed.
+- `ASORT_LT(x,y)` -- Comparing macro. If not provided, compares by the
+  `<` operator.
+- `ASORT_SWAP(i,j)` -- Swap elements with indices `i` and `j`. If not
+  provided, it assumes `ASORT_ELT` is l-value and it just swaps keys.
+- `ASORT_TRESHOLD` -- Sequences of at most this amount of elements are
+  sorted by quick-sort, smaller are sorted by insert-sort. Defaults to
+  `8` (result of experimentation).
+- `ASORT_EXTRA_ARGS` -- Pass some extra arguments to the function.
+  They are visible from all the macros. Must start with a comma.
+
+!!ucw/sorter/array-simple.h ASORT_PREFIX
+
+[example-simple]
+Example
+~~~~~~~
+
+Let's sort an array of integers, in the usual way.
+
+  #define ASORT_PREFIX(X) intarr_##X
+  #define ASORT_TYPE int
+  #include <ucw/sorter/array-simple.h>
+
+This generates an intarr_sort(int *array, uns array_size) function that
+can be used the obvious way.
+
+A more complicated example could be sorting a structure, where items
+with odd indices are stored in one array, even in another. Each item
+could be a structure containing a string and an integer. We would like
+to sort them by the strings.
+
+  struct elem {
+    char *string;
+    int integer;
+  };
+
+  #include <string.h>  // Because of strcmp
+  #define ASORT_PREFIX(X) complicated_##X
+  #define ASORT_TYPE struct elem
+  #define ASORT_ELT(i) ((i % 2 ? even_array : odd_array)[i / 2])
+  #define ASORT_LT(x, y) (strcmp((x).string, (y).string) < 0)
+  #define ASORT_EXTRA_ARGS , struct elem *odd_array, struct elem *even_array
+  #include <ucw/sorter/sorter/array-simple.h>
+
+Now we got a complicated_sort(uns array_size, struct elem *odd_array,
+struct *even_array) function to perform our sorting.
index 393ebd9315219c78c31e01632fb2e3d34676bce6..5e442439f78e8df98ef40eb7cb2fbfbfa4bbaf3a 100644 (file)
 #endif
 
 #ifndef ASORT_ELT
-#define ASORT_ARRAY_ARG
+#define ASORT_ARRAY_ARG ASORT_KEY_TYPE *array,
 #define ASORT_ELT(i) array[i]
+#else
+#define ASORT_ARRAY_ARG
 #endif
 
-static void ASORT_PREFIX(sort)(
-#ifdef ASORT_ARRAY_ARG
-  ASORT_KEY_TYPE *array,
-#endif
-  uns array_size ASORT_EXTRA_ARGS)
+/**
+ * The generated sorting function. If `ASORT_ELT` macro is not provided, the
+ * @ASORT_ARRAY_ARG is equal to `ASORT_KEY_TYPE *array` and is the array to be
+ * sorted. If the macro is provided, this parameter is omitted. In that case,
+ * you can sort global variables or pass your structure by @ASORT_EXTRA_ARGS.
+ **/
+static void ASORT_PREFIX(sort)(ASORT_ARRAY_ARG uns array_size ASORT_EXTRA_ARGS)
 {
   struct stk { int l, r; } stack[8*sizeof(uns)];
   int l, r, left, right, m;