29e444
commit e91a48408662e591e1320b2cab6d3fcda7793b18
29e444
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
29e444
Date:   Fri Jun 28 16:24:16 2013 +0530
29e444
29e444
    Simplify strcoll implementation
29e444
12745e
diff --git glibc-2.17-c758a686/string/strcoll_l.c glibc-2.17-c758a686/string/strcoll_l.c
29e444
index ecda08f..1bb9e23 100644
12745e
--- glibc-2.17-c758a686/string/strcoll_l.c
12745e
+++ glibc-2.17-c758a686/string/strcoll_l.c
29e444
@@ -41,11 +41,244 @@
29e444
 
29e444
 #include "../locale/localeinfo.h"
29e444
 
29e444
+/* Track status while looking for sequences in a string.  */
29e444
+typedef struct
29e444
+{
29e444
+  int len;			/* Length of the current sequence.  */
29e444
+  int val;			/* Position of the sequence relative to the
29e444
+				   previous non-ignored sequence.  */
29e444
+  size_t idxnow;		/* Current index in sequences.  */
29e444
+  size_t idxmax;		/* Maximum index in sequences.  */
29e444
+  size_t idxcnt;		/* Current count of indeces.  */
29e444
+  size_t backw;			/* Current Backward sequence index.  */
29e444
+  size_t backw_stop;		/* Index where the backward sequences stop.  */
29e444
+  const USTRING_TYPE *us;	/* The string.  */
29e444
+  int32_t *idxarr;		/* Array to cache weight indeces.  */
29e444
+  unsigned char *rulearr;	/* Array to cache rules.  */
29e444
+} coll_seq;
29e444
+
29e444
+/* Get next sequence.  The weight indeces are cached, so we don't need to
29e444
+   traverse the string.  */
29e444
+static void
29e444
+get_next_seq_cached (coll_seq *seq, int nrules, int pass,
29e444
+		     const unsigned char *rulesets,
29e444
+		     const USTRING_TYPE *weights)
29e444
+{
29e444
+  int val = seq->val = 0;
29e444
+  int len = seq->len;
29e444
+  size_t backw_stop = seq->backw_stop;
29e444
+  size_t backw = seq->backw;
29e444
+  size_t idxcnt = seq->idxcnt;
29e444
+  size_t idxmax = seq->idxmax;
29e444
+  size_t idxnow = seq->idxnow;
29e444
+  unsigned char *rulearr = seq->rulearr;
29e444
+  int32_t *idxarr = seq->idxarr;
29e444
+
29e444
+  while (len == 0)
29e444
+    {
29e444
+      ++val;
29e444
+      if (backw_stop != ~0ul)
29e444
+	{
29e444
+	  /* The is something pushed.  */
29e444
+	  if (backw == backw_stop)
29e444
+	    {
29e444
+	      /* The last pushed character was handled.  Continue
29e444
+		 with forward characters.  */
29e444
+	      if (idxcnt < idxmax)
29e444
+		{
29e444
+		  idxnow = idxcnt;
29e444
+		  backw_stop = ~0ul;
29e444
+		}
29e444
+	      else
29e444
+		{
29e444
+		  /* Nothing anymore.  The backward sequence
29e444
+		     ended with the last sequence in the string.  */
29e444
+		  idxnow = ~0ul;
29e444
+		  break;
29e444
+		}
29e444
+	    }
29e444
+	  else
29e444
+	    idxnow = --backw;
29e444
+	}
29e444
+      else
29e444
+	{
29e444
+	  backw_stop = idxcnt;
29e444
+
29e444
+	  while (idxcnt < idxmax)
29e444
+	    {
29e444
+	      if ((rulesets[rulearr[idxcnt] * nrules + pass]
29e444
+		   & sort_backward) == 0)
29e444
+		/* No more backward characters to push.  */
29e444
+		break;
29e444
+	      ++idxcnt;
29e444
+	    }
29e444
+
29e444
+	  if (backw_stop == idxcnt)
29e444
+	    {
29e444
+	      /* No sequence at all or just one.  */
29e444
+	      if (idxcnt == idxmax)
29e444
+		/* Note that seq1len is still zero.  */
29e444
+		break;
29e444
+
29e444
+	      backw_stop = ~0ul;
29e444
+	      idxnow = idxcnt++;
29e444
+	    }
29e444
+	  else
29e444
+	    /* We pushed backward sequences.  */
29e444
+	    idxnow = backw = idxcnt - 1;
29e444
+	}
29e444
+      len = weights[idxarr[idxnow]++];
29e444
+    }
29e444
+
29e444
+  /* Update the structure.  */
29e444
+  seq->val = val;
29e444
+  seq->len = len;
29e444
+  seq->backw_stop = backw_stop;
29e444
+  seq->backw = backw;
29e444
+  seq->idxcnt = idxcnt;
29e444
+  seq->idxnow = idxnow;
29e444
+}
29e444
+
29e444
+/* Get next sequence.  Traverse the string as required.  */
29e444
+static void
29e444
+get_next_seq (coll_seq *seq, int nrules, const unsigned char *rulesets,
29e444
+	      const USTRING_TYPE *weights, const int32_t *table,
29e444
+	      const USTRING_TYPE *extra, const int32_t *indirect)
29e444
+{
29e444
+#include WEIGHT_H
29e444
+  int val = seq->val = 0;
29e444
+  int len = seq->len;
29e444
+  size_t backw_stop = seq->backw_stop;
29e444
+  size_t backw = seq->backw;
29e444
+  size_t idxcnt = seq->idxcnt;
29e444
+  size_t idxmax = seq->idxmax;
29e444
+  size_t idxnow = seq->idxnow;
29e444
+  unsigned char *rulearr = seq->rulearr;
29e444
+  int32_t *idxarr = seq->idxarr;
29e444
+  const USTRING_TYPE *us = seq->us;
29e444
+
29e444
+  while (len == 0)
29e444
+    {
29e444
+      ++val;
29e444
+      if (backw_stop != ~0ul)
29e444
+	{
29e444
+	  /* The is something pushed.  */
29e444
+	  if (backw == backw_stop)
29e444
+	    {
29e444
+	      /* The last pushed character was handled.  Continue
29e444
+		 with forward characters.  */
29e444
+	      if (idxcnt < idxmax)
29e444
+		{
29e444
+		  idxnow = idxcnt;
29e444
+		  backw_stop = ~0ul;
29e444
+		}
29e444
+	      else
29e444
+		/* Nothing anymore.  The backward sequence ended with
29e444
+		   the last sequence in the string.  Note that seq2len
29e444
+		   is still zero.  */
29e444
+		break;
29e444
+	    }
29e444
+	  else
29e444
+	    idxnow = --backw;
29e444
+	}
29e444
+      else
29e444
+	{
29e444
+	  backw_stop = idxmax;
29e444
+
29e444
+	  while (*us != L('\0'))
29e444
+	    {
29e444
+	      int32_t tmp = findidx (&us, -1);
29e444
+	      rulearr[idxmax] = tmp >> 24;
29e444
+	      idxarr[idxmax] = tmp & 0xffffff;
29e444
+	      idxcnt = idxmax++;
29e444
+
29e444
+	      if ((rulesets[rulearr[idxcnt] * nrules]
29e444
+		   & sort_backward) == 0)
29e444
+		/* No more backward characters to push.  */
29e444
+		break;
29e444
+	      ++idxcnt;
29e444
+	    }
29e444
+
29e444
+	  if (backw_stop >= idxcnt)
29e444
+	    {
29e444
+	      /* No sequence at all or just one.  */
29e444
+	      if (idxcnt == idxmax || backw_stop > idxcnt)
29e444
+		/* Note that seq1len is still zero.  */
29e444
+		break;
29e444
+
29e444
+	      backw_stop = ~0ul;
29e444
+	      idxnow = idxcnt;
29e444
+	    }
29e444
+	  else
29e444
+	    /* We pushed backward sequences.  */
29e444
+	    idxnow = backw = idxcnt - 1;
29e444
+	}
29e444
+      len = weights[idxarr[idxnow]++];
29e444
+    }
29e444
+
29e444
+  /* Update the structure.  */
29e444
+  seq->val = val;
29e444
+  seq->len = len;
29e444
+  seq->backw_stop = backw_stop;
29e444
+  seq->backw = backw;
29e444
+  seq->idxcnt = idxcnt;
29e444
+  seq->idxmax = idxmax;
29e444
+  seq->idxnow = idxnow;
29e444
+  seq->us = us;
29e444
+}
29e444
+
29e444
+/* Compare two sequences.  */
29e444
+static int
29e444
+do_compare (coll_seq *seq1, coll_seq *seq2, int position,
29e444
+	    const USTRING_TYPE *weights)
29e444
+{
29e444
+  int seq1len = seq1->len;
29e444
+  int seq2len = seq2->len;
29e444
+  int val1 = seq1->val;
29e444
+  int val2 = seq2->val;
29e444
+  int32_t *idx1arr = seq1->idxarr;
29e444
+  int32_t *idx2arr = seq2->idxarr;
29e444
+  int idx1now = seq1->idxnow;
29e444
+  int idx2now = seq2->idxnow;
29e444
+  int result = 0;
29e444
+
29e444
+  /* Test for position if necessary.  */
29e444
+  if (position && val1 != val2)
29e444
+    {
29e444
+      result = val1 - val2;
29e444
+      goto out;
29e444
+    }
29e444
+
29e444
+  /* Compare the two sequences.  */
29e444
+  do
29e444
+    {
29e444
+      if (weights[idx1arr[idx1now]] != weights[idx2arr[idx2now]])
29e444
+	{
29e444
+	  /* The sequences differ.  */
29e444
+	  result = weights[idx1arr[idx1now]] - weights[idx2arr[idx2now]];
29e444
+	  goto out;
29e444
+	}
29e444
+
29e444
+      /* Increment the offsets.  */
29e444
+      ++idx1arr[idx1now];
29e444
+      ++idx2arr[idx2now];
29e444
+
29e444
+      --seq1len;
29e444
+      --seq2len;
29e444
+    }
29e444
+  while (seq1len > 0 && seq2len > 0);
29e444
+
29e444
+  if (position && seq1len != seq2len)
29e444
+    result = seq1len - seq2len;
29e444
+
29e444
+out:
29e444
+  seq1->len = seq1len;
29e444
+  seq2->len = seq2len;
29e444
+  return result;
29e444
+}
29e444
+
29e444
 int
