Chris@16: /*-----------------------------------------------------------------------------+ Chris@16: Copyright (c) 2007-2010: Joachim Faulhaber Chris@16: +------------------------------------------------------------------------------+ Chris@16: Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin Chris@16: +------------------------------------------------------------------------------+ Chris@16: Distributed under the Boost Software License, Version 1.0. Chris@16: (See accompanying file LICENCE.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt) Chris@16: +-----------------------------------------------------------------------------*/ Chris@16: #ifndef BOOST_ICL_SET_ALGO_HPP_JOFA_990225 Chris@16: #define BOOST_ICL_SET_ALGO_HPP_JOFA_990225 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: namespace boost{namespace icl Chris@16: { Chris@16: Chris@16: namespace Set Chris@16: { Chris@16: Chris@16: template Chris@16: bool common_range(IteratorT& lwb, IteratorT& upb, ObjectT& x1, const ConstObjectT& x2) Chris@16: { Chris@16: // lwb and upb are iterators of x1 marking the lower and upper bound of Chris@16: // the common range of x1 and x2. Chris@16: typedef typename ConstObjectT::const_iterator ConstObject_iterator; Chris@16: // ObjectT may be const or non const. Chris@16: typedef typename remove_const::type PureObjectT; Chris@16: Chris@16: lwb = x1.end(); Chris@16: upb = x1.end(); Chris@16: Chris@16: if(icl::is_empty(x1) || icl::is_empty(x2)) Chris@16: return false; Chris@16: Chris@16: IteratorT x1_fst_ = x1.begin(); Chris@16: IteratorT x1_lst_ = x1.end(); x1_lst_--; Chris@16: Chris@16: ConstObject_iterator x2_fst_ = x2.begin(); Chris@16: ConstObject_iterator x2_lst_ = x2.end(); x2_lst_--; Chris@16: Chris@16: typename ObjectT::key_compare key_less; Chris@16: if(key_less(icl::key_value< PureObjectT>(x1_lst_), Chris@16: icl::key_value(x2_fst_))) // {x1} {x2} Chris@16: return false; Chris@16: if(key_less(icl::key_value(x2_lst_), Chris@16: icl::key_value< PureObjectT>(x1_fst_))) // {x2} {x1} Chris@16: return false; Chris@16: Chris@16: // We do have a common range Chris@16: lwb = x1.lower_bound(icl::key_value(x2_fst_)); Chris@16: upb = x1.upper_bound(icl::key_value(x2_lst_)); Chris@16: Chris@16: return true; Chris@16: } Chris@16: Chris@16: Chris@16: /** Function template contained_in implements the subset relation. Chris@16: contained_in(sub, super) is true if sub is contained in super */ Chris@16: template Chris@16: inline bool within(const SetType& sub, const SetType& super) Chris@16: { Chris@16: if(&super == &sub) return true; Chris@16: if(icl::is_empty(sub)) return true; Chris@16: if(icl::is_empty(super)) return false; Chris@16: Chris@16: typename SetType::const_iterator common_lwb_, common_upb_; Chris@16: if(!common_range(common_lwb_, common_upb_, sub, super)) Chris@16: return false; Chris@16: Chris@16: typename SetType::const_iterator sub_ = common_lwb_, super_; Chris@16: while(sub_ != common_upb_) Chris@16: { Chris@16: super_ = super.find(*sub_++); Chris@16: if(super_ == super.end()) Chris@16: return false; Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@16: bool intersects(const SetType& left, const SetType& right) Chris@16: { Chris@16: typename SetType::const_iterator common_lwb_right_, common_upb_right_; Chris@16: if(!common_range(common_lwb_right_, common_upb_right_, right, left)) Chris@16: return false; Chris@16: Chris@16: typename SetType::const_iterator right_ = common_lwb_right_, found_; Chris@16: while(right_ != common_upb_right_) Chris@16: { Chris@16: found_ = left.find(*right_++); Chris@16: if(found_ != left.end()) Chris@16: return true; // found a common element Chris@16: } Chris@16: // found no common element Chris@16: return false; Chris@16: } Chris@16: Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4996) //'std::equal': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' Chris@16: #endif // I do guarantee here that I am using the parameters correctly :) Chris@16: Chris@16: /** Function template lexicographical_equal implements Chris@16: lexicographical equality. */ Chris@16: template Chris@16: inline bool lexicographical_equal(const SetType& left, const SetType& right) Chris@16: { Chris@16: if(&left == &right) Chris@16: return true; Chris@16: else return left.iterative_size() == right.iterative_size() Chris@16: && std::equal(left.begin(), left.end(), right.begin()); Chris@16: } Chris@16: Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: Chris@16: } // namespace Set Chris@16: Chris@16: }} // namespace icl boost Chris@16: Chris@16: #endif Chris@16: