PermLib
abstract_bsgs_helpers.h
00001 // ---------------------------------------------------------------------------
00002 //
00003 //  This file is part of PermLib.
00004 //
00005 // Copyright (c) 2009-2012 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 #include <boost/scoped_ptr.hpp>
00033 #include <algorithm>
00034 #include <vector>
00035 #include <set>
00036 
00037 #ifndef ABSTRACT_BSGS_HELPERS_H_
00038 #define ABSTRACT_BSGS_HELPERS_H_
00039 
00040 namespace permlib {
00041 namespace helpers {
00042 
00044 class SupportRestriction {
00045 public:
00047         virtual ~SupportRestriction() { }
00048         
00052         virtual bool canBeIgnored() const = 0;
00056         virtual const std::vector<dom_int>* set() const = 0;
00057 };
00058 
00060 class BaseSupportRestriction : public SupportRestriction {
00061 public:
00066         BaseSupportRestriction(const boost::shared_ptr<std::set<dom_int> >& support, const std::vector<dom_int>& s)
00067                 : m_support(support), m_originalSet(s) {}
00068         
00072         virtual bool canBeIgnored() const { return false; }
00076         virtual const std::vector<dom_int>* set() const { return &m_originalSet; }
00077 protected:
00078         const boost::shared_ptr<std::set<dom_int> > m_support;
00079         const std::vector<dom_int>& m_originalSet;
00080 };
00081 
00083 class ReducedSupportRestriction : public BaseSupportRestriction {
00084 public:
00089         ReducedSupportRestriction(const boost::shared_ptr<std::set<dom_int> >& support, const std::vector<dom_int>& s)
00090                 : BaseSupportRestriction(support, s) {}
00091         
00095         virtual bool canBeIgnored() const {
00096                 BOOST_ASSERT( m_support );
00097                 return std::find_first_of(m_support->begin(), m_support->end(), m_originalSet.begin(), m_originalSet.end()) == m_support->end();
00098         }
00099 };
00100 
00102 class FullSupportRestriction : public BaseSupportRestriction {
00103 public:
00108         FullSupportRestriction(const boost::shared_ptr<std::set<dom_int> >& support, const std::vector<dom_int>& s) 
00109                 : BaseSupportRestriction(support, s), m_reducedSet(0) 
00110         {
00111                 m_reducedSet = new std::vector<dom_int>();
00112                 m_reducedSet->reserve(s.size());
00113                 std::vector<dom_int> sorted(s);
00114                 std::sort(sorted.begin(), sorted.end());
00115                 std::set_intersection(m_support->begin(), m_support->end(), sorted.begin(), sorted.end(), std::back_inserter(*m_reducedSet));
00116         }
00117         virtual ~FullSupportRestriction() { delete m_reducedSet; }
00118 
00122         bool canBeIgnored() const {
00123                 BOOST_ASSERT( m_reducedSet );
00124                 return m_reducedSet->empty();
00125         }
00129         const std::vector<dom_int>* set() const {
00130                 return m_reducedSet;
00131         }
00132 private:
00133         std::vector<dom_int>* m_reducedSet;
00134 };
00135 
00136 } // end NS permlib::helpers
00137 } // end NS permlib
00138 
00139 #endif