29e444
-STRCOLL (s1, s2, l)
29e444
-     const STRING_TYPE *s1;
29e444
-     const STRING_TYPE *s2;
29e444
-     __locale_t l;
29e444
+STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, __locale_t l)
29e444
 {
29e444
   struct __locale_data *current = l->__locales[LC_COLLATE];
29e444
   uint_fast32_t nrules = current->values[_NL_ITEM_INDEX (_NL_COLLATE_NRULES)].word;
29e444
@@ -56,34 +289,6 @@ STRCOLL (s1, s2, l)
29e444
   const USTRING_TYPE *weights;
29e444
   const USTRING_TYPE *extra;
29e444
   const int32_t *indirect;
29e444
-  uint_fast32_t pass;
29e444
-  int result = 0;
29e444
-  const USTRING_TYPE *us1;
29e444
-  const USTRING_TYPE *us2;
29e444
-  size_t s1len;
29e444
-  size_t s2len;
29e444
-  int32_t *idx1arr;
29e444
-  int32_t *idx2arr;
29e444
-  unsigned char *rule1arr;
29e444
-  unsigned char *rule2arr;
29e444
-  size_t idx1max;
29e444
-  size_t idx2max;
29e444
-  size_t idx1cnt;
29e444
-  size_t idx2cnt;
29e444
-  size_t idx1now;
29e444
-  size_t idx2now;
29e444
-  size_t backw1_stop;
29e444
-  size_t backw2_stop;
29e444
-  size_t backw1;
29e444
-  size_t backw2;
29e444
-  int val1;
29e444
-  int val2;
29e444
-  int position;
29e444
-  int seq1len;
29e444
-  int seq2len;
29e444
-  int use_malloc;
29e444
-
29e444
-#include WEIGHT_H
29e444
 
29e444
   if (nrules == 0)
29e444
     return STRCMP (s1, s2);
29e444
@@ -98,7 +303,6 @@ STRCOLL (s1, s2, l)
29e444
     current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_EXTRA,SUFFIX))].string;
