gtk2/testsuite/gdk/array.c
Benjamin Otte 8bf8ac5076 Add GdkArray
This is a scary idea where you #define a bunch of preprocessor values
and then #include "gdkarrayimpl.c" and end up with a dynamic array for
that data type.

See https://en.wikipedia.org/wiki/X_Macro for what's going on.

What are the advantages over using GArray or GPtrArray?

 * It's typesafe
   Because it works like C++ templates, we can use the actual type of
   the object instead of having to use gpointer.

 * It's one less indirection
   instead of 2 indirections via self->array->data, this array is
   embedded, so self->array is the actual data, and just one indirection
   away. This is pretty irrelevant in general, but can be very noticable
   in tight loops.

 * It's all inline
   Because the whole API is defined as static inline functions, the
   compiler has full access to everything and can (and does) optimize
   out unnecessary calls, thereby speeding up some operations quite
   significantly, when full optimizations are enabled.

 * It has more features
   In particular preallocation allows for avoiding malloc() calls, which
   can again speed up tight loops a lot.
   But there's also splice(), which is very useful when used with
   listmodels.
2020-07-16 18:09:57 +02:00

70 lines
2.2 KiB
C

/*
* Copyright © 2020 Benjamin Otte
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include <locale.h>
#include <gtk/gtk.h>
static void
int_free_func (int data)
{
}
#define GDK_ARRAY_ELEMENT_TYPE int
#define GDK_ARRAY_NAME int_array
#define GDK_ARRAY_TYPE_NAME IntVector
#include "arrayimpl.c"
#define GDK_ARRAY_ELEMENT_TYPE int
#define GDK_ARRAY_NAME pre_int_array
#define GDK_ARRAY_TYPE_NAME PreIntVector
#define GDK_ARRAY_PREALLOC 100
#include "arrayimpl.c"
#define GDK_ARRAY_ELEMENT_TYPE int
#define GDK_ARRAY_NAME free_int_array
#define GDK_ARRAY_TYPE_NAME FreeIntVector
#define GDK_ARRAY_FREE_FUNC int_free_func
#include "arrayimpl.c"
#define GDK_ARRAY_ELEMENT_TYPE int
#define GDK_ARRAY_NAME pre_free_int_array
#define GDK_ARRAY_TYPE_NAME PreFreeIntVector
#define GDK_ARRAY_PREALLOC 100
#define GDK_ARRAY_FREE_FUNC int_free_func
#include "arrayimpl.c"
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
setlocale (LC_ALL, "C");
g_test_add_func ("/intarray/simple", int_array_test_simple);
g_test_add_func ("/intarray/prealloc/simple", pre_int_array_test_simple);
g_test_add_func ("/intarray/freefunc/simple", free_int_array_test_simple);
g_test_add_func ("/intarray/prealloc_freefunc_simple", pre_free_int_array_test_simple);
g_test_add_func ("/intarray/splice", int_array_test_splice);
g_test_add_func ("/intarray/prealloc/splice", pre_int_array_test_splice);
g_test_add_func ("/intarray/freefunc/splice", free_int_array_test_splice);
g_test_add_func ("/intarray/prealloc_freefunc_splice", pre_free_int_array_test_splice);
return g_test_run ();
}