diff options
author | Cédric Barreteau <> | 2020-06-30 18:39:54 +0200 |
---|---|---|
committer | Cédric Barreteau <> | 2020-07-15 20:27:20 +0200 |
commit | 6420615e3f703870ed898083f84d6e3515a8c279 (patch) | |
tree | 4ccc6606a2fc3b900f1c6a40e28a778e73762695 /src/nvim/eval/typval.c | |
parent | a02a267f8ad4b6d8b9038d2c7d9b85f03e734814 (diff) | |
download | rneovim-6420615e3f703870ed898083f84d6e3515a8c279.tar.gz rneovim-6420615e3f703870ed898083f84d6e3515a8c279.tar.bz2 rneovim-6420615e3f703870ed898083f84d6e3515a8c279.zip |
vim-patch:8.2.0935: flattening a list with existing code is slow
Problem: Flattening a list with existing code is slow.
Solution: Add flatten(). (Mopp, closes vim/vim#3676)
https://github.com/vim/vim/commit/077a1e670ad69ef4cefc22103ca6635bd269e764
Diffstat (limited to 'src/nvim/eval/typval.c')
-rw-r--r-- | src/nvim/eval/typval.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 89ca2db59b..576948f052 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -640,6 +640,53 @@ tv_list_copy_error: return NULL; } +/// Flatten "list" in place to depth "maxdepth". +/// Does nothing if "maxdepth" is 0. +/// +/// @param[in] list List to flatten +/// @param[in] maxdepth Maximum depth that will be flattened +/// +/// @return OK or FAIL +int tv_list_flatten(list_T *list, long maxdepth) + FUNC_ATTR_WARN_UNUSED_RESULT +{ + listitem_T *item; + int n; + if (maxdepth == 0) { + return OK; + } + + n = 0; + item = list->lv_first; + while (item != NULL) { + fast_breakcheck(); + if (got_int) { + return FAIL; + } + if (item->li_tv.v_type == VAR_LIST) { + listitem_T *next = item->li_next; + + tv_list_drop_items(list, item, item); + tv_list_extend(list, item->li_tv.vval.v_list, next); + + if (item->li_prev == NULL) { + item = list->lv_first; + } else { + item = item->li_prev->li_next; + } + + if (++n >= maxdepth) { + n = 0; + item = next; + } + } else { + n = 0; + item = item->li_next; + } + } + return OK; +} + /// Extend first list with the second /// /// @param[out] l1 List to extend. |