29e444
   indirect = (const int32_t *)
29e444
     current->values[_NL_ITEM_INDEX (CONCAT(_NL_COLLATE_INDIRECT,SUFFIX))].string;
29e444
-  use_malloc = 0;
29e444
 
29e444
   assert (((uintptr_t) table) % __alignof__ (table[0]) == 0);
29e444
   assert (((uintptr_t) weights) % __alignof__ (weights[0]) == 0);
29e444
@@ -106,18 +310,13 @@ STRCOLL (s1, s2, l)
29e444
   assert (((uintptr_t) indirect) % __alignof__ (indirect[0]) == 0);
29e444
 
29e444
   /* We need this a few times.  */
29e444
-  s1len = STRLEN (s1);
29e444
-  s2len = STRLEN (s2);
29e444
+  size_t s1len = STRLEN (s1);
29e444
+  size_t s2len = STRLEN (s2);
29e444
 
29e444
   /* Catch empty strings.  */
29e444
-  if (__builtin_expect (s1len == 0, 0) || __builtin_expect (s2len == 0, 0))
29e444
+  if (__glibc_unlikely (s1len == 0) || __glibc_unlikely (s2len == 0))
29e444
     return (s1len != 0) - (s2len != 0);
29e444
 
29e444
-  /* We need the elements of the strings as unsigned values since they
29e444
-     are used as indeces.  */
29e444
-  us1 = (const USTRING_TYPE *) s1;
29e444
-  us2 = (const USTRING_TYPE *) s2;
29e444
-
29e444
   /* Perform the first pass over the string and while doing this find
29e444
      and store the weights for each character.  Since we want this to
29e444
      be as fast as possible we are using `alloca' to store the temporary
29e444
@@ -127,14 +326,27 @@ STRCOLL (s1, s2, l)
29e444
 
29e444
      Please note that the localedef programs makes sure that `position'
29e444
      is not used at the first level.  */
