random-access

boost::multi_index_container with random_access and ordered_unique

帅比萌擦擦* 提交于 2019-12-11 03:10:11
问题 I have a problem getting boost::multi_index_container work with random-access and with orderd_unique at the same time. (I'm sorry for the lengthly question, but I think I should use an example..) Here an example: Suppose I want to produce N objects in a factory and for each object I have a demand to fulfill (this demand is known at creation of the multi-index). Well, within my algorithm I get intermediate results, which I store in the following class: class intermediate_result { private: std:

C++ nested iterators

百般思念 提交于 2019-12-10 10:45:33
问题 Is it OK to have a nested iterator like the following? for (vector<type>::iterator i = list.begin(); i != list.end(); ++i) { for (vector<type>::iterator j = i; j != list.end(); ++j) { ... } } Note that j starts at i , and not list.begin() . Since the iterator is random access, can I guarantee that both i and j will have the same order? is there a better way of doing this? 回答1: Your code is correct. Both iterators will have the same order and incrementing j doesn't affect i as long as you don

Random access (or otherwise fast access) of edges in boost graph library

泄露秘密 提交于 2019-12-09 23:14:23
问题 I want to run Monte Carlo edge swaps, i.e., picking two existing edges in a graph uniformly at random and then (if some conditions are met) swap their end points. I am currently using boost::random_edge for selecting edges uniformly at random. #include <boost/graph/adjacency_list.hpp> #include <boost/graph/erdos_renyi_generator.hpp> #include <boost/random/mersenne_twister.hpp> #include <boost/random/variate_generator.hpp> #include <boost/graph/random.hpp> #include <boost/random/linear

Very fast sampling from a set with fixed number of elements in python

倖福魔咒の 提交于 2019-12-08 16:53:33
问题 I need to sample uniformly at random a number from a set with fixed size, do some calculation, and put the new number back into the set. (The number samples needed is very large) I've tried to store the numbers in a list and use random.choice() to pick an element, remove it, and then append the new element. But that's way too slow! I'm thinking to store the numbers in a numpy array, sample a list of indices, and for each index perform the calculation. Are there any faster way of doing this

Rails - Show A Random Record But Not The Current

◇◆丶佛笑我妖孽 提交于 2019-12-08 02:38:06
问题 At the end of a blog post I would like to display a teaser link to another random post. Obviously this link should be NOT a teaser for the current post itself. I figured already out how to choose a random post, but there is still the chance that the teaser link is a link to the current post. I couldn't figure out, how can I exclude this case. Here is my controller: def show @post = Post.find(params[:id]) @staff_pickrandom = Post.where(staff_pick:true).order("RANDOM()").limit(1) end I am quite

About the speed of random file read (Python)

房东的猫 提交于 2019-12-08 02:16:56
问题 Please take a look at the following code (kind of pseudo code): index = db.open() fh = open('somefile.txt','rb') for i in range(1000): x = random_integer(1,5000) pos,length = index[x] fh.seek(pos) buffer = fh.read(length) doSomeThingWith(buffer) fh.close() db.close() I used a database to index the positions and lengths of text segments in a .txt file for random retrieval. No wonder, if the above code is run repeatedly, the execution takes less and less time. 1) What is responsible for this

Does FileInputStream.skip() do a seek?

ε祈祈猫儿з 提交于 2019-12-06 18:36:21
问题 I want to copy the last 10MB of a possibly large file into another file. Ideally I would use FileInputStream, skip() and then read(). However I'm unsure if the performance of skip() will be bad. Is skip() typically implemented using a file seek underneath or does it actually read and discard data? I know about RandomAccessFile but I'm interested in whether I could use FileInputStream in place of that (RandomAccessFile is annoying as the API is non-standard). 回答1: Depends on your JVM, but here

About the speed of random file read (Python)

只愿长相守 提交于 2019-12-06 15:53:42
Please take a look at the following code (kind of pseudo code): index = db.open() fh = open('somefile.txt','rb') for i in range(1000): x = random_integer(1,5000) pos,length = index[x] fh.seek(pos) buffer = fh.read(length) doSomeThingWith(buffer) fh.close() db.close() I used a database to index the positions and lengths of text segments in a .txt file for random retrieval. No wonder, if the above code is run repeatedly, the execution takes less and less time. 1) What is responsible for this speed-up? Is it because of things staying in the memory or the "caching" or something? 2) Is there anyway

Rails - Show A Random Record But Not The Current

社会主义新天地 提交于 2019-12-06 11:48:06
At the end of a blog post I would like to display a teaser link to another random post. Obviously this link should be NOT a teaser for the current post itself. I figured already out how to choose a random post, but there is still the chance that the teaser link is a link to the current post. I couldn't figure out, how can I exclude this case. Here is my controller: def show @post = Post.find(params[:id]) @staff_pickrandom = Post.where(staff_pick:true).order("RANDOM()").limit(1) end I am quite new to Rails and I am still struggling with basic stuff. Sorry, if this is an easy one. Thank you so

C++ nested iterators

安稳与你 提交于 2019-12-06 09:40:16
Is it OK to have a nested iterator like the following? for (vector<type>::iterator i = list.begin(); i != list.end(); ++i) { for (vector<type>::iterator j = i; j != list.end(); ++j) { ... } } Note that j starts at i , and not list.begin() . Since the iterator is random access, can I guarantee that both i and j will have the same order? is there a better way of doing this? Rafał Rawicki Your code is correct. Both iterators will have the same order and incrementing j doesn't affect i as long as you don't make any operation that invalidates iterators (for example erasing from or pushing to vector