MCPcopy Create free account
hub / github.com/GJDuck/e9patch / pool_tsearch

Function pool_tsearch

examples/stdlib.c:5408–5445  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5406}
5407
5408static void *pool_tsearch(struct malloc_pool_s *pool, const void *key,
5409 void **root, int (*compare)(const void *, const void *))
5410{
5411 if (root == NULL)
5412 return NULL;
5413 struct tree_s *t = (struct tree_s *)root;
5414 struct node_s *n = t->root, *parent = NULL;
5415 int cmp = 0;
5416 while (n != NULL)
5417 {
5418 parent = n;
5419 cmp = compare(key, n->key);
5420 if (cmp < 0)
5421 n = TREE_LEFT(n);
5422 else if (cmp > 0)
5423 n = TREE_RIGHT(n);
5424 else
5425 return (void *)n;
5426 }
5427 n = (struct node_s *)pool_malloc(pool, sizeof(struct node_s));
5428 if (n == NULL)
5429 return NULL;
5430 n->key = (void *)key;
5431 TREE_PARENT(n) = parent;
5432 TREE_LEFT(n) = TREE_RIGHT(n) = NULL;
5433 TREE_COLOR(n) = TREE_RED;
5434 if (parent != NULL)
5435 {
5436 if (cmp < 0)
5437 TREE_LEFT(parent) = n;
5438 else
5439 TREE_RIGHT(parent) = n;
5440 }
5441 else
5442 t->root = n;
5443 tree_rebalance_insert(t, n);
5444 return n;
5445}
5446static void *tsearch(const void *key, void **root,
5447 int (*compare)(const void *, const void *))
5448{

Callers 1

tsearchFunction · 0.85

Calls 3

pool_mallocFunction · 0.85
tree_rebalance_insertFunction · 0.85
compareFunction · 0.70

Tested by

no test coverage detected