29e444
+
29e444
+  coll_seq seq1, seq2;
29e444
+  bool use_malloc = false;
29e444
+  int result = 0;
29e444
+
29e444
+  memset (&seq1, 0, sizeof (seq1));
29e444
+  seq2 = seq1;
29e444
+
29e444
+  /* We need the elements of the strings as unsigned values since they
29e444
+     are used as indeces.  */
29e444
+  seq1.us = (const USTRING_TYPE *) s1;
29e444
+  seq2.us = (const USTRING_TYPE *) s2;
29e444
+
29e444
   if (! __libc_use_alloca ((s1len + s2len) * (sizeof (int32_t) + 1)))
29e444
     {
29e444
-      idx1arr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1));
29e444
-      idx2arr = &idx1arr[s1len];
29e444
-      rule1arr = (unsigned char *) &idx2arr[s2len];
29e444
-      rule2arr = &rule1arr[s1len];
29e444
+      seq1.idxarr = (int32_t *) malloc ((s1len + s2len) * (sizeof (int32_t) + 1));
29e444
+      seq2.idxarr = &seq1.idxarr[s1len];
29e444
+      seq1.rulearr = (unsigned char *) &seq2.idxarr[s2len];
29e444
+      seq2.rulearr = &seq1.rulearr[s1len];
29e444
 
29e444
-      if (idx1arr == NULL)
29e444
+      if (seq1.idxarr == NULL)
29e444
 	/* No memory.  Well, go with the stack then.
29e444
 
29e444
 	   XXX Once this implementation is stable we will handle this
29e444
@@ -142,396 +354,73 @@ STRCOLL (s1, s2, l)
29e444
 	   do this in time.  This means, though, that this happens for
29e444
 	   every pass again.  */
29e444
 	goto try_stack;
29e444
-      use_malloc = 1;
29e444
+      use_malloc = true;
29e444
     }
29e444
   else
29e444
     {
29e444
     try_stack:
29e444
-      idx1arr = (int32_t *) alloca (s1len * sizeof (int32_t));
29e444
-      idx2arr = (int32_t *) alloca (s2len * sizeof (int32_t));
29e444
-      rule1arr = (unsigned char *) alloca (s1len);
29e444
-      rule2arr = (unsigned char *) alloca (s2len);
29e444
+      seq1.idxarr = (int32_t *) alloca (s1len * sizeof (int32_t));
29e444
+      seq2.idxarr = (int32_t *) alloca (s2len * sizeof (int32_t));
29e444
+      seq1.rulearr = (unsigned char *) alloca (s1len);
29e444
+      seq2.rulearr = (unsigned char *) alloca (s2len);
29e444
     }
