"Fossies" - the Fresh Open Source Software Archive

Member "apache-zookeeper-3.5.6/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/SimpleTree.h" (5 Oct 2019, 4655 Bytes) of package /linux/misc/apache-zookeeper-3.5.6.tar.gz:


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 "SimpleTree.h" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * Licensed to the Apache Software Foundation (ASF) under one
    3  * or more contributor license agreements.  See the NOTICE file
    4  * distributed with this work for additional information
    5  * regarding copyright ownership.  The ASF licenses this file
    6  * to you under the Apache License, Version 2.0 (the
    7  * "License"); you may not use this file except in compliance
    8  * with the License.  You may obtain a copy of the License at
    9  *
   10  *     http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  * Unless required by applicable law or agreed to in writing, software
   13  * distributed under the License is distributed on an "AS IS" BASIS,
   14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15  * See the License for the specific language governing permissions and
   16  * limitations under the License.
   17  */
   18 
   19 #ifndef __SIMPLE_TREE_H__
   20 #define __SIMPLE_TREE_H__
   21 
   22 #include <vector>
   23 #include <boost/shared_ptr.hpp>
   24 
   25 namespace zktreeutil
   26 {
   27    using std::vector;
   28 
   29    /**
   30     * \brief A simple tree data-structure template.
   31     */
   32    template < class KeyType, class DataType > class SimpleTreeNode
   33    {
   34       private:
   35          /**
   36           * \brief The type representing simple-tree node smart-pointer.
   37           */
   38          typedef boost::shared_ptr< SimpleTreeNode< KeyType, DataType > > SimpleTreeNodeSptr;
   39 
   40       public:
   41          /**
   42           * \brief Constructor.
   43           * 
   44           * @param isRoot the flag indicating whether the node is root.
   45           */
   46          SimpleTreeNode (bool isRoot=false) : isRoot_(isRoot)
   47          {
   48          }
   49 
   50          /**
   51           * \brief Constructor.
   52           * 
   53           * @param key the key stored at the tree node
   54           * @param isRoot the flag indicating whether the node is root
   55           */
   56          SimpleTreeNode (const KeyType& key, bool isRoot=false) :
   57             isRoot_(isRoot), key_(key)
   58          {
   59          }
   60 
   61          /**
   62           * \brief Constructor.
   63           * 
   64           * @param key the key stored at the tree node
   65           * @param val the value stored at the tree node
   66           * @param isRoot the flag indicating whether the node is root
   67           */
   68          SimpleTreeNode (const KeyType& key, const DataType& val, bool isRoot=false) :
   69             isRoot_(isRoot), key_(key), val_(val)
   70          {
   71          }
   72 
   73          /**
   74           * \brief Destructor.
   75           */
   76          ~SimpleTreeNode () throw() {}
   77 
   78          /**
   79           * \brief Add a child node to this node.
   80           *
   81           * @param node the child node to be added
   82           */
   83          void addChild (const SimpleTreeNodeSptr node) { children_.push_back (node); }
   84 
   85          /**
   86           * \brief Sets the key of this node.
   87           *
   88           * @param key the key to be set
   89           */
   90          void setKey (const KeyType& key) { key_ = key; }
   91 
   92          /**
   93           * \brief Sets the data of this node.
   94           *
   95           * @param val the value to be set
   96           */
   97          void setData (const DataType& val) { val_ = val; }
   98 
   99          /**
  100           * \brief Gets the key of this node.
  101           *
  102           * @return the key of this node 
  103           */
  104          KeyType getKey () const { return key_; }
  105 
  106          /**
  107           * \brief Gets the data of this node.
  108           *
  109           * @return the value of this node 
  110           */
  111          DataType getData () const { return val_; }
  112 
  113          /**
  114           * \brief Gets the i'th of this node.
  115           *
  116           * @param idx the index of the child node
  117           * @return the child node
  118           */
  119          SimpleTreeNodeSptr getChild (unsigned idx) const { return children_[idx]; }
  120 
  121          /**
  122           * \brief Gets the number of children of this node.
  123           *
  124           * @return the number of children
  125           */
  126          unsigned numChildren () const { return children_.size(); }
  127 
  128          /**
  129           * \brief Indicates whether this node is root.
  130           *
  131           * @return 'true' if this node is root, 'false' otherwise
  132           */
  133          bool isRoot () const { return isRoot_; }
  134 
  135          /**
  136           * \brief Indicates whether this node is leaf node.
  137           *
  138           * @return 'true' if this node is leaf node, 'false' otherwise
  139           */
  140          bool isLeaf () const { return !numChildren(); }
  141 
  142       private:
  143          bool isRoot_;                                        // Flag indicates if the node is root
  144          KeyType key_;                                        // Key of this node
  145          DataType val_;                                        // Value of this node
  146          vector< SimpleTreeNodeSptr > children_;    // List of children of this node
  147    };
  148 }
  149 
  150 #endif // __SIMPLE_TREE_H__