073263
To: vim_dev@googlegroups.com
073263
Subject: Patch 7.4.351
073263
Fcc: outbox
073263
From: Bram Moolenaar <Bram@moolenaar.net>
073263
Mime-Version: 1.0
073263
Content-Type: text/plain; charset=UTF-8
073263
Content-Transfer-Encoding: 8bit
073263
------------
073263
073263
Patch 7.4.351
073263
Problem:    sort() is not stable.
073263
Solution:   When the items are identical, compare the pointers.
073263
Files:	    src/eval.c, src/testdir/test55.in, src/testdir/test55.ok
073263
073263
073263
*** ../vim-7.4.350/src/eval.c	2014-06-25 17:31:04.942737863 +0200
073263
--- src/eval.c	2014-07-02 18:52:19.102313288 +0200
073263
***************
073263
*** 17334,17339 ****
073263
--- 17334,17340 ----
073263
  static char_u	*item_compare_func;
073263
  static dict_T	*item_compare_selfdict;
073263
  static int	item_compare_func_err;
073263
+ static int	item_compare_keep_zero;
073263
  static void	do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
073263
  #define ITEM_COMPARE_FAIL 999
073263
  
073263
***************
073263
*** 17374,17379 ****
073263
--- 17375,17386 ----
073263
  	n2 = strtod((char *)p2, (char **)&p2;;
073263
  	res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
073263
      }
073263
+ 
073263
+     /* When the result would be zero, compare the pointers themselves.  Makes
073263
+      * the sort stable. */
073263
+     if (res == 0 && !item_compare_keep_zero)
073263
+ 	res = s1 > s2 ? 1 : -1;
073263
+ 
073263
      vim_free(tofree1);
073263
      vim_free(tofree2);
073263
      return res;
073263
***************
073263
*** 17396,17402 ****
073263
      if (item_compare_func_err)
073263
  	return 0;
073263
  
073263
!     /* copy the values.  This is needed to be able to set v_lock to VAR_FIXED
073263
       * in the copy without changing the original list items. */
073263
      copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
073263
      copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
073263
--- 17403,17409 ----
073263
      if (item_compare_func_err)
073263
  	return 0;
073263
  
073263
!     /* Copy the values.  This is needed to be able to set v_lock to VAR_FIXED
073263
       * in the copy without changing the original list items. */
073263
      copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
073263
      copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
073263
***************
073263
*** 17415,17420 ****
073263
--- 17422,17433 ----
073263
      if (item_compare_func_err)
073263
  	res = ITEM_COMPARE_FAIL;  /* return value has wrong type */
073263
      clear_tv(&rettv);
073263
+ 
073263
+     /* When the result would be zero, compare the pointers themselves.  Makes
073263
+      * the sort stable. */
073263
+     if (res == 0 && !item_compare_keep_zero)
073263
+ 	res = s1 > s2 ? 1 : -1;
073263
+ 
073263
      return res;
073263
  }
073263
  
073263
***************
073263
*** 17509,17514 ****
073263
--- 17522,17528 ----
073263
  		ptrs[i++] = li;
073263
  
073263
  	    item_compare_func_err = FALSE;
073263
+ 	    item_compare_keep_zero = FALSE;
073263
  	    /* test the compare function */
073263
  	    if (item_compare_func != NULL
073263
  		    && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
073263
***************
073263
*** 17536,17541 ****
073263
--- 17550,17556 ----
073263
  
073263
  	    /* f_uniq(): ptrs will be a stack of items to remove */
073263
  	    item_compare_func_err = FALSE;
073263
+ 	    item_compare_keep_zero = TRUE;
073263
  	    item_compare_func_ptr = item_compare_func
073263
  					       ? item_compare2 : item_compare;
073263
  
073263
*** ../vim-7.4.350/src/testdir/test55.in	2014-06-26 22:33:47.850693627 +0200
073263
--- src/testdir/test55.in	2014-07-02 19:00:09.238320492 +0200
073263
***************
073263
*** 332,340 ****
073263
  :$put =string(reverse(sort(l)))
073263
  :$put =string(sort(reverse(sort(l))))
073263
  :$put =string(uniq(sort(l)))
073263
! :let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0.22, 'foo']
073263
  :$put =string(sort(copy(l), 'n'))
073263
! :let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'foo', 'FOOBAR',{}, []]
073263
  :$put =string(sort(copy(l), 1))
073263
  :$put =string(sort(copy(l), 'i'))
073263
  :$put =string(sort(copy(l)))
073263
--- 332,340 ----
073263
  :$put =string(reverse(sort(l)))
073263
  :$put =string(sort(reverse(sort(l))))
073263
  :$put =string(uniq(sort(l)))
073263
! :let l=[7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four']
073263
  :$put =string(sort(copy(l), 'n'))
073263
! :let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
073263
  :$put =string(sort(copy(l), 1))
073263
  :$put =string(sort(copy(l), 'i'))
073263
  :$put =string(sort(copy(l)))
073263
*** ../vim-7.4.350/src/testdir/test55.ok	2014-06-26 22:33:47.850693627 +0200
073263
--- src/testdir/test55.ok	2014-07-02 19:00:57.078321225 +0200
073263
***************
073263
*** 101,110 ****
073263
  [[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0']
073263
  ['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]]
073263
  ['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]]
073263
! [-1, 'foo', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255]
073263
! ['foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
073263
! ['foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
073263
! ['FOOBAR', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
073263
  ['aa', 'bb']
073263
  ['aa', 'bb']
073263
  ['', 'aa', 'bb', '']
073263
--- 101,110 ----
073263
  [[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0']
073263
  ['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]]
073263
  ['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]]
073263
! [-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255]
073263
! ['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
073263
! ['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
073263
! ['BAR', 'Bar', 'FOO', 'FOOBAR', 'Foo', 'bar', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}]
073263
  ['aa', 'bb']
073263
  ['aa', 'bb']
073263
  ['', 'aa', 'bb', '']
073263
*** ../vim-7.4.350/src/version.c	2014-07-02 18:27:44.662290695 +0200
073263
--- src/version.c	2014-07-02 18:46:38.230308065 +0200
073263
***************
073263
*** 736,737 ****
073263
--- 736,739 ----
073263
  {   /* Add new patch number below this line */
073263
+ /**/
073263
+     351,
073263
  /**/
073263
073263
-- 
073263
The early bird gets the worm. If you want something else for
073263
breakfast, get up later.
073263
073263
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
073263
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
073263
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
073263
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///