| 1 | /*
 | 
|---|
| 2 |  * SubsetMap.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jul 3, 2012
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef SUBSETMAP_HPP_
 | 
|---|
| 9 | #define SUBSETMAP_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | // include config.h
 | 
|---|
| 13 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 14 | #include <config.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <boost/shared_ptr.hpp>
 | 
|---|
| 18 | #include <map>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "IndexSetContainer.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | class SubsetMapTest;
 | 
|---|
| 25 | 
 | 
|---|
| 26 | /** A SubsetMap is a map containing shared_ptr of IndexSet instances that knows
 | 
|---|
| 27 |  * about which set is contained in which for fast lookup for SubsetValues.
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | class SubsetMap
 | 
|---|
| 30 | {
 | 
|---|
| 31 |   //!> grant unit test access
 | 
|---|
| 32 |   friend class SubsetMapTest;
 | 
|---|
| 33 | public:
 | 
|---|
| 34 |   //!> typedef for wrapping an instance in a shared_ptr
 | 
|---|
| 35 |   typedef boost::shared_ptr<SubsetMap> ptr;
 | 
|---|
| 36 | 
 | 
|---|
| 37 |   /** Constructor for SubsetMap that creates the internal map from IndexSet to a
 | 
|---|
| 38 |    * container with all contained sets.
 | 
|---|
| 39 |    *
 | 
|---|
| 40 |    * To this end, we first put each IndexSet in a multimap from size to IndexSet.
 | 
|---|
| 41 |    * Then we go "level-wise" from low to high through this map and use gatherSubsets()
 | 
|---|
| 42 |    * on each set as we are then sure that all contained subsets have already been
 | 
|---|
| 43 |    * initialized.
 | 
|---|
| 44 |    *
 | 
|---|
| 45 |    * @param _container container with IndexSet
 | 
|---|
| 46 |    */
 | 
|---|
| 47 |   SubsetMap(IndexSetContainer &_container);
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   /** Getter for all subsets of set \a ptr.
 | 
|---|
| 50 |    *
 | 
|---|
| 51 |    * @param ptr IndexSet
 | 
|---|
| 52 |    * @return IndexSetContainer with all contained sets
 | 
|---|
| 53 |    */
 | 
|---|
| 54 |   IndexSetContainer::ptr & getSubsets(const IndexSet_ptr &ptr) {
 | 
|---|
| 55 |     Lookup_t::iterator lookupiter = Lookup.find(ptr);
 | 
|---|
| 56 |     ASSERT(lookupiter != Lookup.end(),
 | 
|---|
| 57 |         "SubsetMap::getSubsets() - the set "+toString(*ptr)+" is not in the Lookup.");
 | 
|---|
| 58 |     return lookupiter->second;
 | 
|---|
| 59 |   }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | private:
 | 
|---|
| 62 |   /** Private function to fill the internal Lookup for a given IndexSet \a ptr.
 | 
|---|
| 63 |    *
 | 
|---|
| 64 |    * @param ptr IndexSet
 | 
|---|
| 65 |    */
 | 
|---|
| 66 |   void gatherSubsets(const IndexSet_ptr &ptr);
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   /** Returns the index of \a subset in the power subsets of \a ptr.
 | 
|---|
| 69 |    *
 | 
|---|
| 70 |    * @param ptr set to check
 | 
|---|
| 71 |    * @param subset subset whose lexicographical position to find in \a ptr
 | 
|---|
| 72 |    * @return index of this subset
 | 
|---|
| 73 |    */
 | 
|---|
| 74 |   static const size_t getPowerSetIndex(const IndexSet_ptr &ptr, const IndexSet &subset);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   /** Returns the number of power subsets for a given set size.
 | 
|---|
| 77 |    *
 | 
|---|
| 78 |    * @param SetSize size of the set
 | 
|---|
| 79 |    * @return possible number of true subsets
 | 
|---|
| 80 |    */
 | 
|---|
| 81 |   static size_t getNoPowerSets(const size_t SetSize) {
 | 
|---|
| 82 |     return (1 << SetSize);
 | 
|---|
| 83 |   }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   /** Returns the subset for a given \a _set by its power set index \a PowerSetNo.
 | 
|---|
| 86 |    *
 | 
|---|
| 87 |    * The idea is that each bit in \a PowerSetNo encodes whether the Index at the
 | 
|---|
| 88 |    * position in the set is to appear in the subset or not. Hence, we use std::bitset
 | 
|---|
| 89 |    * to construct the bit representation from \a PowerSetNo and then go through each
 | 
|---|
| 90 |    * bit and either insert into the subset or not.
 | 
|---|
| 91 |    *
 | 
|---|
| 92 |    * @param _set index set
 | 
|---|
| 93 |    * @param PowerSetNo subset index
 | 
|---|
| 94 |    * @return specific power set subset given by the index
 | 
|---|
| 95 |    */
 | 
|---|
| 96 |   static IndexSet getSubset(const IndexSet &_set, size_t PowerSetNo);
 | 
|---|
| 97 | 
 | 
|---|
| 98 | private:
 | 
|---|
| 99 |   //!> enumeration of states whether a subset is contained or not
 | 
|---|
| 100 |   enum ContainedState {
 | 
|---|
| 101 |     NotContained = 0,
 | 
|---|
| 102 |     Contained = 1,
 | 
|---|
| 103 |   };
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   //!> temporary IndexSet_ptr
 | 
|---|
| 106 |   IndexSet_ptr tempset;
 | 
|---|
| 107 |   //!> enumeration trick to define a maximum subset size
 | 
|---|
| 108 |   enum {MAX_SETSIZE = 32};
 | 
|---|
| 109 |   //!> typedef for the specific map from IndexSet to all contained IndexSets
 | 
|---|
| 110 |   typedef std::map< IndexSet_ptr, IndexSetContainer::ptr,
 | 
|---|
| 111 |       IndexSetContainer::Comparator_t> Lookup_t;
 | 
|---|
| 112 |   Lookup_t Lookup;
 | 
|---|
| 113 | };
 | 
|---|
| 114 | 
 | 
|---|
| 115 | #endif /* SUBSETMAP_HPP_ */
 | 
|---|