2008-08-27 03:07:12 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2008 Cedric Bail
|
2012-02-27 05:29:47 -08:00
|
|
|
* Copyright (C) 2011 Alexandre Becoulet
|
2008-08-27 03:07:12 -07:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2008-08-27 03:07:12 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-02-27 05:29:47 -08:00
|
|
|
#include <stdint.h>
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_config.h"
|
2008-08-27 03:07:12 -07:00
|
|
|
#include "eina_private.h"
|
2009-09-06 15:21:56 -07:00
|
|
|
#include "eina_array.h"
|
2012-09-10 20:37:10 -07:00
|
|
|
#include "eina_trash.h"
|
2016-04-22 10:58:25 -07:00
|
|
|
#include "eina_lock.h"
|
2009-09-06 15:21:56 -07:00
|
|
|
|
|
|
|
/* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
|
2008-12-26 10:31:14 -08:00
|
|
|
#include "eina_safety_checks.h"
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_rbtree.h"
|
2008-08-27 03:07:12 -07:00
|
|
|
|
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* Local *
|
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-08-27 03:07:12 -07:00
|
|
|
#define EINA_RBTREE_ITERATOR_PREFIX_MASK 0x1
|
|
|
|
#define EINA_RBTREE_ITERATOR_INFIX_MASK 0x2
|
|
|
|
#define EINA_RBTREE_ITERATOR_POSTFIX_MASK 0x4
|
|
|
|
|
|
|
|
typedef struct _Eina_Iterator_Rbtree Eina_Iterator_Rbtree;
|
|
|
|
typedef struct _Eina_Iterator_Rbtree_List Eina_Iterator_Rbtree_List;
|
|
|
|
|
|
|
|
struct _Eina_Iterator_Rbtree
|
|
|
|
{
|
|
|
|
Eina_Iterator iterator;
|
|
|
|
|
|
|
|
Eina_Array *stack;
|
2012-09-10 20:37:10 -07:00
|
|
|
Eina_Trash *trash;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
|
|
|
unsigned char mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _Eina_Iterator_Rbtree_List
|
|
|
|
{
|
|
|
|
Eina_Rbtree *tree;
|
|
|
|
|
|
|
|
Eina_Rbtree_Direction dir : 1;
|
|
|
|
Eina_Bool up : 1;
|
|
|
|
};
|
|
|
|
|
2016-04-22 10:58:25 -07:00
|
|
|
static Eina_Array iterator_trash;
|
|
|
|
static Eina_Spinlock iterator_trash_lock;
|
|
|
|
|
2008-08-27 03:07:12 -07:00
|
|
|
static Eina_Iterator_Rbtree_List *
|
2012-09-10 20:37:10 -07:00
|
|
|
_eina_rbtree_iterator_list_new(Eina_Iterator_Rbtree *it, const Eina_Rbtree *tree)
|
2008-08-27 03:07:12 -07:00
|
|
|
{
|
|
|
|
Eina_Iterator_Rbtree_List *new;
|
|
|
|
|
2012-09-10 20:37:10 -07:00
|
|
|
new = eina_trash_pop(&it->trash);
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!new)
|
|
|
|
{
|
2012-09-10 20:37:10 -07:00
|
|
|
new = malloc(sizeof (Eina_Iterator_Rbtree_List));
|
2013-10-11 00:49:13 -07:00
|
|
|
if (!new) return NULL;
|
2010-07-27 19:37:05 -07:00
|
|
|
}
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
new->tree = (Eina_Rbtree *)tree;
|
2008-08-27 03:07:12 -07:00
|
|
|
new->dir = EINA_RBTREE_RIGHT;
|
|
|
|
new->up = EINA_FALSE;
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Rbtree *
|
|
|
|
_eina_rbtree_iterator_get_content(Eina_Iterator_Rbtree *it)
|
|
|
|
{
|
2012-01-10 07:34:48 -08:00
|
|
|
if (eina_array_count(it->stack) <= 0)
|
2010-07-27 19:37:05 -07:00
|
|
|
return NULL;
|
|
|
|
|
2008-09-15 12:17:15 -07:00
|
|
|
return eina_array_data_get(it->stack, 0);
|
2008-08-27 03:07:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-04-22 10:58:25 -07:00
|
|
|
_eina_rbtree_iterator_forced_free(Eina_Iterator_Rbtree *it)
|
2008-08-27 03:07:12 -07:00
|
|
|
{
|
|
|
|
Eina_Iterator_Rbtree_List *item;
|
|
|
|
Eina_Array_Iterator et;
|
|
|
|
unsigned int i;
|
2012-09-10 20:37:10 -07:00
|
|
|
void *last;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2010-08-11 07:32:30 -07:00
|
|
|
EINA_ARRAY_ITER_NEXT(it->stack, i, item, et)
|
2010-08-09 20:26:15 -07:00
|
|
|
free(item);
|
2008-08-27 03:07:12 -07:00
|
|
|
|
|
|
|
eina_array_free(it->stack);
|
2012-09-10 20:37:10 -07:00
|
|
|
while ((last = eina_trash_pop(&it->trash)))
|
|
|
|
free(last);
|
|
|
|
free(it);
|
2008-08-27 03:07:12 -07:00
|
|
|
}
|
|
|
|
|
2016-04-22 10:58:25 -07:00
|
|
|
static void
|
|
|
|
_eina_rbtree_iterator_free(Eina_Iterator_Rbtree *it)
|
|
|
|
{
|
|
|
|
if (eina_array_count(&iterator_trash) >= 7)
|
|
|
|
{
|
|
|
|
_eina_rbtree_iterator_forced_free(it);
|
2016-07-18 23:40:16 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (eina_array_count(it->stack) > 0)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
Eina_Array_Iterator et;
|
|
|
|
Eina_Iterator_Rbtree_List *item;
|
|
|
|
|
|
|
|
EINA_ARRAY_ITER_NEXT(it->stack, i, item, et)
|
|
|
|
free(item);
|
2016-04-22 10:58:25 -07:00
|
|
|
}
|
|
|
|
|
2019-12-11 02:13:39 -08:00
|
|
|
eina_array_clean(it->stack);
|
2016-04-22 10:58:25 -07:00
|
|
|
|
|
|
|
eina_spinlock_take(&iterator_trash_lock);
|
|
|
|
eina_array_push(&iterator_trash, it);
|
|
|
|
eina_spinlock_release(&iterator_trash_lock);
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:07:12 -07:00
|
|
|
static Eina_Bool
|
|
|
|
_eina_rbtree_iterator_next(Eina_Iterator_Rbtree *it, void **data)
|
|
|
|
{
|
|
|
|
Eina_Iterator_Rbtree_List *last;
|
|
|
|
Eina_Iterator_Rbtree_List *new;
|
|
|
|
Eina_Rbtree *tree;
|
|
|
|
|
2012-01-10 07:34:48 -08:00
|
|
|
if (eina_array_count(it->stack) <= 0)
|
2010-07-27 19:37:05 -07:00
|
|
|
return EINA_FALSE;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2012-01-10 07:34:48 -08:00
|
|
|
last = eina_array_data_get(it->stack, eina_array_count(it->stack) - 1);
|
2008-08-27 03:07:12 -07:00
|
|
|
tree = last->tree;
|
|
|
|
|
2010-08-21 06:52:25 -07:00
|
|
|
if (!last->tree || last->up == EINA_TRUE)
|
2008-08-27 03:07:12 -07:00
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
last = eina_array_pop(it->stack);
|
|
|
|
while (last->dir == EINA_RBTREE_LEFT
|
2010-08-21 06:52:25 -07:00
|
|
|
|| !last->tree)
|
2010-07-27 19:37:05 -07:00
|
|
|
{
|
|
|
|
if (tree)
|
|
|
|
if ((it->mask & EINA_RBTREE_ITERATOR_POSTFIX_MASK) ==
|
|
|
|
EINA_RBTREE_ITERATOR_POSTFIX_MASK)
|
|
|
|
{
|
2012-09-10 20:37:10 -07:00
|
|
|
eina_trash_push(&it->trash, last);
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2012-01-10 07:34:48 -08:00
|
|
|
if (eina_array_count(it->stack) > 0)
|
2010-07-27 19:37:05 -07:00
|
|
|
{
|
|
|
|
last = eina_array_data_get(it->stack,
|
2012-01-10 07:34:48 -08:00
|
|
|
eina_array_count(
|
2010-07-27 19:37:05 -07:00
|
|
|
it->
|
|
|
|
stack)
|
|
|
|
- 1);
|
|
|
|
last->up = EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto onfix;
|
|
|
|
}
|
|
|
|
|
2012-09-10 20:37:10 -07:00
|
|
|
eina_trash_push(&it->trash, last);
|
2010-07-27 19:37:05 -07:00
|
|
|
|
|
|
|
last = eina_array_pop(it->stack);
|
|
|
|
if (!last)
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
|
|
|
tree = last->tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
last->dir = EINA_RBTREE_LEFT;
|
|
|
|
last->up = EINA_FALSE;
|
|
|
|
|
|
|
|
eina_array_push(it->stack, last);
|
|
|
|
|
|
|
|
if ((it->mask & EINA_RBTREE_ITERATOR_INFIX_MASK) ==
|
|
|
|
EINA_RBTREE_ITERATOR_INFIX_MASK)
|
|
|
|
goto onfix;
|
2008-08-27 03:07:12 -07:00
|
|
|
}
|
|
|
|
|
2012-09-10 20:37:10 -07:00
|
|
|
new = _eina_rbtree_iterator_list_new(it, last->tree->son[last->dir]);
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!new)
|
2012-09-10 20:37:10 -07:00
|
|
|
return EINA_FALSE;
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2012-09-10 20:37:10 -07:00
|
|
|
eina_array_push(it->stack, new);
|
2008-08-27 03:07:12 -07:00
|
|
|
|
|
|
|
if (last->dir == EINA_RBTREE_RIGHT)
|
2010-07-27 19:37:05 -07:00
|
|
|
if ((it->mask & EINA_RBTREE_ITERATOR_PREFIX_MASK) ==
|
|
|
|
EINA_RBTREE_ITERATOR_PREFIX_MASK)
|
|
|
|
goto onfix;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
|
|
|
return _eina_rbtree_iterator_next(it, data);
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
onfix:
|
2009-08-25 14:05:00 -07:00
|
|
|
*data = tree;
|
2008-08-27 03:07:12 -07:00
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Iterator *
|
|
|
|
_eina_rbtree_iterator_build(const Eina_Rbtree *root, unsigned char mask)
|
|
|
|
{
|
|
|
|
Eina_Iterator_Rbtree_List *first;
|
|
|
|
Eina_Iterator_Rbtree *it;
|
|
|
|
|
2016-04-22 10:58:25 -07:00
|
|
|
eina_spinlock_take(&iterator_trash_lock);
|
|
|
|
it = eina_array_pop(&iterator_trash);
|
|
|
|
eina_spinlock_release(&iterator_trash_lock);
|
|
|
|
|
|
|
|
if (!it)
|
|
|
|
{
|
|
|
|
it = calloc(1, sizeof (Eina_Iterator_Rbtree));
|
|
|
|
if (!it) return NULL;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2020-02-14 01:17:25 -08:00
|
|
|
eina_trash_init(&it->trash);
|
2012-09-10 20:37:10 -07:00
|
|
|
|
2020-02-14 01:17:25 -08:00
|
|
|
it->stack = eina_array_new(8);
|
|
|
|
if (!it->stack)
|
|
|
|
{
|
|
|
|
free(it);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-22 10:58:25 -07:00
|
|
|
}
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2012-09-10 20:37:10 -07:00
|
|
|
first = _eina_rbtree_iterator_list_new(it, root);
|
2016-04-22 10:58:25 -07:00
|
|
|
if (!first) goto on_error;
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2008-08-27 03:07:12 -07:00
|
|
|
eina_array_push(it->stack, first);
|
|
|
|
|
|
|
|
it->mask = mask;
|
|
|
|
|
2010-08-19 05:02:28 -07:00
|
|
|
it->iterator.version = EINA_ITERATOR_VERSION;
|
2008-08-27 03:07:12 -07:00
|
|
|
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_rbtree_iterator_next);
|
2010-07-27 19:37:05 -07:00
|
|
|
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
|
|
|
|
_eina_rbtree_iterator_get_content);
|
2008-08-27 03:07:12 -07:00
|
|
|
it->iterator.free = FUNC_ITERATOR_FREE(_eina_rbtree_iterator_free);
|
|
|
|
|
2008-12-05 22:17:11 -08:00
|
|
|
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
|
|
|
|
|
2008-08-27 03:07:12 -07:00
|
|
|
return &it->iterator;
|
|
|
|
|
2010-07-13 19:20:30 -07:00
|
|
|
on_error:
|
2016-04-22 10:58:25 -07:00
|
|
|
_eina_rbtree_iterator_free(it);
|
2008-08-27 03:07:12 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eina_rbtree_node_init(Eina_Rbtree *node)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!node)
|
|
|
|
return;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
|
|
|
node->son[0] = NULL;
|
|
|
|
node->son[1] = NULL;
|
|
|
|
|
|
|
|
node->color = EINA_RBTREE_RED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Rbtree *
|
2010-07-27 19:37:05 -07:00
|
|
|
_eina_rbtree_inline_single_rotation(Eina_Rbtree *node,
|
|
|
|
Eina_Rbtree_Direction dir)
|
2008-08-27 03:07:12 -07:00
|
|
|
{
|
2012-02-27 05:29:47 -08:00
|
|
|
Eina_Rbtree *save = node->son[dir ^ 1];
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
node->son[dir ^ 1] = save->son[dir];
|
2008-08-27 03:07:12 -07:00
|
|
|
save->son[dir] = node;
|
|
|
|
|
|
|
|
node->color = EINA_RBTREE_RED;
|
|
|
|
save->color = EINA_RBTREE_BLACK;
|
|
|
|
|
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Eina_Rbtree *
|
2010-07-27 19:37:05 -07:00
|
|
|
_eina_rbtree_inline_double_rotation(Eina_Rbtree *node,
|
|
|
|
Eina_Rbtree_Direction dir)
|
2008-08-27 03:07:12 -07:00
|
|
|
{
|
2012-02-27 05:29:47 -08:00
|
|
|
node->son[dir ^ 1] = _eina_rbtree_inline_single_rotation(node->son[dir ^ 1], dir ^ 1);
|
2008-08-27 03:07:12 -07:00
|
|
|
return _eina_rbtree_inline_single_rotation(node, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-08-27 03:07:12 -07:00
|
|
|
/*============================================================================*
|
2010-07-27 19:37:05 -07:00
|
|
|
* API *
|
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
eina: Rename EAPI macro to EINA_API in Eina library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
`__attribute__ ((visibility ("default")))` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
`__atttribute__((visibility("default")))`
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: jptiz, lucas, woohyun, vtorri, raster
Reviewed By: jptiz, lucas, vtorri
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12188
2020-11-25 04:35:48 -08:00
|
|
|
EINA_API Eina_Rbtree *
|
2010-07-27 19:37:05 -07:00
|
|
|
eina_rbtree_inline_insert(Eina_Rbtree *root,
|
|
|
|
Eina_Rbtree *node,
|
|
|
|
Eina_Rbtree_Cmp_Node_Cb cmp,
|
|
|
|
const void *data)
|
2008-08-27 03:07:12 -07:00
|
|
|
{
|
2012-02-27 05:29:47 -08:00
|
|
|
Eina_Rbtree **r = &root;
|
|
|
|
Eina_Rbtree *q = root;
|
|
|
|
uintptr_t stack[48];
|
|
|
|
unsigned int s = 0;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(node, root);
|
2010-07-27 19:37:05 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL( cmp, root);
|
2008-12-26 10:31:14 -08:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
/* Find insertion leaf */
|
|
|
|
while (q != NULL)
|
|
|
|
{
|
|
|
|
Eina_Rbtree_Direction dir = cmp(q, node, (void *)data);
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
/* Keep path in stack */
|
|
|
|
stack[s++] = (uintptr_t)r | dir;
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
r = q->son + dir;
|
|
|
|
q = *r;
|
|
|
|
}
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
/* Insert */
|
|
|
|
*r = node;
|
|
|
|
_eina_rbtree_node_init(node);
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
/* Rebalance */
|
|
|
|
while (s > 0)
|
|
|
|
{
|
|
|
|
Eina_Rbtree *a, *b;
|
|
|
|
uintptr_t top = stack[--s]; /* Pop link pointer and direction */
|
|
|
|
Eina_Rbtree_Direction dir = top & 1;
|
|
|
|
|
2012-03-02 07:36:54 -08:00
|
|
|
r = (Eina_Rbtree **)(top & ~(uintptr_t)1);
|
2012-02-27 05:29:47 -08:00
|
|
|
q = *r;
|
|
|
|
|
|
|
|
a = q->son[dir];
|
|
|
|
/* Rebalance done ? */
|
|
|
|
if (a == NULL || a->color == EINA_RBTREE_BLACK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
b = q->son[dir ^ 1];
|
|
|
|
if (b != NULL && b->color == EINA_RBTREE_RED)
|
|
|
|
{
|
|
|
|
q->color = EINA_RBTREE_RED;
|
|
|
|
b->color = a->color = EINA_RBTREE_BLACK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Eina_Rbtree *c = a->son[dir];
|
|
|
|
Eina_Rbtree *d = a->son[dir ^ 1];
|
|
|
|
|
|
|
|
if (c != NULL && c->color == EINA_RBTREE_RED)
|
|
|
|
*r = _eina_rbtree_inline_single_rotation(*r, dir ^ 1);
|
|
|
|
else if (d != NULL && d->color == EINA_RBTREE_RED)
|
|
|
|
*r = _eina_rbtree_inline_double_rotation(*r, dir ^ 1);
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 03:07:12 -07:00
|
|
|
|
|
|
|
root->color = EINA_RBTREE_BLACK;
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
eina: Rename EAPI macro to EINA_API in Eina library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
`__attribute__ ((visibility ("default")))` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
`__atttribute__((visibility("default")))`
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: jptiz, lucas, woohyun, vtorri, raster
Reviewed By: jptiz, lucas, vtorri
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12188
2020-11-25 04:35:48 -08:00
|
|
|
EINA_API Eina_Rbtree *
|
2010-07-27 19:37:05 -07:00
|
|
|
eina_rbtree_inline_remove(Eina_Rbtree *root,
|
|
|
|
Eina_Rbtree *node,
|
|
|
|
Eina_Rbtree_Cmp_Node_Cb cmp,
|
|
|
|
const void *data)
|
2008-08-27 03:07:12 -07:00
|
|
|
{
|
2012-02-27 05:29:47 -08:00
|
|
|
Eina_Rbtree *l0, *l1, *r, **rt = &root;
|
2008-08-27 03:07:12 -07:00
|
|
|
Eina_Rbtree_Direction dir;
|
2012-02-27 05:29:47 -08:00
|
|
|
uintptr_t stack[48];
|
|
|
|
unsigned int s = 0;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(node, root);
|
2010-07-27 19:37:05 -07:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL( cmp, root);
|
2008-12-26 10:31:14 -08:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
/* Item search loop */
|
|
|
|
for (r = *rt; r != NULL; r = *rt)
|
|
|
|
{
|
|
|
|
if (r == node)
|
|
|
|
goto found;
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
dir = cmp(r, node, (void*)data);
|
|
|
|
stack[s++] = (uintptr_t)rt | dir;
|
|
|
|
rt = r->son + dir;
|
|
|
|
}
|
|
|
|
return root;
|
2008-08-27 03:07:12 -07:00
|
|
|
|
2012-02-27 05:29:47 -08:00
|
|
|
found:
|
|
|
|
/* remove entry */
|
|
|
|
l0 = node->son[0];
|
|
|
|
l1 = node->son[1];
|
|
|
|
|
|
|
|
if (l0 != NULL && l1 != NULL) /* two links case */
|
|
|
|
{
|
|
|
|
Eina_Rbtree *q, **t, **p;
|
|
|
|
uintptr_t ss;
|
|
|
|
|
|
|
|
stack[s++] = (uintptr_t)rt | 1;
|
|
|
|
ss = s; /* keep predecessor right link stack index */
|
|
|
|
|
|
|
|
/* find predecessor */
|
|
|
|
p = node->son + 1;
|
|
|
|
q = *p;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
t = q->son;
|
|
|
|
q = *t;
|
|
|
|
if (q == NULL)
|
|
|
|
break;
|
|
|
|
stack[s++] = (uintptr_t)p | 0;
|
|
|
|
p = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* detach predecessor */
|
|
|
|
q = *p;
|
|
|
|
*p = q->son[1];
|
|
|
|
|
|
|
|
int c = q->color;
|
|
|
|
|
|
|
|
/* replace entry by predecessor */
|
|
|
|
memcpy(q, node, sizeof(Eina_Rbtree));
|
|
|
|
*rt = q;
|
|
|
|
|
|
|
|
if (c == EINA_RBTREE_RED)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* fix stack for replaced entry */
|
|
|
|
if (s > ss)
|
|
|
|
stack[ss] = (uintptr_t)(q->son + 1) | 0;
|
|
|
|
}
|
|
|
|
else /* single link case */
|
|
|
|
{
|
|
|
|
if (l0 == NULL)
|
|
|
|
l0 = l1;
|
|
|
|
|
|
|
|
*rt = l0;
|
|
|
|
|
|
|
|
if (node->color == EINA_RBTREE_RED)
|
|
|
|
goto end; /* removed red */
|
|
|
|
|
|
|
|
if (l0 != NULL && l0->color == EINA_RBTREE_RED)
|
|
|
|
{
|
|
|
|
/* red child replace removed black */
|
|
|
|
l0->color = EINA_RBTREE_BLACK;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rebalance */
|
|
|
|
while (s > 0)
|
|
|
|
{
|
|
|
|
Eina_Rbtree *q;
|
|
|
|
uintptr_t st = stack[--s];
|
|
|
|
|
2012-03-02 07:36:54 -08:00
|
|
|
rt = (Eina_Rbtree**)(st & ~(uintptr_t)1);
|
2012-02-27 05:29:47 -08:00
|
|
|
dir = st & 1;
|
|
|
|
r = *rt;
|
|
|
|
q = r->son[dir ^ 1];
|
|
|
|
|
|
|
|
if (q != NULL && q->color == EINA_RBTREE_RED)
|
|
|
|
{
|
|
|
|
*rt = _eina_rbtree_inline_single_rotation(*rt, dir);
|
|
|
|
q = r->son[dir ^ 1];
|
|
|
|
rt = (*rt)->son + dir;
|
|
|
|
}
|
|
|
|
|
2020-06-20 02:48:52 -07:00
|
|
|
if (q != NULL)
|
2012-02-27 05:29:47 -08:00
|
|
|
{
|
|
|
|
int r_color = r->color;
|
|
|
|
Eina_Rbtree *nd = q->son[dir ^ 1];
|
|
|
|
|
|
|
|
if (nd != NULL && nd->color == EINA_RBTREE_RED)
|
|
|
|
{
|
|
|
|
*rt = _eina_rbtree_inline_single_rotation(*rt, dir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Eina_Rbtree *d = q->son[dir];
|
|
|
|
|
|
|
|
if (d != NULL && d->color == EINA_RBTREE_RED)
|
|
|
|
{
|
|
|
|
*rt = _eina_rbtree_inline_double_rotation(*rt, dir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
r->color = EINA_RBTREE_BLACK;
|
|
|
|
q->color = EINA_RBTREE_RED;
|
|
|
|
if (r_color == EINA_RBTREE_RED)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = *rt;
|
|
|
|
r->color = r_color;
|
|
|
|
r->son[1]->color = r->son[0]->color = EINA_RBTREE_BLACK;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (root != NULL)
|
2010-07-27 19:37:05 -07:00
|
|
|
root->color = EINA_RBTREE_BLACK;
|
2008-08-27 03:07:12 -07:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
eina: Rename EAPI macro to EINA_API in Eina library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
`__attribute__ ((visibility ("default")))` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
`__atttribute__((visibility("default")))`
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: jptiz, lucas, woohyun, vtorri, raster
Reviewed By: jptiz, lucas, vtorri
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12188
2020-11-25 04:35:48 -08:00
|
|
|
EINA_API Eina_Iterator *
|
2008-08-27 03:07:12 -07:00
|
|
|
eina_rbtree_iterator_prefix(const Eina_Rbtree *root)
|
|
|
|
{
|
|
|
|
return _eina_rbtree_iterator_build(root, EINA_RBTREE_ITERATOR_PREFIX_MASK);
|
|
|
|
}
|
|
|
|
|
eina: Rename EAPI macro to EINA_API in Eina library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
`__attribute__ ((visibility ("default")))` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
`__atttribute__((visibility("default")))`
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: jptiz, lucas, woohyun, vtorri, raster
Reviewed By: jptiz, lucas, vtorri
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12188
2020-11-25 04:35:48 -08:00
|
|
|
EINA_API Eina_Iterator *
|
2008-08-27 03:07:12 -07:00
|
|
|
eina_rbtree_iterator_infix(const Eina_Rbtree *root)
|
|
|
|
{
|
|
|
|
return _eina_rbtree_iterator_build(root, EINA_RBTREE_ITERATOR_INFIX_MASK);
|
|
|
|
}
|
|
|
|
|
eina: Rename EAPI macro to EINA_API in Eina library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
`__attribute__ ((visibility ("default")))` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
`__atttribute__((visibility("default")))`
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: jptiz, lucas, woohyun, vtorri, raster
Reviewed By: jptiz, lucas, vtorri
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12188
2020-11-25 04:35:48 -08:00
|
|
|
EINA_API Eina_Iterator *
|
2008-08-27 03:07:12 -07:00
|
|
|
eina_rbtree_iterator_postfix(const Eina_Rbtree *root)
|
|
|
|
{
|
|
|
|
return _eina_rbtree_iterator_build(root, EINA_RBTREE_ITERATOR_POSTFIX_MASK);
|
|
|
|
}
|
|
|
|
|
eina: Rename EAPI macro to EINA_API in Eina library
Summary:
Patch from a series of patches to rename EAPI symbols to specific
library DSOs.
EAPI was designed to be able to pass
`__attribute__ ((visibility ("default")))` for symbols with
GCC, which would mean that even if -fvisibility=hidden was used
when compiling the library, the needed symbols would get exported.
MSVC __almost__ works like GCC (or mingw) in which you can
declare everything as export and it will just work (slower, but
it will work). But there's a caveat: global variables will not
work the same way for MSVC, but works for mingw and GCC.
For global variables (as opposed to functions), MSVC requires
correct DSO visibility for MSVC: instead of declaring a symbol as
export for everything, you need to declare it as import when
importing from another DSO and export when defining it locally.
With current EAPI definitions, we get the following example
working in mingw and MSVC (observe it doesn't define any global
variables as exported symbols).
Example 1:
dll1:
```
EAPI void foo(void);
EAPI void bar()
{
foo();
}
```
dll2:
```
EAPI void foo()
{
printf ("foo\n");
}
```
This works fine with API defined as __declspec(dllexport) in both
cases and for gcc defining as
`__atttribute__((visibility("default")))`
However, the following:
Example 2:
dll1:
```
EAPI extern int foo;
EAPI void foobar(void);
EAPI void bar()
{
foo = 5;
foobar();
}
```
dll2:
```
EAPI int foo = 0;
EAPI void foobar()
{
printf ("foo %d\n", foo);
}
```
This will work on mingw but will not work for MSVC. And that's why
EAPI is the only solution that worked for MSVC.
Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com>
Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev>
Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
Reviewers: jptiz, lucas, woohyun, vtorri, raster
Reviewed By: jptiz, lucas, vtorri
Subscribers: ProhtMeyhet, cedric, #reviewers, #committers
Tags: #efl
Differential Revision: https://phab.enlightenment.org/D12188
2020-11-25 04:35:48 -08:00
|
|
|
EINA_API void
|
2008-09-24 10:14:29 -07:00
|
|
|
eina_rbtree_delete(Eina_Rbtree *root, Eina_Rbtree_Free_Cb func, void *data)
|
2008-09-23 09:07:48 -07:00
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!root)
|
|
|
|
return;
|
2008-09-23 09:07:48 -07:00
|
|
|
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(func);
|
|
|
|
|
2008-09-24 10:14:29 -07:00
|
|
|
eina_rbtree_delete(root->son[0], func, data);
|
|
|
|
eina_rbtree_delete(root->son[1], func, data);
|
|
|
|
func(root, data);
|
2008-09-23 09:07:48 -07:00
|
|
|
}
|
2016-04-22 10:58:25 -07:00
|
|
|
|
|
|
|
Eina_Bool
|
|
|
|
eina_rbtree_init(void)
|
|
|
|
{
|
|
|
|
eina_array_step_set(&iterator_trash, sizeof(iterator_trash), 8);
|
|
|
|
return eina_spinlock_new(&iterator_trash_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
Eina_Bool
|
|
|
|
eina_rbtree_shutdown(void)
|
|
|
|
{
|
|
|
|
Eina_Iterator_Rbtree *it;
|
|
|
|
|
|
|
|
while ((it = eina_array_pop(&iterator_trash)))
|
|
|
|
_eina_rbtree_iterator_forced_free(it);
|
|
|
|
eina_array_flush(&iterator_trash);
|
|
|
|
eina_spinlock_free(&iterator_trash_lock);
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|