Advertisement
Guest User

avl.h

a guest
Aug 2nd, 2019
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. // avl.h
  2. #pragma once
  3.  
  4. enum { avl_none=-1 };
  5.  
  6. typedef struct avl_s {
  7.     // internals
  8.     int *LR;    // cells links storage
  9.     void *items;// cells data storage
  10.     int root;   // root element index or avl_null
  11.     int count;  // count items in tree. cells_available=limit-count
  12.     int size;   // size cell used.      erased_cells_count=size-count
  13.     int tail;   // last erased cell or avl_null
  14.     int limit;  // limit total cells allocated for tree
  15.     // input parameters item_size,cmp,copy,ovf required
  16.     int  item_size; // item size in bytes                      
  17.     int  (*cmp) (void *ctx,void *lv,void *rv);   void *cmp_ctx;
  18.     void (*copy)(void *ctx,void *dst,void *src); void *copy_ctx;
  19.     void (*ovf) (void *ctx,struct avl_s *avl);   void *ovf_ctx;
  20.     // ovf can be 0 called to handle limit overflow
  21. } avl_t;
  22.  
  23. int  avl_init(avl_t *avl,int limit);      // allocate LR and items, require item_size
  24. void avl_done(avl_t *avl);                // free LR and items
  25. int  avl_find(avl_t *avl,void *item);     // check is item exist in tree
  26. int  avl_insert(avl_t *avl,void *item);   // insert item if not exists
  27. int  avl_remove(avl_t *avl,void *item);   // remove item from tree
  28. int  avl_get(avl_t *avl,int n,void *item);// get item value for n position, return position or avl_null (item can be null)
  29. int  avl_min(avl_t *avl,void *item);      // find min item, return position or avl_null (item can be null)
  30. int  avl_max(avl_t *avl,void *item);      // find min item, return position or avl_null (item can be null)
  31. int  avl_next(avl_t *avl,void *item);     // find item greater than input item, and store result in item
  32. int  avl_prev(avl_t *avl,void *item);     // find item less than input item, and store result in item
  33. int  avl_clear(avl_t *avl);               // clear tree
  34. int  avl_available(avl_t *avl);           // return cells available
  35. int  avl_count(avl_t *avl);               // return items count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement