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