WvStreams
unihashtree.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * UniConf low-level tree storage abstraction.
00006  */
00007 #ifndef __UNIHASHTREE_H
00008 #define __UNIHASHTREE_H
00009 
00010 #include "uniconfkey.h"
00011 #include "wvtr1.h"
00012 #include "wvscatterhash.h"
00013 
00014 class UniHashTreeBase;
00015 
00016 // parameters: a node (won't be NULL), userdata
00017 typedef wv::function<void(const UniHashTreeBase*,
00018                           void*)> UniHashTreeBaseVisitor;
00019 // parameters: 1st node (may be NULL), 2nd node (may be NULL), userdata
00020 typedef wv::function<bool(const UniHashTreeBase*, 
00021                           const UniHashTreeBase*)> UniHashTreeBaseComparator;
00022 
00023 class UniHashTreeBase
00024 {
00025 protected:
00026     struct Accessor
00027     {
00028         static const UniConfKey *get_key(const UniHashTreeBase *obj)
00029             { return &obj->key(); }
00030     };
00031 
00032     typedef WvScatterHash<UniHashTreeBase, UniConfKey, Accessor> Container;
00033     typedef UniHashTreeBaseVisitor BaseVisitor;
00034     typedef UniHashTreeBaseComparator BaseComparator;
00035 
00036 public:
00037     ~UniHashTreeBase();
00038 
00040     const UniConfKey &key() const
00041         { return xkey; }
00042 
00044     bool haschildren() const;
00045 
00046 protected:
00047     UniHashTreeBase(UniHashTreeBase *parent, const UniConfKey &key);
00048 
00049     UniConfKey _fullkey(const UniHashTreeBase *ancestor = NULL) const;
00050     UniHashTreeBase *_find(const UniConfKey &key) const;
00051     UniHashTreeBase *_findchild(const UniConfKey &key) const;
00052 
00053     static bool _recursivecompare(
00054         const UniHashTreeBase *a, const UniHashTreeBase *b,
00055         const UniHashTreeBaseComparator &comparator);
00056 
00057     static void _recursive_unsorted_visit(
00058         const UniHashTreeBase *a,
00059         const UniHashTreeBaseVisitor &visitor, void *userdata,
00060         bool preorder, bool postorder);
00061 
00062     UniHashTreeBase *xparent; 
00063     Container *xchildren; 
00065 private:
00066     void _setparent(UniHashTreeBase *parent);
00067     UniHashTreeBase *_root() const;
00068 
00070     void link(UniHashTreeBase *node);
00071 
00073     void unlink(UniHashTreeBase *node);
00074 
00075     UniConfKey xkey;   
00077 protected:
00078     class Iter : public Container::Iter
00079     {
00080     public:
00081         Iter(UniHashTreeBase &b) : Container::Iter(*b.xchildren) { }
00082     };
00083     friend class Iter;
00084 };
00085 
00086 #endif //__UNIHASHTREE_H