]> pd.if.org Git - zpackage/blobdiff - lib/jsw/jsw_atree.c
support for package dependencies
[zpackage] / lib / jsw / jsw_atree.c
index ba66bd0ccb6eb984c285a178c2b6f93383de0122..d73e22afc18898ad80d579f5c61efa0e2ce4075f 100644 (file)
@@ -1,3 +1,4 @@
+#define _POSIX_C_SOURCE 200809L
 /*
   Andersson tree library
 
       4) Bug in jsw_aerase:
            Search for successor should save the path
 */
-#include "jsw_atree.h"
+#include "jsw.h"
 
-#ifdef __cplusplus
-#include <cstdlib>
-
-using std::malloc;
-using std::free;
-using std::size_t;
-#else
 #include <stdlib.h>
-#endif
+#include <stdio.h>
+#include <string.h>
 
 #ifndef HEIGHT_LIMIT
 #define HEIGHT_LIMIT 64 /* Tallest allowable tree */
@@ -72,9 +67,13 @@ struct jsw_atrav {
   }                                                                \
 } while(0)
 
+int jsw_afind_strcmp(const void *a, const void *b) {
+       return strcmp(a, b);
+}
+
 static jsw_anode_t *new_node ( jsw_atree_t *tree, void *data )
 {
-  jsw_anode_t *rn = (jsw_anode_t *)malloc ( sizeof *rn );
+  jsw_anode_t *rn = malloc ( sizeof *rn );
 
   if ( rn == NULL )
     return tree->nil;
@@ -86,15 +85,19 @@ static jsw_anode_t *new_node ( jsw_atree_t *tree, void *data )
   return rn;
 }
 
+jsw_atree_t *jsw_anew_str(void) {
+       return jsw_anew(jsw_afind_strcmp, (dup_f)strdup, (rel_f)free);
+}
+
 jsw_atree_t *jsw_anew ( cmp_f cmp, dup_f dup, rel_f rel )
 {
-  jsw_atree_t *rt = (jsw_atree_t *)malloc ( sizeof *rt );
+  jsw_atree_t *rt = malloc ( sizeof *rt );
 
   if ( rt == NULL )
     return NULL;
 
   /* Initialize sentinel */
-  rt->nil = (jsw_anode_t *)malloc ( sizeof *rt->nil );
+  rt->nil = malloc ( sizeof *rt->nil );
   if ( rt->nil == NULL ) {
     free ( rt );
     return NULL;
@@ -114,10 +117,16 @@ jsw_atree_t *jsw_anew ( cmp_f cmp, dup_f dup, rel_f rel )
   return rt;
 }
 
-void jsw_adelete ( jsw_atree_t *tree )
-{
-  jsw_anode_t *it = tree->root;
-  jsw_anode_t *save;
+void jsw_adelete(jsw_atree_t *tree) {
+       jsw_anode_t *it;
+       jsw_anode_t *save;
+
+       if (!tree) {
+               return;
+       }
+
+       it = tree->root;
+
 
   /* Destruction by rotation */
   while ( it != tree->nil ) {
@@ -142,12 +151,12 @@ void jsw_adelete ( jsw_atree_t *tree )
   free ( tree );
 }
 
-void *jsw_afind ( jsw_atree_t *tree, void *data )
-{
+void *jsw_afind ( jsw_atree_t *tree, void *data) {
   jsw_anode_t *it = tree->root;
+  int cmp = 0;
 
   while ( it != tree->nil ) {
-    int cmp = tree->cmp ( it->data, data );
+    cmp = tree->cmp(it->data, data);
 
     if ( cmp == 0 )
       break;