IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 4, 2007, 1:35:32 PM (19 years ago)
Author:
jhoblitt
Message:

add pxNode.data field
add pxNodeAddData()
add pxTreeBuilder()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/pxtree.c

    r15190 r15221  
    3232    psFree(node->parent);
    3333    psFree(node->children);
     34    psFree(node->data);
    3435}
    3536
     
    4344    node->parent = NULL;
    4445    node->children = psListAlloc(NULL);
     46    node->data = NULL;
    4547
    4648    psMemSetDeallocator(node, (psFreeFunc) pxNodeFree);
     
    6971    // [re]parent the child
    7072    child->parent = psMemIncrRefCounter(node);
     73
     74    return node;
     75}
     76
     77pxNode *pxNodeAddData(pxNode *node, psPtr data)
     78{
     79    node->data = psMemIncrRefCounter(data);
    7180
    7281    return node;
     
    151160pxNode *pxTreeFromMetadata(psMetadata *md)
    152161{
    153     psHash *nodeNames = psHashAlloc(10);
     162    psHash *forest = psHashAlloc(10);
    154163
    155164    psMetadataIterator *iter = psMetadataIteratorAlloc(md, 0, NULL);
     
    160169        }
    161170
     171        // add this node to the forest and hopefully it'll get attached to the
     172        // root tree
     173        pxTreeBuilder(forest, item->name, item->data.str, NULL);
     174    }
     175    psFree(iter);
     176
     177    pxNode *root = psHashLookup(forest, "root");
     178    psFree(forest);
     179
     180    return psMemIncrRefCounter(root);
     181}
     182
     183psHash *pxTreeBuilder(psHash *forest,
     184                      const char *nodeName,
     185                      const char *childName,
     186                      psPtr data)
     187{
     188    // try to find a node with this name
     189    pxNode *node = psHashLookup(forest, nodeName);
     190    if (!node) {
     191        // create a new node with this name
     192        node = pxNodeAlloc(nodeName);
     193        if (data) {
     194            pxNodeAddData(node, data);
     195        }
     196        // add it to the hash of nodes
     197        psHashAdd(forest, nodeName, node);
     198        // node may be used below because it will still have a ref count of
     199        // 1 from being on the hash, this is equivalent to the "view" we
     200        // get from a hashlookup
     201        psFree(node);
     202    }
     203
     204    // does this node declare a child?
     205    if (childName) {
    162206        // try to find a node with this name
    163         pxNode *node = psHashLookup(nodeNames, item->name);
    164         if (!node) {
     207        pxNode *child = psHashLookup(forest, childName);
     208        if (!child ) {
    165209            // create a new node with this name
    166             node = pxNodeAlloc(item->name);
     210            child = pxNodeAlloc(childName);
    167211            // add it to the hash of nodes
    168             psHashAdd(nodeNames, item->name, node);
    169             // node may be used below because it will still have a ref count of
    170             // 1 from being on the hash, this is equivalent to the "view" we
    171             // get from a hashlookup
    172             psFree(node);
     212            psHashAdd(forest, childName, child);
     213            // child may be used below because it will still have a ref
     214            // count of 1 from being on the hash, this is equivalent to the
     215            // "view" we get from a hashlookup
     216            psFree(child);
    173217        }
    174 
    175         // does this node declare a child?
    176         if (item->data.str) {
    177             // try to find a node with this name
    178             pxNode *child = psHashLookup(nodeNames, item->data.str);
    179             if (!child ) {
    180                 // create a new node with this name
    181                 child = pxNodeAlloc(item->data.str);
    182                 // add it to the hash of nodes
    183                 psHashAdd(nodeNames, item->data.str, child);
    184                 // child may be used below because it will still have a ref
    185                 // count of 1 from being on the hash, this is equivalent to the
    186                 // "view" we get from a hashlookup
    187                 psFree(child);
    188             }
    189             pxNodeAddChild(node, child);
    190         }
    191     }
    192     psFree(iter);
    193 
    194     pxNode *root = psHashLookup(nodeNames, "root");
    195     psFree(nodeNames);
    196 
    197     return psMemIncrRefCounter(root);
    198 }
     218        pxNodeAddChild(node, child);
     219    }
     220
     221    return forest;
     222}
Note: See TracChangeset for help on using the changeset viewer.