libBf 0.1
|
00001 #ifndef PARTITION_H 00002 #define PARTITION_H 00003 00004 namespace bf { 00005 00008 template <typename Derived> 00009 class partition_policy 00010 { 00011 public: 00012 unsigned parts() const 00013 { 00014 return derived().parts(); 00015 } 00016 00017 private: 00018 // 00019 // CRTP interface 00020 // 00021 Derived& derived() 00022 { 00023 return *static_cast<Derived*>(this); 00024 } 00025 00026 const Derived& derived() const 00027 { 00028 return *static_cast<const Derived*>(this); 00029 } 00030 }; 00031 00034 class no_partitioning : public partition_policy<no_partitioning> 00035 { 00036 public: 00037 no_partitioning(unsigned /* unused */) 00038 { 00039 } 00040 00041 unsigned parts() const 00042 { 00043 return 1; 00044 } 00045 00046 unsigned position(std::size_t hash, unsigned i, unsigned cells) const 00047 { 00048 return hash % cells; 00049 } 00050 }; 00051 00055 struct partitioning : public partition_policy<partitioning> 00056 { 00057 public: 00058 partitioning(unsigned parts) 00059 : parts_(parts) 00060 { 00061 } 00062 00063 unsigned parts() const 00064 { 00065 return parts_; 00066 } 00067 00068 unsigned position(std::size_t hash, unsigned i, unsigned cells) const 00069 { 00070 assert(cells % parts_ == 0); 00071 00072 auto part_size = cells / parts_; 00073 return hash % part_size + (i * part_size); 00074 } 00075 00076 private: 00077 unsigned parts_; 00078 }; 00079 00080 } // namespace partition 00081 00082 #endif