From 8cfb272c74b760c5c6224bc7b73358d982e117ab Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 1 May 2016 04:53:15 +0300 Subject: lib/kvec: Remove useless type argument from kv_push macros --- src/nvim/lib/kvec.h | 84 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 34 deletions(-) (limited to 'src/nvim/lib') diff --git a/src/nvim/lib/kvec.h b/src/nvim/lib/kvec.h index b41ef0cc9f..92a1232354 100644 --- a/src/nvim/lib/kvec.h +++ b/src/nvim/lib/kvec.h @@ -28,20 +28,20 @@ #include "kvec.h" int main() { - kvec_t(int) array; - kv_init(array); - kv_push(int, array, 10); // append - kv_a(int, array, 20) = 5; // dynamic - kv_A(array, 20) = 4; // static - kv_destroy(array); - return 0; + kvec_t(int) array; + kv_init(array); + kv_push(array, 10); // append + kv_a(array, 20) = 5; // dynamic + kv_A(array, 20) = 4; // static + kv_destroy(array); + return 0; } */ /* 2008-09-22 (0.1.0): - * The initial version. + * The initial version. */ @@ -62,31 +62,47 @@ int main() { #define kv_max(v) ((v).capacity) #define kv_last(v) kv_A(v, kv_size(v) - 1) -#define kv_resize(type, v, s) ((v).capacity = (s), (v).items = (type*)xrealloc((v).items, sizeof(type) * (v).capacity)) - -#define kv_copy(type, v1, v0) do { \ - if ((v1).capacity < (v0).size) kv_resize(type, v1, (v0).size); \ - (v1).size = (v0).size; \ - memcpy((v1).items, (v0).items, sizeof(type) * (v0).size); \ - } while (0) \ - -#define kv_push(type, v, x) do { \ - if ((v).size == (v).capacity) { \ - (v).capacity = (v).capacity? (v).capacity<<1 : 8; \ - (v).items = (type*)xrealloc((v).items, sizeof(type) * (v).capacity); \ - } \ - (v).items[(v).size++] = (x); \ - } while (0) - -#define kv_pushp(type, v) ((((v).size == (v).capacity)? \ - ((v).capacity = ((v).capacity? (v).capacity<<1 : 8), \ - (v).items = (type*)xrealloc((v).items, sizeof(type) * (v).capacity), 0) \ - : 0), ((v).items + ((v).size++))) - -#define kv_a(type, v, i) (((v).capacity <= (size_t)(i)? \ - ((v).capacity = (v).size = (i) + 1, kv_roundup32((v).capacity), \ - (v).items = (type*)xrealloc((v).items, sizeof(type) * (v).capacity), 0) \ - : (v).size <= (size_t)(i)? (v).size = (i) + 1 \ - : 0), (v).items[(i)]) +#define kv_resize(v, s) \ + do { \ + (v).capacity = (s); \ + (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity); \ + } while (0) + +#define kv_copy(v1, v0) \ + do { \ + if ((v1).capacity < (v0).size) { \ + kv_resize(v1, (v0).size); \ + } \ + (v1).size = (v0).size; \ + memcpy((v1).items, (v0).items, sizeof((v1).items[0]) * (v0).size); \ + } while (0) \ + +#define kv_push(v, x) \ + do { \ + if ((v).size == (v).capacity) { \ + (v).capacity = (v).capacity ? (v).capacity << 1 : 8; \ + (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity); \ + } \ + (v).items[(v).size++] = (x); \ + } while (0) + +#define kv_pushp(v) \ + ((((v).size == (v).capacity) \ + ? ((v).capacity = ((v).capacity ? (v).capacity << 1 : 8), \ + (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity), \ + 0) \ + : 0), \ + ((v).items + ((v).size++))) + +#define kv_a(v, i) \ + (((v).capacity <= (size_t) (i) \ + ? ((v).capacity = (v).size = (i) + 1, \ + kv_roundup32((v).capacity), \ + (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity), \ + 0) \ + : ((v).size <= (size_t)(i) \ + ? (v).size = (i) + 1 \ + : 0)), \ + (v).items[(i)]) #endif -- cgit From d007c2977bada0db7fa1da6748ba33a7cd33bb86 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 1 May 2016 05:10:27 +0300 Subject: lib/kvec: Do not use kv_init unless needed --- src/nvim/lib/kvec.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/nvim/lib') diff --git a/src/nvim/lib/kvec.h b/src/nvim/lib/kvec.h index 92a1232354..b86548e473 100644 --- a/src/nvim/lib/kvec.h +++ b/src/nvim/lib/kvec.h @@ -28,8 +28,7 @@ #include "kvec.h" int main() { - kvec_t(int) array; - kv_init(array); + kvec_t(int) array = KV_INITIAL_VALUE; kv_push(array, 10); // append kv_a(array, 20) = 5; // dynamic kv_A(array, 20) = 4; // static @@ -53,7 +52,14 @@ int main() { #define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) -#define kvec_t(type) struct { size_t size, capacity; type *items; } +#define KV_INITIAL_VALUE { .size = 0, .capacity = 0, .items = NULL } + +#define kvec_t(type) \ + struct { \ + size_t size; \ + size_t capacity; \ + type *items; \ + } #define kv_init(v) ((v).size = (v).capacity = 0, (v).items = 0) #define kv_destroy(v) xfree((v).items) #define kv_A(v, i) ((v).items[(i)]) -- cgit From 1ce1b85cc6a6736f793135290ee8ce924f486b64 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 1 May 2016 05:19:44 +0300 Subject: lib/kvec: Make code cleaner --- src/nvim/lib/kvec.h | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) (limited to 'src/nvim/lib') diff --git a/src/nvim/lib/kvec.h b/src/nvim/lib/kvec.h index b86548e473..ddfd9ca987 100644 --- a/src/nvim/lib/kvec.h +++ b/src/nvim/lib/kvec.h @@ -48,6 +48,7 @@ int main() { #define AC_KVEC_H #include + #include "nvim/memory.h" #define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) @@ -60,6 +61,7 @@ int main() { size_t capacity; \ type *items; \ } + #define kv_init(v) ((v).size = (v).capacity = 0, (v).items = 0) #define kv_destroy(v) xfree((v).items) #define kv_A(v, i) ((v).items[(i)]) @@ -69,10 +71,11 @@ int main() { #define kv_last(v) kv_A(v, kv_size(v) - 1) #define kv_resize(v, s) \ - do { \ - (v).capacity = (s); \ - (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity); \ - } while (0) + ((v).capacity = (s), \ + (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity)) + +#define kv_resize_full(v) \ + kv_resize(v, (v).capacity ? (v).capacity << 1 : 8) #define kv_copy(v1, v0) \ do { \ @@ -83,30 +86,19 @@ int main() { memcpy((v1).items, (v0).items, sizeof((v1).items[0]) * (v0).size); \ } while (0) \ -#define kv_push(v, x) \ - do { \ - if ((v).size == (v).capacity) { \ - (v).capacity = (v).capacity ? (v).capacity << 1 : 8; \ - (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity); \ - } \ - (v).items[(v).size++] = (x); \ - } while (0) - #define kv_pushp(v) \ - ((((v).size == (v).capacity) \ - ? ((v).capacity = ((v).capacity ? (v).capacity << 1 : 8), \ - (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity), \ - 0) \ - : 0), \ + ((((v).size == (v).capacity) ? (kv_resize_full(v), 0) : 0), \ ((v).items + ((v).size++))) +#define kv_push(v, x) \ + (*kv_pushp(v) = (x)) + #define kv_a(v, i) \ (((v).capacity <= (size_t) (i) \ ? ((v).capacity = (v).size = (i) + 1, \ kv_roundup32((v).capacity), \ - (v).items = xrealloc((v).items, sizeof((v).items[0]) * (v).capacity), \ - 0) \ - : ((v).size <= (size_t)(i) \ + kv_resize((v), (v).capacity), 0) \ + : ((v).size <= (size_t) (i) \ ? (v).size = (i) + 1 \ : 0)), \ (v).items[(i)]) -- cgit From 7a44f9e343c79a1dedf535b9146b308122085300 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 30 May 2016 21:26:30 +0300 Subject: lib/kvec: Fix remaining linter errors --- src/nvim/lib/kvec.h | 93 +++++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 50 deletions(-) (limited to 'src/nvim/lib') diff --git a/src/nvim/lib/kvec.h b/src/nvim/lib/kvec.h index ddfd9ca987..dcc69f7b26 100644 --- a/src/nvim/lib/kvec.h +++ b/src/nvim/lib/kvec.h @@ -1,57 +1,50 @@ -/* The MIT License - - Copyright (c) 2008, by Attractive Chaos - - Permission is hereby granted, free of charge, to any person obtaining - a copy of this software and associated documentation files (the - "Software"), to deal in the Software without restriction, including - without limitation the rights to use, copy, modify, merge, publish, - distribute, sublicense, and/or sell copies of the Software, and to - permit persons to whom the Software is furnished to do so, subject to - the following conditions: - - The above copyright notice and this permission notice shall be - included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS - BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ - -/* - An example: - -#include "kvec.h" -int main() { - kvec_t(int) array = KV_INITIAL_VALUE; - kv_push(array, 10); // append - kv_a(array, 20) = 5; // dynamic - kv_A(array, 20) = 4; // static - kv_destroy(array); - return 0; -} -*/ - -/* - 2008-09-22 (0.1.0): - - * The initial version. - -*/ - -#ifndef AC_KVEC_H -#define AC_KVEC_H +// The MIT License +// +// Copyright (c) 2008, by Attractive Chaos +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +// An example: +// +// #include "kvec.h" +// int main() { +// kvec_t(int) array = KV_INITIAL_VALUE; +// kv_push(array, 10); // append +// kv_a(array, 20) = 5; // dynamic +// kv_A(array, 20) = 4; // static +// kv_destroy(array); +// return 0; +// } + +#ifndef NVIM_LIB_KVEC_H +#define NVIM_LIB_KVEC_H #include #include "nvim/memory.h" -#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) +#define kv_roundup32(x) \ + ((--(x)), \ + ((x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16), \ + (++(x))) #define KV_INITIAL_VALUE { .size = 0, .capacity = 0, .items = NULL } @@ -103,4 +96,4 @@ int main() { : 0)), \ (v).items[(i)]) -#endif +#endif // NVIM_LIB_KVEC_H -- cgit