]> git.kaiwu.me - klib.git/commitdiff
added kavll_size()
authorAttractive Chaos <attractor@live.co.uk>
Fri, 26 Mar 2021 02:58:27 +0000 (22:58 -0400)
committerAttractive Chaos <attractor@live.co.uk>
Fri, 26 Mar 2021 02:58:27 +0000 (22:58 -0400)
kavl-lite.h

index 354e90d4b5a02b2a34e63aed0b149dabaa143d58..240ac8abb24b30888d6af56f8d967215dc6c5c83 100644 (file)
@@ -236,6 +236,21 @@ int main(void) {
                } \
        } while (0)
 
+#define kavll_size(__type, __head, __root, __cnt) do { \
+               __type *_p, *_q; \
+               *(__cnt) = 0; \
+               for (_p = __root; _p; _p = _q) { \
+                       if (_p->__head.p[0] == 0) { \
+                               _q = _p->__head.p[1]; \
+                               ++*(__cnt); \
+                       } else { \
+                               _q = _p->__head.p[0]; \
+                               _p->__head.p[0] = _q->__head.p[1]; \
+                               _q->__head.p[1] = _p; \
+                       } \
+               } \
+       } while (0)
+
 #define __KAVLL_ITR(pre, __scope, __type, __head, __cmp) \
        typedef struct pre##_itr_t { \
                const __type *stack[KAVLL_MAX_DEPTH], **top, *right; /* _right_ points to the right child of *top */ \