"Fossies" - the Fresh Open Source Software Archive

Member "getdp-3.1.0-source/Common/TreeUtils.cpp" (29 Dec 2018, 2549 Bytes) of package /linux/privat/getdp-3.1.0-source.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TreeUtils.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.0.4_vs_3.1.0.

    1 // GetDP - Copyright (C) 1997-2019 P. Dular and C. Geuzaine, University of Liege
    2 //
    3 // See the LICENSE.txt file for license information. Please report all
    4 // issues on https://gitlab.onelab.info/getdp/getdp/issues.
    5 //
    6 // Contributor(s):
    7 //   Marc Ume
    8 //
    9 
   10 #include <stdlib.h>
   11 #include <string.h>
   12 #include "GetDPConfig.h"
   13 #include "MallocUtils.h"
   14 #include "TreeUtils.h"
   15 
   16 #if !defined(HAVE_GMSH)
   17 
   18 Tree_T *Tree_Create(int size, int (*fcmp) (const void *a, const void *b))
   19 {
   20   Tree_T *tree = (Tree_T*)Malloc(sizeof(Tree_T));
   21   tree->size = size;
   22   tree->root = avl_init_table(fcmp);
   23   return tree;
   24 }
   25 
   26 void Tree_Delete(Tree_T * tree)
   27 {
   28   if(!tree) return;
   29   avl_free_table(tree->root, Free, 0);
   30   Free(tree);
   31 }
   32 
   33 void *Tree_Add(Tree_T * tree, void *data)
   34 {
   35   if(!tree) return 0;
   36   void *ptr = Malloc(tree->size);
   37   memcpy(ptr, data, tree->size);
   38   avl_insert(tree->root, ptr, ptr);
   39   return ptr;
   40 }
   41 
   42 int Tree_Nbr(Tree_T * tree)
   43 {
   44   if(!tree) return 0;
   45   return avl_count(tree->root);
   46 }
   47 
   48 int Tree_Insert(Tree_T * tree, void *data)
   49 {
   50   if(!Tree_Search(tree, data)){
   51     Tree_Add(tree, data);
   52     return 1;
   53   }
   54   return 0;
   55 }
   56 
   57 int Tree_Search(Tree_T * tree, void *data)
   58 {
   59   if(!tree) return 0;
   60   void *ptr;
   61   return avl_lookup(tree->root, data, &ptr);
   62 }
   63 
   64 int Tree_Query(Tree_T * tree, void *data)
   65 {
   66   if(!tree) return 0;
   67   void *ptr;
   68   if(!avl_lookup(tree->root, data, &ptr))
   69     return 0;
   70   memcpy(data, ptr, tree->size);
   71   return 1;
   72 }
   73 
   74 void *Tree_PQuery(Tree_T * tree, void *data)
   75 {
   76   if(!tree) return 0;
   77   void *ptr;
   78   if(!avl_lookup(tree->root, data, &ptr))
   79     return 0;
   80   return ptr;
   81 }
   82 
   83 int Tree_Suppress(Tree_T * tree, void *data)
   84 {
   85   if(!tree) return 0;
   86   void *ptr = data;
   87   if(!avl_delete(tree->root, &ptr, &ptr))
   88     return 0;
   89   Free(ptr);
   90   return 1;
   91 }
   92 
   93 int Tree_Size(Tree_T * tree)
   94 {
   95   if(!tree) return 0;
   96   return tree->size;
   97 }
   98 
   99 void Tree_Action(Tree_T *tree, void (*action) (void *data, void *dummy))
  100 {
  101   if(!tree) return;
  102 
  103   avl_foreach(tree->root, action, AVL_FORWARD);
  104 }
  105 
  106 static List_T *pListTransfer;
  107 
  108 void TransferList(void *a, void *b)
  109 {
  110   List_Add(pListTransfer, a);
  111 }
  112 
  113 List_T *Tree2List(Tree_T * pTree)
  114 {
  115   int Nb;
  116   Nb = Tree_Nbr(pTree);
  117   if(Nb == 0)
  118     Nb = 1;
  119   pListTransfer = List_Create(Nb, Nb, Tree_Size(pTree));
  120   Tree_Action(pTree, TransferList);
  121   return pListTransfer;
  122 }
  123 
  124 #endif
  125 
  126 int Tree_Replace(Tree_T *tree, void *data)
  127 {
  128   void *ptr;
  129   int state;
  130 
  131   state = avl_lookup(tree->root, data, &ptr);
  132   if (state == 0) {
  133     Tree_Add(tree,data);
  134     return 0;
  135   }
  136   else {
  137     memcpy(ptr, data, tree->size);
  138     return 1;
  139   }
  140 }