permlib  0.2.6
Library for permutation computations
 All Classes Functions Variables Typedefs Enumerations Friends
include/permlib/search/partition/set_image_refinement.h
00001 // ---------------------------------------------------------------------------
00002 //
00003 //  This file is part of PermLib.
00004 //
00005 // Copyright (c) 2009-2011 Thomas Rehn <thomas@carmen76.de>
00006 // All rights reserved.
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions
00010 // are met:
00011 // 1. Redistributions of source code must retain the above copyright
00012 //    notice, this list of conditions and the following disclaimer.
00013 // 2. Redistributions in binary form must reproduce the above copyright
00014 //    notice, this list of conditions and the following disclaimer in the
00015 //    documentation and/or other materials provided with the distribution.
00016 // 3. The name of the author may not be used to endorse or promote products
00017 //    derived from this software without specific prior written permission.
00018 // 
00019 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00020 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00021 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00022 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00024 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00028 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // ---------------------------------------------------------------------------
00031 
00032 
00033 #ifndef SETIMAGEREFINEMENT_H_
00034 #define SETIMAGEREFINEMENT_H_
00035 
00036 #include <permlib/predicate/pointwise_stabilizer_predicate.h>
00037 
00038 #include <permlib/search/partition/partition.h>
00039 #include <permlib/search/partition/refinement.h>
00040 
00041 #include <algorithm>
00042 #include <boost/dynamic_bitset.hpp>
00043 #include <boost/foreach.hpp>
00044 
00045 namespace permlib {
00046 namespace partition {
00047 
00050 template<class PERM>
00051 class SetImageRefinement : public Refinement<PERM> {
00052 public:
00054 
00061         template<class InputIterator>
00062         SetImageRefinement(unsigned long n, InputIterator begin, InputIterator end, InputIterator beginImg, InputIterator endImg);
00063         
00064         virtual unsigned int apply(Partition& pi) const;
00065         virtual unsigned int apply2(Partition& pi, const PERM& t) const;
00066         
00067         virtual bool init(Partition& pi);
00068 private:
00069         std::vector<unsigned long> delta;
00070         std::vector<unsigned long> gamma;
00071 };
00072 
00073 template<class PERM>
00074 template<class InputIterator>
00075 SetImageRefinement<PERM>::SetImageRefinement(unsigned long n, InputIterator begin, InputIterator end, InputIterator beginImg, InputIterator endImg) 
00076         : Refinement<PERM>(n, Default), delta(begin, end), gamma(beginImg, endImg)
00077 {
00078         std::sort(delta.begin(), delta.end());
00079         std::sort(gamma.begin(), gamma.end());
00080 }
00081 
00082 template<class PERM>
00083 unsigned int SetImageRefinement<PERM>::apply(Partition& pi) const {
00084         BOOST_ASSERT( this->initialized() );
00085         unsigned int ret = 0;
00086         BOOST_FOREACH(unsigned int cell, Refinement<PERM>::m_cellPairs) {
00087                 PERMLIB_DEBUG(std::cout << "apply set image1 " << cell << std::endl;)
00088                 if (pi.intersect(delta.begin(), delta.end(), cell))
00089                         ++ret;
00090         }
00091         return ret;
00092 }
00093 
00094 template<class PERM>
00095 unsigned int SetImageRefinement<PERM>::apply2(Partition& pi, const PERM& t) const {
00096         BOOST_ASSERT( this->initialized() );
00097         unsigned int ret = 0;
00098         BOOST_FOREACH(unsigned int cell, Refinement<PERM>::m_cellPairs) {
00099                 PERMLIB_DEBUG(std::cout << "apply set image2 " << cell << std::endl;)
00100                 if (pi.intersect(gamma.begin(), gamma.end(), cell))
00101                         ++ret;
00102         }
00103         return ret;
00104 }
00105 
00106 template<class PERM>
00107 bool SetImageRefinement<PERM>::init(Partition& pi) {
00108         for (unsigned int c = 0; c < pi.cells(); ++c) {
00109                 if (pi.intersect(delta.begin(), delta.end(), c))
00110                         Refinement<PERM>::m_cellPairs.push_back(c);
00111         }
00112         if (!Refinement<PERM>::m_cellPairs.empty()) {
00113                 typename Refinement<PERM>::RefinementPtr ref(new SetImageRefinement<PERM>(*this));
00114                 Refinement<PERM>::m_backtrackRefinements.push_back(ref);
00115                 return true;
00116         }
00117         return false;
00118 }
00119 
00120 }
00121 }
00122 
00123 #endif // -- SETIMAGEREFINEMENT_H_