std::string count() { MessageHandler mh; mh.begin("counting") << " paths of " << typenameof(spec); std::vector<Word> tmp(stateWords + 1); Word* ptmp = tmp.data(); int const n = spec.get_root(state(ptmp)); if (n <= 0) { mh << " ..."; mh.end(0); return (n == 0) ? "0" : "1"; } uint64_t totalStorage[n / 63 + 1]; BigNumber total(totalStorage); total.store(0); size_t maxWidth = 0; //std::cerr << "\nLevel,Width\n"; std::vector<MemoryPool> pools(n + 1); MyVector<MyList<Word> > vnodeTable(n + 1); MyVector<UniqTable> uniqTable; uniqTable.reserve(n + 1); for (int i = 0; i <= n; ++i) { uniqTable.push_back(UniqTable(hasher, hasher)); } int numberWords = 1; Word* p0 = vnodeTable[n].alloc_front(stateWords + 1); spec.get_copy(state(p0), state(ptmp)); spec.destruct(state(ptmp)); number(p0).store(1); mh.setSteps(n); for (int i = n; i > 0; --i) { MyList<Word>& vnodes = vnodeTable[i]; size_t m = vnodes.size(); //std::cerr << i << "," << m << "\n"; maxWidth = std::max(maxWidth, m); MyList<Word>& nextVnodes = vnodeTable[i - 1]; UniqTable& nextUniq = uniqTable[i - 1]; int const nextWords = stateWords + numberWords + 1; Word* pp = nextVnodes.alloc_front(nextWords); //if (nextUniq.size() < m) nextUniq.rehash(m); for (; !vnodes.empty(); vnodes.pop_front()) { Word* p = vnodes.front(); if (number(p).equals(0)) { spec.destruct(state(p)); continue; } for (int b = 0; b <= 1; ++b) { spec.get_copy(state(pp), state(p)); int ii = spec.get_child(state(pp), i, b); if (ii <= 0) { spec.destruct(state(pp)); if (ii != 0) { total.add(number(p)); } } else if (ii < i - 1) { Word* qq = vnodeTable[ii].alloc_front( nextWords + (i - ii) / 63); spec.get_copy(state(qq), state(pp)); spec.destruct(state(pp)); Word* qqq = uniqTable[ii].add(qq); if (qqq == qq) { number(qqq).store(number(p)); } else { spec.destruct(state(qq)); int w = number(qqq).add(number(p)); if (numberWords < w) { numberWords = w; //FIXME might be broken at long skip } vnodeTable[ii].pop_front(); } } else { assert(ii == i - 1); Word* ppp = nextUniq.add(pp); if (ppp == pp) { number(ppp).store(number(p)); pp = nextVnodes.alloc_front(nextWords); } else { spec.destruct(state(pp)); int w = number(ppp).add(number(p)); if (numberWords < w) { numberWords = w; //FIXME might be broken at long skip } } } } spec.destruct(state(p)); } nextVnodes.pop_front(); nextUniq.clear(); pools[i].clear(); spec.destructLevel(i); mh.step(); } mh.end(maxWidth); return total; }
int main () { //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } } remover; managed_shared_memory segment( create_only, "MySharedMemory", //segment name 65536); //segment size in bytes //Construct shared memory vector MyVector *myvector = segment.construct<MyVector>("MyVector") (IntAllocator(segment.get_segment_manager())); //Fill vector myvector->reserve(100); for(int i = 0; i < 100; ++i){ myvector->push_back(i); } //Create the vectorstream. To create the internal shared memory //basic_string we need to pass the shared memory allocator as //a constructor argument MyVectorStream myvectorstream(CharAllocator(segment.get_segment_manager())); //Reserve the internal string myvectorstream.reserve(100*5); //Write all vector elements as text in the internal string //Data will be directly written in shared memory, because //internal string's allocator is a shared memory allocator for(std::size_t i = 0, max = myvector->size(); i < max; ++i){ myvectorstream << (*myvector)[i] << std::endl; } //Auxiliary vector to compare original data MyVector *myvector2 = segment.construct<MyVector>("MyVector2") (IntAllocator(segment.get_segment_manager())); //Avoid reallocations myvector2->reserve(100); //Extract all values from the internal //string directly to a shared memory vector. std::istream_iterator<int> it(myvectorstream), itend; std::copy(it, itend, std::back_inserter(*myvector2)); //Compare vectors assert(std::equal(myvector->begin(), myvector->end(), myvector2->begin())); //Create a copy of the internal string MyString stringcopy (myvectorstream.vector()); //Now we create a new empty shared memory string... MyString *mystring = segment.construct<MyString>("MyString") (CharAllocator(segment.get_segment_manager())); //...and we swap vectorstream's internal string //with the new one: after this statement mystring //will be the owner of the formatted data. //No reallocations, no data copies myvectorstream.swap_vector(*mystring); //Let's compare both strings assert(stringcopy == *mystring); //Done, destroy and delete vectors and string from the segment segment.destroy_ptr(myvector2); segment.destroy_ptr(myvector); segment.destroy_ptr(mystring); return 0; }
uint64_t count64() { MessageHandler mh; mh.begin("counting") << " paths of " << typenameof(spec); std::vector<Word> tmp(stateWords + 1); Word* ptmp = tmp.data(); int const n = spec.get_root(state(ptmp)); if (n <= 0) { mh << " ..."; mh.end(0); return (n == 0) ? 0 : 1; } mh << "\n"; uint64_t total = 0; size_t maxWidth = 0; //std::cerr << "\nLevel,Width\n"; std::vector<MemoryPool> pools(n + 1); MyVector<MyList<Word> > vnodeTable(n + 1); MyVector<UniqTable> uniqTable; uniqTable.reserve(n + 1); for (int i = 0; i <= n; ++i) { uniqTable.push_back(UniqTable(hasher, hasher)); } Word* p0 = vnodeTable[n].alloc_front(stateWords + 1); spec.get_copy(state(p0), state(ptmp)); spec.destruct(state(ptmp)); number64(p0) = 1; for (int i = n; i > 0; --i) { MyList<Word>& vnodes = vnodeTable[i]; size_t m = vnodes.size(); //std::cerr << i << "," << m << "\n"; maxWidth = std::max(maxWidth, m); MyList<Word>& nextVnodes = vnodeTable[i - 1]; UniqTable& nextUniq = uniqTable[i - 1]; Word* pp = nextVnodes.alloc_front(stateWords + 1); //if (nextUniq.size() < m) nextUniq.rehash(m); for (; !vnodes.empty(); vnodes.pop_front()) { Word* p = vnodes.front(); if (number64(p) == 0) { spec.destruct(state(p)); continue; } for (int b = 0; b <= 1; ++b) { spec.get_copy(state(pp), state(p)); int ii = spec.get_child(state(pp), i, b); if (ii <= 0) { spec.destruct(state(pp)); if (ii != 0) { total += number64(p); } } else if (ii < i - 1) { Word* qq = vnodeTable[ii].alloc_front(stateWords + 1); spec.get_copy(state(qq), state(pp)); spec.destruct(state(pp)); Word* qqq = uniqTable[ii].add(qq); if (qqq == qq) { number64(qqq) = number64(p); } else { spec.destruct(state(qq)); number64(qqq) += number64(p); vnodeTable[ii].pop_front(); } } else { assert(ii == i - 1); Word* ppp = nextUniq.add(pp); if (ppp == pp) { number64(ppp) = number64(p); pp = nextVnodes.alloc_front(stateWords + 1); } else { spec.destruct(state(pp)); number64(ppp) += number64(p); } } } spec.destruct(state(p)); } nextVnodes.pop_front(); nextUniq.clear(); pools[i].clear(); spec.destructLevel(i); mh << "."; } mh.end(maxWidth); return total; }