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