1 #ifndef MPQC_CI_STRING_HPP
2 #define MPQC_CI_STRING_HPP
4 #include "mpqc/range.hpp"
8 #include <boost/iterator/zip_iterator.hpp>
9 #include <boost/tuple/tuple.hpp>
18 inline int index(
int i,
int j) {
21 return (i + (j * j + j) / 2);
25 template<
class >
class List;
28 typedef std::bitset<64> bitset;
31 typedef const int* const_iterator;
32 const_iterator begin()
const {
35 const_iterator end()
const {
36 return data_ + count_;
52 MPQC_ASSERT(0 < size && size < 64);
53 MPQC_ASSERT(0 < count && count < size);
56 this->value_ = bitset((1 << count) - 1);
64 MPQC_ASSERT(value.size() <= 64);
65 std::reverse(value.begin(), value.end());
66 this->size_ = value.size();
67 this->value_ = bitset(value);
68 this->count_ = bitset(value).count();
72 this->size_ = s.size_;
73 this->count_ = s.count_;
74 this->value_ = s.value_;
77 const void* data()
const {
78 return (
const char*) &this->value_;
81 bool operator[](
size_t i)
const {
82 return this->value_[i];
89 size_t count()
const {
93 orbitals occ()
const {
94 orbitals o(this->count());
95 for (
size_t i = 0, j = 0; i < this->count(); ++i) {
96 while (!this->
operator[](j))
102 String swap(
size_t i,
size_t j)
const {
111 operator std::string()
const {
112 std::string s(this->size_,
'*');
113 for (
size_t i = 0; i < this->size_; ++i) {
114 s[i] = (
"01"[this->operator[](i)]);
119 size_t to_ulong()
const {
120 return this->value_.to_ulong();
125 size_t operator^(
const String &b)
const {
126 return (b.to_ulong() ^ this->to_ulong());
129 return (b & this->to_ulong());
131 static size_t difference(
const String &a,
const String &b) {
132 MPQC_ASSERT(a.size() == b.size());
133 MPQC_ASSERT(a.count() == b.count());
134 size_t n = String::count(a ^ b);
136 MPQC_ASSERT(!(n % 2));
139 static size_t count(
size_t b) {
140 return bitset(b).count();
144 size_t size_, count_;
145 void flip(
size_t i) {
146 this->value_.flip(i);
150 inline std::ostream&
operator<<(std::ostream& os,
const String &s) {
151 os << std::string(s);
157 inline int sgn(
size_t ij) {
158 return (ij % 2) ? -1 : 1;
165 inline int sgn(
const String &I,
int i,
int j) {
166 uint64_t b = I.to_ulong();
167 int n = std::abs(i - j);
168 if (j < i) std::swap(i, j);
169 b = b & ((uint64_t(1) << j) - 1);
170 b = b >> i; b = b >> 1;
171 size_t p = String::bitset(b).count();
174 MPQC_ASSERT(p < I.count());
176 for (
int k = std::min(i,j)+1; k < std::max(i,j); ++k) {
181 std::string(I).c_str(), I.to_ulong(), i,j, b, p, q);
194 template<
class IndexType>
195 struct String::List {
196 typedef typename std::vector<String>::iterator iterator;
197 typedef typename std::vector<String>::const_iterator const_iterator;
198 List(
const std::vector<String> &v) {
200 index_.initialize(data_);
201 for (
int i = 0; i < v.size(); ++i) {
205 size_t size()
const {
208 const_iterator begin()
const {
209 return data_.begin();
211 const_iterator end()
const {
215 return data_.begin();
220 size_t operator[](
const String &s)
const {
221 return perm_[this->index_[s]];
223 const String& operator[](
size_t i)
const {
226 const String& at(
size_t i)
const {
234 std::stable_sort(data_.begin(), data_.end(), f);
236 for (
int i = 0; i < data_.size(); ++i) {
237 perm_[this->index_[data_[i]]] = i;
240 const std::vector<String>& data()
const {
return data_; }
242 std::vector<String> data_;
243 std::vector<int> perm_;
248 explicit ZipSort(F f) : f(f) {}
249 template <
typename Tuple>
250 bool operator()(
const Tuple &a,
const Tuple &b)
const {
251 return f(boost::get<0>(a), boost::get<0>(b));
260 struct String::SparseIndex {
261 void intitialize(
const std::vector<String> &v) {
262 cmph_io_adapter_t *source =
263 cmph_io_struct_vector_adapter((
void*)&v[0],
sizeof(v[0]),
264 0, v[0].bytes(), v.size());
265 cmph_config_t *config = cmph_config_new(source);
266 cmph_config_set_algo(config, CMPH_BDZ);
267 cmph_t *hash = cmph_new(config);
268 std::cout << cmph_packed_size(hash) << std::endl;
269 this->data_.resize(cmph_packed_size(hash));
270 cmph_pack(hash, &this->data_[0]);
273 size_t operator[](
const String &s)
const {
274 return cmph_search_packed((
void*)&this->data_[0],
275 s.data(), s.bytes());
278 std::vector<char> data_;
286 static const size_t K = 2;
288 void initialize(
const std::vector<String> &v) {
290 size_t size[] = { s.count(), s.size() };
291 for (
size_t k = 0, i = 0; k < K; ++k) {
294 while (i < size[k]) {
295 mask_[k] = mask_[k] | (1UL << i);
300 std::reverse(shift_, shift_ + K);
301 std::reverse(mask_, mask_ + K);
303 size_t index[K] = { size_t(-1), size_t(-1) };
304 for (
size_t i = 0; i < v.size(); ++i) {
306 for (
int k = 0; k < K; ++k) {
307 if ((s & mask_[k]) != index[k]) {
308 index[k] = s & mask_[k];
310 for (
int j = 0; j < k; ++j) {
311 *hash(k, s) -= *hash(j, s);
317 for (
size_t i = 0; i < v.size(); ++i) {
318 const String &s = v.at(i);
320 if (this->
operator[](s) == i)
322 std::cout << i <<
" != " << this->operator[](s) << std::endl;
326 size_t operator[](
const String &s)
const {
328 for (
int k = 0; k < K; ++k) {
329 index += *hash(k, s);
334 std::vector<String> data_;
335 typedef size_t bucket[1 << 16];
336 size_t shift_[K], mask_[K];
339 static const size_t N = 1 << 16;
341 data_.resize(N * K, 0);
343 size_t* operator[](
int k) {
344 return &data_[k * N];
346 const size_t* operator[](
int k)
const {
347 return &data_[k * N];
350 std::vector<size_t> data_;
355 size_t* hash(
size_t k,
const String &s)
const {
356 size_t b = s & mask_[k];
357 return (
size_t*) bucket_[k] + (b >> shift_[k]);
362 MPQC_ASSERT(no > ne);
364 std::vector<String> v;
365 std::string r =
String(no, ne);
368 if (N && (String::difference(
String(r),
String(s))) > N)
374 while (std::next_permutation(s.rbegin(), s.rend()));
383 #endif // MPQC_CI_STRING_HPP