int main() { // Create a default vector MyVector sam; // push some data into sam cout << "\nPushing three values into sam"; sam.push_back(TEST_VALUE1); sam.push_back(TEST_VALUE2); sam.push_back(TEST_VALUE3); cout << "\nThe values in sam are: "; // test for out of bounds condition here // and test exception for (int i = 0; i < sam.size() + 1; i++) { try { cout << sam.at(i) << " "; } catch (int badIndex) { cout << "\nOut of bounds at index " << badIndex << endl; } } cout << "\n--------------\n"; // clear sam and display its size and capacity sam.clear(); cout << "\nsam has been cleared."; cout << "\nSam's size is now " << sam.size(); cout << "\nSam's capacity is now " << sam.capacity() << endl; cout << "---------------\n"; // Push 12 values into the vector - it should grow cout << "\nPush 12 values into sam."; for (int i = 0; i < MAX; i++) sam.push_back(i); cout << "\nSam's size is now " << sam.size(); cout << "\nSam's capcacity is now " << sam.capacity() << endl; cout << "---------------\n"; cout << "\nTest to see if contents are correct..."; // display the values in the vector for (int i = 0; i < sam.size(); i++) { cout << sam.at(i) << " "; } cout << "\n--------------\n"; cout << "\n\nTest Complete..."; cout << endl; system("PAUSE"); return 0; }
int main(int argc, char** argv) { named_mutex mutex(open_or_create, "grapevine_collect_files"); scoped_lock<named_mutex> lock(mutex); managed_shared_memory segment(open_or_create, "grapevine_collect_files_memory", 1 << 16); CharAllocator charAllocator (segment.get_segment_manager()); StringAllocator stringAllocator (segment.get_segment_manager()); MyVector *fileList = segment.find_or_construct<MyVector>("file_list")(stringAllocator); time_t* lastUpdate = segment.find_or_construct<time_t>("last_update")(time(NULL)); if (*lastUpdate + 1 < time(NULL)) { fileList->clear(); } for (int iArg = 1; iArg < argc; ++iArg) { MyString str(charAllocator); str = argv[iArg]; fileList->push_back(str); } *lastUpdate = time(NULL); return 0; }
int main(int argc, char * * argv) { typedef std::vector<std::string> MyVector; MyVector myVector; myVector.push_back("foo"); myVector.push_back("bar"); myVector.push_back("baz"); std::for_each(myVector.begin(), myVector.end(), [](const std::string & s) { std::cout << s << std::endl; }); return 0; }
MyVector<double> GetParameters(int k, int n, int** M, double** Par) { MyVector<double> Parameters; Parameters.push_back(Par[k-1][n-1]); if (k>1) { int Prec = M[k-1][n-1]; if (k>2) for (int i=(k-2); i>=1; i--) { Parameters.push_back(Par[i][Prec]); Prec = M[i][Prec]; } Parameters.push_back(Par[0][Prec]); } Parameters.reverse(); return Parameters; }
int main(){ MyVector<string> vec; vec.push_back("CS14"); vec.push_back("World"); vec.insert("Hello", 0); cout << vec.front() << endl; // "Hello" cout << vec.back() << endl; // "World" vec.pop_back(); cout << vec.back() << endl; // "CS14" vec.pop_back(); cout << vec.back() << endl; // "Hello" MyVector<int> another_vec; another_vec.push_back(42); cout << another_vec.front() << endl; //42 return 0; }
MyVector<int> GetBreakpoints(int k, int n, int** M) { MyVector<int> TheBreakpoints; if (k>1) { int Prec = M[k-1][n-1]; TheBreakpoints.push_back(Prec+1); if (k>2) for (int i=(k-2); i>=1; i--) { TheBreakpoints.push_back(M[i][Prec]+1); Prec = M[i][Prec]; } } TheBreakpoints.push_back(0); TheBreakpoints.reverse(); TheBreakpoints.push_back(n); TheBreakpoints.sort(); return TheBreakpoints; }
int main() { MyVector<int> intVector; MyVector<string> stringVector; intVector.push_back(3); intVector.push_back(4); stringVector.push_back("hi"); stringVector.push_back("bye"); for (int i = 0; i < intVector.size(); i++) { cout << intVector[i] << endl; } for (int i = 0; i < stringVector.size(); i++) { cout << stringVector[i] << endl; } return 0; }
int main( ) { MyVector<int> v; v.push_back( 3 ); v.push_back( 2 ); cout << "Vector contents: " << endl; VectorIterator<int> itr = v.getIterator( ); while( itr.hasNext( ) ) cout << itr.next( ) << endl; return 0; }
void testVectorWithTypedef() { // To avoid some of the cumbersome syntax, many programmers use typedefs // to streamline things. So, repeating the previous example, we end up with: typedef std::vector<int> MyVector; MyVector myVector; myVector.push_back(100); myVector.push_back(200); for (MyVector::const_iterator it = myVector.begin(); it != myVector.end(); it++) { std::cout << *it << "\n"; } }
int main( ) { MyVector<int> v; v.push_back( 3 ); v.push_back( 2 ); cout << "Vector contents: " << endl; Iterator<int> *itr = v.getIterator( ); while( itr->hasNext( ) ) cout << itr->next( ) << endl; delete itr; return 0; }
MyVector<int> IntersectLists(const MyVector<int> &A, const MyVector<int> &B) { MyVector<int> Res; MyVector<int>::const_iterator IA = A.begin(), IB = B.begin(); while ((IA != A.end()) && (IB != B.end())) if (*IA < *IB) IA++; else if (*IB < *IA) IB++; else { Res.push_back(*IA); IA++; IB++; } return Res; }
int main(int argc, char *argv[]) { if (argc == 1) { struct shm_remove { shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove() { shared_memory_object::remove("MySharedMemory"); } } remover; // Create a new segment with given name and size managed_shared_memory segment(create_only, "MySharedMemory", 65536); // Initialize shared memory STL-compatible allocator const ShmemAllocator alloc_inst(segment.get_segment_manager()); // Construct a vector named "MyVector" in shared memory with argument alloc_inst MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst); for (int i = 0; i < 100; ++i) { myvector->push_back(i); } // Launch child process std::string s(argv[0]); s += " child "; if (0 != std::system(s.c_str())) return 1; if (segment.find<MyVector>("MyVector").first) return 1; } else { managed_shared_memory segment(open_only, "MySharedMemory"); MyVector *myvector = segment.find<MyVector>("MyVector").first; std::sort(myvector->rbegin(), myvector->rend()); segment.destroy<MyVector>("MyVector"); } return 0; }
int main () { using namespace boost::interprocess; try{ //Shared memory front-end that is able to construct objects //associated with a c-string. Erase previous shared memory with the name //to be used and create the memory segment at the specified address and initialize resources shared_memory_object::remove("MySharedMemory"); managed_shared_memory segment (create_only ,"MySharedMemory" //segment name ,65536); //segment size in bytes //Alias an STL compatible allocator of ints that allocates ints from the managed //shared memory segment. This allocator will allow to place containers //in managed shared memory segments typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator; //Alias a vector that uses the previous STL-like allocator typedef vector<int, ShmemAllocator> MyVector; //Initialize shared memory STL-compatible allocator const ShmemAllocator alloc_inst (segment.get_segment_manager()); //Construct a shared memory MyVector *myvector = segment.construct<MyVector>("MyVector") //object name (alloc_inst);//first ctor parameter //Insert data in the vector for(int i = 0; i < 100; ++i){ myvector->push_back(i); } } catch(...){ shared_memory_object::remove("MySharedMemory"); throw; } shared_memory_object::remove("MySharedMemory"); return 0; }
int main(int argc, char *argv[]) { if(argc == 1){ 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", 65536); const ShmemAllocator alloc_inst (segment.get_segment_manager()); MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst); for(int i = 0; i < 100; ++i) //Insert data in the vector myvector->push_back(i); std::string s(argv[0]); s += " child "; if(0 != std::system(s.c_str())) return 1; if(segment.find<MyVector>("MyVector").first) return 1; } else{ managed_shared_memory segment(open_only, "MySharedMemory"); MyVector *myvector = segment.find<MyVector>("MyVector").first; std::sort(myvector->rbegin(), myvector->rend()); for(int i = 0; i < 100; ++i) //Insert data in the vector { printf("%d ", myvector->at(i)); } segment.destroy<MyVector>("MyVector"); } return 0; };
//Main function. For parent process argc == 1, for child process argc == 2 __declspec(dllexport) int host(char * sharedMemName, char * properVectorName)//int argc, char *argv[]) { //if (argc == 1) { //Parent process //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;*/ shared_memory_object::remove(sharedMemName); //Create a new segment with given name and size managed_shared_memory segment(create_only, sharedMemName, 65536); //Initialize shared memory STL-compatible allocator const ShmemAllocator alloc_inst(segment.get_segment_manager()); //Construct a vector named "MyVector" in shared memory with argument alloc_inst MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst); // for (int i = 0; i < 100; ++i) //Insert data in the vector // myvector->push_back(i); myvector->push_back(std::string("hi there")); //Launch child process //std::string s(argv[0]); s += " child "; //if (0 != std::system(s.c_str())) // return 1; printf("needfull\n"); //Check child has destroyed the vector //if (segment.find<MyVector>("MyVector").first) //return 1; //} // int c; // std::cin >> c; return 0; };
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; }
//Main function. For parent process argc == 1, for child process argc == 2 int main(int argc, char *argv[]) { if(argc == 1){ //Parent process //Remove shared memory on construction and destruction struct shm_remove { //<- #if 1 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } #else //-> shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } //<- #endif //-> } remover; //<- (void)remover; //-> //Create a new segment with given name and size //<- #if 1 managed_shared_memory segment(create_only, test::get_process_id_name(), 65536); #else //-> managed_shared_memory segment(create_only, "MySharedMemory", 65536); //<- #endif //-> //Initialize shared memory STL-compatible allocator const ShmemAllocator alloc_inst (segment.get_segment_manager()); //Construct a vector named "MyVector" in shared memory with argument alloc_inst MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst); for(int i = 0; i < 100; ++i) //Insert data in the vector myvector->push_back(i); //Launch child process std::string s(argv[0]); s += " child "; //<- s += test::get_process_id_name(); //-> if(0 != std::system(s.c_str())) return 1; //Check child has destroyed the vector if(segment.find<MyVector>("MyVector").first) return 1; } else{ //Child process //Open the managed segment //<- #if 1 managed_shared_memory segment(open_only, argv[2]); #else //-> managed_shared_memory segment(open_only, "MySharedMemory"); //<- #endif //-> //Find the vector using the c-string name MyVector *myvector = segment.find<MyVector>("MyVector").first; //Use vector in reverse order std::sort(myvector->rbegin(), myvector->rend()); //When done, destroy the vector from the segment segment.destroy<MyVector>("MyVector"); } return 0; };
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; }
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; }