bug-texinfo
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Unstable sorting of index items


From: Ben Leslie
Subject: Unstable sorting of index items
Date: Thu, 10 Jan 2013 02:11:22 +1100

Hi,

The algorithm used to sort index elements is 'qsort', which is unstable, which means the output from makeinfo is non-deterministic.
The following patch addresses the issue (assuming not more than 1 index entry is allowed per line.)

Apart from the inherent desirability of having a deterministic tool, the non-deterministic approach can lead to duplicated index items for the same node if these index items are made non-adjacent during the search.
When this occurs the netry is not detected as duplicate in the make_index_entries_unique function.

Thanks,

Benno

diff --git a/makeinfo/index.c b/makeinfo/index.c
index 65d7484..b34025c 100644
--- a/makeinfo/index.c
+++ b/makeinfo/index.c
@@ -505,10 +505,23 @@ cm_tindex (void)                    /* Data Type index. */
 static int
 index_element_compare (const void *element1, const void *element2)
 {
+  int cmp;
   INDEX_ELT **elt1 = (INDEX_ELT **) element1;
   INDEX_ELT **elt2 = (INDEX_ELT **) element2;
 
-  return index_compare_fn ((*elt1)->entry, (*elt2)->entry);
+  cmp = index_compare_fn ((*elt1)->entry, (*elt2)->entry);
+  if (cmp != 0)
+    {
+      return cmp;
+    }
+  /* entries are equal, should compare node names */
+  cmp = index_compare_fn ((*elt1)->node, (*elt2)->node);
+  if (cmp != 0)
+    {
+      return cmp;
+    }
+  /* entries and nodes are equal, finally compare by line number */
+  return (*elt1)->defining_line - (*elt2)->defining_line;
 }
 
 /* Force all index entries to be unique. */

reply via email to

[Prev in Thread] Current Thread [Next in Thread]