patch 8.2.4627: flatten() does not use maxdepth correctly
Problem: flatten() does not use maxdepth correctly. Solution: Use a recursive implementation. (closes #10020)
This commit is contained in:
43
src/list.c
43
src/list.c
@ -916,35 +916,40 @@ list_assign_range(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Flatten "list" to depth "maxdepth".
|
* Flatten up to "maxitems" in "list", starting at "first" to depth "maxdepth".
|
||||||
|
* When "first" is NULL use the first item.
|
||||||
* It does nothing if "maxdepth" is 0.
|
* It does nothing if "maxdepth" is 0.
|
||||||
* Returns FAIL when out of memory.
|
* Returns FAIL when out of memory.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
list_flatten(list_T *list, long maxdepth)
|
list_flatten(list_T *list, listitem_T *first, long maxitems, long maxdepth)
|
||||||
{
|
{
|
||||||
listitem_T *item;
|
listitem_T *item;
|
||||||
listitem_T *tofree;
|
listitem_T *tofree;
|
||||||
int n;
|
int done = 0;
|
||||||
|
|
||||||
if (maxdepth == 0)
|
if (maxdepth == 0)
|
||||||
return;
|
return;
|
||||||
CHECK_LIST_MATERIALIZE(list);
|
CHECK_LIST_MATERIALIZE(list);
|
||||||
|
if (first == NULL)
|
||||||
n = 0;
|
|
||||||
item = list->lv_first;
|
item = list->lv_first;
|
||||||
while (item != NULL)
|
else
|
||||||
|
item = first;
|
||||||
|
|
||||||
|
while (item != NULL && done < maxitems)
|
||||||
{
|
{
|
||||||
|
listitem_T *next = item->li_next;
|
||||||
|
|
||||||
fast_breakcheck();
|
fast_breakcheck();
|
||||||
if (got_int)
|
if (got_int)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (item->li_tv.v_type == VAR_LIST)
|
if (item->li_tv.v_type == VAR_LIST)
|
||||||
{
|
{
|
||||||
listitem_T *next = item->li_next;
|
list_T *itemlist = item->li_tv.vval.v_list;
|
||||||
|
|
||||||
vimlist_remove(list, item, item);
|
vimlist_remove(list, item, item);
|
||||||
if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
|
if (list_extend(list, itemlist, next) == FAIL)
|
||||||
{
|
{
|
||||||
list_free_item(list, item);
|
list_free_item(list, item);
|
||||||
return;
|
return;
|
||||||
@ -952,24 +957,16 @@ list_flatten(list_T *list, long maxdepth)
|
|||||||
clear_tv(&item->li_tv);
|
clear_tv(&item->li_tv);
|
||||||
tofree = item;
|
tofree = item;
|
||||||
|
|
||||||
if (item->li_prev == NULL)
|
if (maxdepth > 0)
|
||||||
item = list->lv_first;
|
list_flatten(list, item->li_prev == NULL
|
||||||
else
|
? list->lv_first : item->li_prev->li_next,
|
||||||
item = item->li_prev->li_next;
|
itemlist->lv_len, maxdepth - 1);
|
||||||
list_free_item(list, tofree);
|
list_free_item(list, tofree);
|
||||||
|
}
|
||||||
|
|
||||||
if (++n >= maxdepth)
|
++done;
|
||||||
{
|
|
||||||
n = 0;
|
|
||||||
item = next;
|
item = next;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
n = 0;
|
|
||||||
item = item->li_next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1031,7 +1028,7 @@ flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
|
|||||||
++l->lv_refcount;
|
++l->lv_refcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
list_flatten(l, maxdepth);
|
list_flatten(l, NULL, l->lv_len, maxdepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -79,6 +79,14 @@ func Test_flatten()
|
|||||||
call add(y, x) " l:y = [2, [1, [...]]]
|
call add(y, x) " l:y = [2, [1, [...]]]
|
||||||
call assert_equal([1, 2, 1, 2], flatten(l:x, 2))
|
call assert_equal([1, 2, 1, 2], flatten(l:x, 2))
|
||||||
call assert_equal([2, l:x], l:y)
|
call assert_equal([2, l:x], l:y)
|
||||||
|
|
||||||
|
let l4 = [ 1, [ 11, [ 101, [ 1001 ] ] ] ]
|
||||||
|
call assert_equal(l4, flatten(deepcopy(l4), 0))
|
||||||
|
call assert_equal([1, 11, [101, [1001]]], flatten(deepcopy(l4), 1))
|
||||||
|
call assert_equal([1, 11, 101, [1001]], flatten(deepcopy(l4), 2))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4), 3))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4), 4))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flatten(deepcopy(l4)))
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
func Test_flattennew()
|
func Test_flattennew()
|
||||||
@ -88,6 +96,14 @@ func Test_flattennew()
|
|||||||
|
|
||||||
call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
|
call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1))
|
||||||
call assert_equal([1, [2, [3, 4]], 5], l)
|
call assert_equal([1, [2, [3, 4]], 5], l)
|
||||||
|
|
||||||
|
let l4 = [ 1, [ 11, [ 101, [ 1001 ] ] ] ]
|
||||||
|
call assert_equal(l4, flatten(deepcopy(l4), 0))
|
||||||
|
call assert_equal([1, 11, [101, [1001]]], flattennew(l4, 1))
|
||||||
|
call assert_equal([1, 11, 101, [1001]], flattennew(l4, 2))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flattennew(l4, 3))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flattennew(l4, 4))
|
||||||
|
call assert_equal([1, 11, 101, 1001], flattennew(l4))
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
" vim: shiftwidth=2 sts=2 expandtab
|
" vim: shiftwidth=2 sts=2 expandtab
|
||||||
|
|||||||
@ -750,6 +750,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
4627,
|
||||||
/**/
|
/**/
|
||||||
4626,
|
4626,
|
||||||
/**/
|
/**/
|
||||||
|
|||||||
Reference in New Issue
Block a user