Node.h

Go to the documentation of this file.
00001 /* Depends: A generic dependency tracker in C++
00002  * Copyright (c) 2004-2007, Ronald Landheer-Cieslak
00003  * All rights reserved
00004  * 
00005  * This is free software. You may distribute it and/or modify it and
00006  * distribute modified forms provided that the following terms are met:
00007  *
00008  * * Redistributions of the source code must retain the above copyright
00009  *   notice, this list of conditions and the following disclaimer;
00010  * * Redistributions in binary form must reproduce the above copyright
00011  *   notice, this list of conditions and the following disclaimer in
00012  *   the documentation and/or other materials provided with the distribution;
00013  * * None of the names of the authors of this software may be used to endorse
00014  *   or promote this software, derived software or any distribution of this 
00015  *   software or any distribution of which this software is part, without 
00016  *   prior written permission from the authors involved;
00017  * * Unless you have received a written statement from Ronald Landheer-Cieslak
00018  *   that says otherwise, the terms of the GNU General Public License, as 
00019  *   published by the Free Software Foundation, version 2 or (at your option)
00020  *   any later version, also apply.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00036 #ifndef _depends_details_node_h
00037 #define _depends_details_node_h
00038 
00039 namespace Depends
00040 {
00041         namespace Details
00042         {
00044                 template <typename NodeType>
00045                 struct ValueCompare
00046                 {
00047                         typedef typename NodeType::value_type value_type;
00048 
00049                         ValueCompare(const value_type & v) : val_(v) {}
00050 
00051                         bool operator()(const value_type & v) const { return val_ == v; }
00052                         bool operator()(const NodeType & n) const { return n.value_ == val_; }
00053                         bool operator()(const NodeType * n) const { return n->value_ == val_; }
00054                         
00055                         value_type val_;
00056                 };
00057                 
00058                 template <typename NodeType>
00059                 struct CompareNodesByContents
00060                 {
00061                         bool operator()(const NodeType & lhs, const NodeType & rhs)
00062                         {
00063                                 // we consider lhs < rhs to be true if
00064                                 if (lhs < rhs)  // compare by scores
00065                                         return true;
00066                                 else if (lhs.value_ < rhs.value_) // compare by values
00067                                         return true;
00068                                 else
00069                                         return std::lexicographical_compare(
00070                                                 boost::indirect_iterator< typename NodeType::targets_type::const_iterator >(lhs.targets_.begin()),
00071                                                 boost::indirect_iterator< typename NodeType::targets_type::const_iterator >(lhs.targets_.end()),
00072                                                 boost::indirect_iterator< typename NodeType::targets_type::const_iterator >(rhs.targets_.begin()),
00073                                                 boost::indirect_iterator< typename NodeType::targets_type::const_iterator >(rhs.targets_.end()),
00074                                                 *this);
00075                         }
00076                 };
00077 
00079                 template <class ValueType, typename ScoreType>
00080                 struct Node
00081                 {
00082                         typedef ValueType value_type;
00083                         typedef ScoreType score_type;
00084                         typedef std::vector<Node*> targets_type;
00085                         
00086                         enum Flag { VISITED = 1 };
00087 
00088                         Node(const ValueType & v)
00089                                 : value_(v),
00090                                   score_(1),
00091                                   flags_(0)
00092                         {
00093                         }
00094 
00095                         bool operator<(const Node & n) const
00096                         {
00097                                 return score_ < n.score_;
00098                         }
00099 
00100                         bool operator==(const Node & rhs) const
00101                         {
00102                                 return
00103                                         (value_ == rhs.value_) &&
00104                                         (score_ == rhs.score_) &&
00105                                         targets_.size() == rhs.targets_.size() &&
00106                                         std::equal(
00107                                                 boost::indirect_iterator< typename targets_type::const_iterator >(targets_.begin()),
00108                                                 boost::indirect_iterator< typename targets_type::const_iterator >(targets_.end()),
00109                                                 boost::indirect_iterator< typename targets_type::const_iterator >(rhs.targets_.begin())
00110                                         );
00111                         }
00112                         
00113                         targets_type targets_;
00114                         ValueType value_;
00115                         ScoreType score_;
00116                         unsigned int flags_;
00117 
00118                 private :
00119                         Node()
00120                                 : score_(0),
00121                                   flags_(0)
00122                         { /* only here for serialization */ }
00123 
00124 #if DEPENDS_SUPPORT_SERIALIZATION
00125                         template < typename Archive >
00126                         void serialize( Archive & ar, const unsigned int version )
00127                         {
00128                                 ar & boost::serialization::make_nvp("targets_", targets_)
00129                                    & boost::serialization::make_nvp("value_", value_)
00130                                    & boost::serialization::make_nvp("score_", score_)
00131                                    & boost::serialization::make_nvp("flags_", flags_)
00132                                    ;
00133                         }
00134 
00135                         friend class boost::serialization::access;
00136 #endif
00137                 };
00138         }
00139 }
00140 
00141 #endif
00142 

Generated on Sat Aug 11 11:55:55 2007 for Depends by  doxygen 1.5.1