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