29e444
 
29e444
-  idx1cnt = 0;
29e444
-  idx2cnt = 0;
29e444
-  idx1max = 0;
29e444
-  idx2max = 0;
29e444
-  idx1now = 0;
29e444
-  idx2now = 0;
29e444
-  backw1_stop = ~0ul;
29e444
-  backw2_stop = ~0ul;
29e444
-  backw1 = ~0ul;
29e444
-  backw2 = ~0ul;
29e444
-  seq1len = 0;
29e444
-  seq2len = 0;
29e444
-  position = rulesets[0] & sort_position;
29e444
-  while (1)
29e444
-    {
29e444
-      val1 = 0;
29e444
-      val2 = 0;
29e444
-
29e444
-      /* Get the next non-IGNOREd element for string `s1'.  */
29e444
-      if (seq1len == 0)
29e444
-	do
29e444
-	  {
29e444
-	    ++val1;
29e444
-
29e444
-	    if (backw1_stop != ~0ul)
29e444
-	      {
29e444
-		/* The is something pushed.  */
29e444
-		if (backw1 == backw1_stop)
29e444
-		  {
29e444
-		    /* The last pushed character was handled.  Continue
29e444
-		       with forward characters.  */
29e444
-		    if (idx1cnt < idx1max)
29e444
-		      {
29e444
-			idx1now = idx1cnt;
29e444
-			backw1_stop = ~0ul;
29e444
-		      }
29e444
-		    else
29e444
-		      /* Nothing anymore.  The backward sequence ended with
29e444
-			 the last sequence in the string.  Note that seq1len
29e444
-			 is still zero.  */
29e444
-		      break;
29e444
-		  }
29e444
-		else
29e444
-		  idx1now = --backw1;
29e444
-	      }
29e444
-	    else
29e444
-	      {
29e444
-		backw1_stop = idx1max;
29e444
-
29e444
-		while (*us1 != L('\0'))
29e444
-		  {
29e444
-		    int32_t tmp = findidx (&us1, -1);
29e444
-		    rule1arr[idx1max] = tmp >> 24;
29e444
-		    idx1arr[idx1max] = tmp & 0xffffff;
29e444
-		    idx1cnt = idx1max++;
29e444
-
29e444
-		    if ((rulesets[rule1arr[idx1cnt] * nrules]
29e444
-			 & sort_backward) == 0)
29e444
-		      /* No more backward characters to push.  */
29e444
-		      break;
29e444
-		    ++idx1cnt;
29e444
-		  }
29e444
-
29e444
-		if (backw1_stop >= idx1cnt)
29e444
-		  {
29e444
-		    /* No sequence at all or just one.  */
29e444
-		    if (idx1cnt == idx1max || backw1_stop > idx1cnt)
29e444
-		      /* Note that seq1len is still zero.  */
29e444
-		      break;
29e444
-
29e444
-		    backw1_stop = ~0ul;
29e444
-		    idx1now = idx1cnt;
29e444
-		  }
29e444
-		else
29e444
-		  /* We pushed backward sequences.  */
29e444
-		  idx1now = backw1 = idx1cnt - 1;
29e444
-	      }
29e444
-	  }
29e444
-	while ((seq1len = weights[idx1arr[idx1now]++]) == 0);
29e444
-
29e444
-      /* And the same for string `s2'.  */
29e444
-      if (seq2len == 0)
29e444
-	do
29e444
-	  {
29e444
-	    ++val2;
29e444
-
29e444
-	    if (backw2_stop != ~0ul)
29e444
-	      {
29e444
-		/* The is something pushed.  */
29e444
-		if (backw2 == backw2_stop)
29e444
-		  {
29e444
-		    /* The last pushed character was handled.  Continue
29e444
-		       with forward characters.  */
29e444
-		    if (idx2cnt < idx2max)
29e444
-		      {
29e444
-			idx2now = idx2cnt;
29e444
-			backw2_stop = ~0ul;
29e444
-		      }
29e444
-		    else
29e444
-		      /* Nothing anymore.  The backward sequence ended with
29e444
-			 the last sequence in the string.  Note that seq2len
29e444
-			 is still zero.  */
29e444
-		      break;
29e444
-		  }
29e444
-		else
29e444
-		  idx2now = --backw2;
29e444
-	      }
29e444
-	    else
29e444
-	      {
29e444
-		backw2_stop = idx2max;
29e444
-
29e444
-		while (*us2 != L('\0'))
29e444
-		  {
29e444
-		    int32_t tmp = findidx (&us2, -1);
29e444
-		    rule2arr[idx2max] = tmp >> 24;
29e444
-		    idx2arr[idx2max] = tmp & 0xffffff;
29e444
-		    idx2cnt = idx2max++;
29e444
-
29e444
-		    if ((rulesets[rule2arr[idx2cnt] * nrules]
29e444
-			 & sort_backward) == 0)
29e444
-		      /* No more backward characters to push.  */
29e444
-		      break;
29e444
-		    ++idx2cnt;
29e444
-		  }
29e444
-
29e444
-		if (backw2_stop >= idx2cnt)
29e444
-		  {
29e444
-		    /* No sequence at all or just one.  */
29e444
-		    if (idx2cnt == idx2max || backw2_stop > idx2cnt)
29e444
-		      /* Note that seq1len is still zero.  */
29e444
-		      break;
29e444
-
29e444
-		    backw2_stop = ~0ul;
29e444
-		    idx2now = idx2cnt;
29e444
-		  }
29e444
-		else
29e444
-		  /* We pushed backward sequences.  */
29e444
-		  idx2now = backw2 = idx2cnt - 1;
29e444
-	      }
29e444
-	  }
29e444
-	while ((seq2len = weights[idx2arr[idx2now]++]) == 0);
29e444
-
29e444
-      /* See whether any or both strings are empty.  */
29e444
-      if (seq1len == 0 || seq2len == 0)
29e444
-	{
29e444
-	  if (seq1len == seq2len)
29e444
-	    /* Both ended.  So far so good, both strings are equal at the
29e444
-	       first level.  */
29e444
-	    break;
29e444
-
29e444
-	  /* This means one string is shorter than the other.  Find out
29e444
-	     which one and return an appropriate value.  */
29e444
-	  result = seq1len == 0 ? -1 : 1;
29e444
-	  goto free_and_return;
29e444
-	}
29e444
+  seq1.rulearr[0] = 0;
29e444
 
29e444
-      /* Test for position if necessary.  */
29e444
-      if (position && val1 != val2)
29e444
-	{
29e444
-	  result = val1 - val2;
29e444
-	  goto free_and_return;
29e444
-	}
29e444
-
29e444
-      /* Compare the two sequences.  */
29e444
-      do
29e444
-	{
29e444
-	  if (weights[idx1arr[idx1now]] != weights[idx2arr[idx2now]])
29e444
-	    {
29e444
-	      /* The sequences differ.  */
29e444
-	      result = weights[idx1arr[idx1now]] - weights[idx2arr[idx2now]];
29e444
-	      goto free_and_return;
29e444
-	    }
29e444
-
29e444
-	  /* Increment the offsets.  */
29e444
-	  ++idx1arr[idx1now];
29e444
-	  ++idx2arr[idx2now];
29e444
-
29e444
-	  --seq1len;
29e444
-	  --seq2len;
29e444
-	}
29e444
-      while (seq1len > 0 && seq2len > 0);
29e444
-
29e444
-      if (position && seq1len != seq2len)
29e444
-	{
29e444
-	  result = seq1len - seq2len;
29e444
-	  goto free_and_return;
29e444
-	}
29e444
-    }
29e444
-
29e444
-  /* Now the remaining passes over the weights.  We now use the
29e444
-     indeces we found before.  */
29e444
-  for (pass = 1; pass < nrules; ++pass)
29e444
+  /* Cache values in the first pass and if needed, use them in subsequent
29e444
+     passes.  */
29e444
+  for (int pass = 0; pass < nrules; ++pass)
29e444
     {
29e444
+      seq1.idxcnt = 0;
29e444
+      seq1.backw_stop = ~0ul;
29e444
+      seq1.backw = ~0ul;
29e444
+      seq2.idxcnt = 0;
29e444
+      seq2.backw_stop = ~0ul;
29e444
+      seq2.backw = ~0ul;
29e444
+
29e444
       /* We assume that if a rule has defined `position' in one section
29e444
 	 this is true for all of them.  */
29e444
-      idx1cnt = 0;
29e444
-      idx2cnt = 0;
29e444
-      backw1_stop = ~0ul;
29e444
-      backw2_stop = ~0ul;
29e444
-      backw1 = ~0ul;
29e444
-      backw2 = ~0ul;
29e444
-      position = rulesets[rule1arr[0] * nrules + pass] & sort_position;
29e444
+      int position = rulesets[seq1.rulearr[0] * nrules + pass] & sort_position;
29e444
 
29e444
       while (1)
29e444
 	{
29e444
-	  val1 = 0;
29e444
-	  val2 = 0;
29e444
-
29e444
-	  /* Get the next non-IGNOREd element for string `s1'.  */
29e444
-	  if (seq1len == 0)
29e444
-	    do
29e444
-	      {
29e444
-		++val1;
29e444
-
29e444
-		if (backw1_stop != ~0ul)
29e444
-		  {
29e444
-		    /* The is something pushed.  */
29e444
-		    if (backw1 == backw1_stop)
29e444
-		      {
29e444
-			/* The last pushed character was handled.  Continue
29e444
-			   with forward characters.  */
29e444
-			if (idx1cnt < idx1max)
29e444
-			  {
29e444
-			    idx1now = idx1cnt;
29e444
-			    backw1_stop = ~0ul;
29e444
-			  }
29e444
-			else
29e444
-			  {
29e444
-			    /* Nothing anymore.  The backward sequence
29e444
-			       ended with the last sequence in the string.  */
29e444
-			    idx1now = ~0ul;
29e444
-			    break;
29e444
-			  }
29e444
-		      }
29e444
-		    else
29e444
-		      idx1now = --backw1;
29e444
-		  }
29e444
-		else
29e444
-		  {
29e444
-		    backw1_stop = idx1cnt;
29e444
-
29e444
-		    while (idx1cnt < idx1max)
29e444
-		      {
29e444
-			if ((rulesets[rule1arr[idx1cnt] * nrules + pass]
29e444
-			     & sort_backward) == 0)
29e444
-			  /* No more backward characters to push.  */
29e444
-			  break;
29e444
-			++idx1cnt;
29e444
-		      }
29e444
-
29e444
-		    if (backw1_stop == idx1cnt)
29e444
-		      {
29e444
-			/* No sequence at all or just one.  */
29e444
-			if (idx1cnt == idx1max)
29e444
-			  /* Note that seq1len is still zero.  */
29e444
-			  break;
29e444
-
29e444
-			backw1_stop = ~0ul;
29e444
-			idx1now = idx1cnt++;
29e444
-		      }
29e444
-		    else
29e444
-		      /* We pushed backward sequences.  */
29e444
-		      idx1now = backw1 = idx1cnt - 1;
29e444
-		  }
29e444
-	      }
29e444
-	    while ((seq1len = weights[idx1arr[idx1now]++]) == 0);
29e444
-
29e444
-	  /* And the same for string `s2'.  */
29e444
-	  if (seq2len == 0)
29e444
-	    do
29e444
-	      {
29e444
-		++val2;
29e444
-
29e444
-		if (backw2_stop != ~0ul)
29e444
-		  {
29e444
-		    /* The is something pushed.  */
29e444
-		    if (backw2 == backw2_stop)
29e444
-		      {
29e444
-			/* The last pushed character was handled.  Continue
29e444
-			   with forward characters.  */
29e444
-			if (idx2cnt < idx2max)
29e444
-			  {
29e444
-			    idx2now = idx2cnt;
29e444
-			    backw2_stop = ~0ul;
29e444
-			  }
29e444
-			else
29e444
-			  {
29e444
-			    /* Nothing anymore.  The backward sequence
29e444
-			       ended with the last sequence in the string.  */
29e444
-			    idx2now = ~0ul;
29e444
-			    break;
29e444
-			  }
29e444
-		      }
29e444
-		    else
29e444
-		      idx2now = --backw2;
29e444
-		  }
29e444
-		else
29e444
-		  {
29e444
-		    backw2_stop = idx2cnt;
29e444
-
29e444
-		    while (idx2cnt < idx2max)
29e444
-		      {
29e444
-			if ((rulesets[rule2arr[idx2cnt] * nrules + pass]
29e444
-			     & sort_backward) == 0)
29e444
-			  /* No more backward characters to push.  */
29e444
-			  break;
29e444
-			++idx2cnt;
29e444
-		      }
29e444
-
29e444
-		    if (backw2_stop == idx2cnt)
29e444
-		      {
29e444
-			/* No sequence at all or just one.  */
29e444
-			if (idx2cnt == idx2max)
29e444
-			  /* Note that seq2len is still zero.  */
29e444
-			  break;
29e444
-
29e444
-			backw2_stop = ~0ul;
29e444
-			idx2now = idx2cnt++;
29e444
-		      }
29e444
-		    else
29e444
-		      /* We pushed backward sequences.  */
29e444
-		      idx2now = backw2 = idx2cnt - 1;
29e444
-		  }
29e444
-	      }
29e444
-	    while ((seq2len = weights[idx2arr[idx2now]++]) == 0);
29e444
+	  if (pass == 0)
29e444
+	    {
29e444
+	      get_next_seq (&seq1, nrules, rulesets, weights, table, extra,
29e444
+			    indirect);
29e444
+	      get_next_seq (&seq2, nrules, rulesets, weights, table, extra,
29e444
+			    indirect);
29e444
+	    }
29e444
+	  else
29e444
+	    {
29e444
+	      get_next_seq_cached (&seq1, nrules, pass, rulesets, weights);
29e444
+	      get_next_seq_cached (&seq2, nrules, pass, rulesets, weights);
29e444
+	    }
29e444
 
29e444
 	  /* See whether any or both strings are empty.  */
29e444
-	  if (seq1len == 0 || seq2len == 0)
29e444
+	  if (seq1.len == 0 || seq2.len == 0)
29e444
 	    {
29e444
-	      if (seq1len == seq2len)
29e444
+	      if (seq1.len == seq2.len)
29e444
 		/* Both ended.  So far so good, both strings are equal
29e444
 		   at this level.  */
29e444
 		break;
29e444
 
29e444
 	      /* This means one string is shorter than the other.  Find out
29e444
 		 which one and return an appropriate value.  */
29e444
-	      result = seq1len == 0 ? -1 : 1;
29e444
+	      result = seq1.len == 0 ? -1 : 1;
29e444
 	      goto free_and_return;
29e444
 	    }
29e444
 
29e444
-	  /* Test for position if necessary.  */
29e444
-	  if (position && val1 != val2)
29e444
-	    {
29e444
-	      result = val1 - val2;
29e444
-	      goto free_and_return;
29e444
-	    }
29e444
-
29e444
-	  /* Compare the two sequences.  */
29e444
-	  do
29e444
-	    {
29e444
-	      if (weights[idx1arr[idx1now]] != weights[idx2arr[idx2now]])
29e444
-		{
29e444
-		  /* The sequences differ.  */
29e444
-		  result = (weights[idx1arr[idx1now]]
29e444
-			    - weights[idx2arr[idx2now]]);
29e444
-		  goto free_and_return;
29e444
-		}
29e444
-
29e444
-	      /* Increment the offsets.  */
29e444
-	      ++idx1arr[idx1now];
29e444
-	      ++idx2arr[idx2now];
29e444
-
29e444
-	      --seq1len;
29e444
-	      --seq2len;
29e444
-	    }
29e444
-	  while (seq1len > 0 && seq2len > 0);
29e444
-
29e444
-	  if (position && seq1len != seq2len)
29e444
-	    {
29e444
-	      result = seq1len - seq2len;
29e444
-	      goto free_and_return;
29e444
-	    }
29e444
+	  result = do_compare (&seq1, &seq2, position, weights);
29e444
+	  if (result != 0)
29e444
+	    goto free_and_return;
29e444
 	}
29e444
     }
29e444
 
29e444
   /* Free the memory if needed.  */
29e444
  free_and_return:
29e444
   if (use_malloc)
29e444
-    free (idx1arr);
29e444
+    free (seq1.idxarr);
29e444
 
29e444
   return result;
29e444
 }