random-access

Python Random Access File

别来无恙 提交于 2019-11-27 08:55:54
Is there a Python file type for accessing random lines without traversing the whole file? I need to search within a large file, reading the whole thing into memory wouldn't be possible. Any types or methods would be appreciated. This seems like just the sort of thing mmap was designed for. A mmap object creates a string-like interface to a file: >>> f = open("bonnie.txt", "wb") >>> f.write("My Bonnie lies over the ocean.") >>> f.close() >>> f.open("bonnie.txt", "r+b") >>> mm = mmap(f.fileno(), 0) >>> print mm[3:9] Bonnie In case you were wondering, mmap objects can also be assigned to: >>>

Buffered RandomAccessFile java

这一生的挚爱 提交于 2019-11-27 07:00:33
RandomAccessFile is quite slow for random access to a file. You often read about implementing a buffered layer over it, but code doing this isn't possible to find online. So my question is: would you guys who know any opensource implementation of this class share a pointer or share your own implementation? It would be nice if this question would turn out as a collection of useful links and code about this problem, which I'm sure, is shared by many and never addressed properly by SUN. Please, no reference to MemoryMapping, as files can be way bigger than Integer.MAX_VALUE. Edwin Dalorzo Well, I

.NET C# - Random access in text files - no easy way?

≡放荡痞女 提交于 2019-11-27 05:40:49
问题 I've got a text file that contains several 'records' inside of it. Each record contains a name and a collection of numbers as data. I'm trying to build a class that will read through the file, present only the names of all the records, and then allow the user to select which record data he/she wants. The first time I go through the file, I only read header names, but I can keep track of the 'position' in the file where the header is. I need random access to the text file to seek to the

STL deque accessing by index is O(1)?

别等时光非礼了梦想. 提交于 2019-11-27 02:30:25
问题 I've read that accessing elements by position index can be done in constant time in a STL deque. As far as I know, elements in a deque may be stored in several non-contiguous locations, eliminating safe access through pointer arithmetic. For example: abc->defghi->jkl->mnop The elements of the deque above consists of a single character. The set of characters in one group indicate it is allocated in contiguous memory (e.g. abc is in a single block of memory, defhi is located in another block of

Why does Collections.shuffle() fail for my array?

冷暖自知 提交于 2019-11-26 22:53:30
问题 Why does my code not work? package generatingInitialPopulation; import java.util.Arrays; import java.util.Collections; public class TestShuffle { public static void main(String[] args) { int[] arr = new int[10]; for (int i = 0; i < arr.length; i++) { arr[i] = i; } Collections.shuffle(Arrays.asList(arr)); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } The result is: 0 1 2 3 4 5 6 7 8 9. I was expecting a randomly shuffled sequence . 回答1: Arrays.asList() can't be

SQLite - ORDER BY RAND()

眉间皱痕 提交于 2019-11-26 18:43:59
In MySQL I can use the RAND() function, is there any alternative in SQLite 3? dfa using random() : SELECT foo FROM bar WHERE id >= (abs(random()) % (SELECT max(id) FROM bar)) LIMIT 1; EDIT (by QOP): Since the docs on SQLite Autoincrement ed columns states that: The normal ROWID selection algorithm described above will generate monotonically increasing unique ROWIDs as long as you never use the maximum ROWID value and you never delete the entry in the table with the largest ROWID. If you ever delete rows, then ROWIDs from previously deleted rows might be reused when creating new rows . The

Using StAX to create index for XML for quick access

夙愿已清 提交于 2019-11-26 14:25:41
问题 Is there a way to use StAX and JAX-B to create an index and then get quick access to an XML file? I have a large XML file and I need to find information in it. This is used in a desktop application and so it should work on systems with few RAM. So my idea is this: Create an index and then quickly access data from the large file. I can't just split the file because it's an official federal database that I want to use unaltered. Using a XMLStreamReader I can quickly find some element and then

Python Random Access File

跟風遠走 提交于 2019-11-26 14:23:09
问题 Is there a Python file type for accessing random lines without traversing the whole file? I need to search within a large file, reading the whole thing into memory wouldn't be possible. Any types or methods would be appreciated. 回答1: This seems like just the sort of thing mmap was designed for. A mmap object creates a string-like interface to a file: >>> f = open("bonnie.txt", "wb") >>> f.write("My Bonnie lies over the ocean.") >>> f.close() >>> f.open("bonnie.txt", "r+b") >>> mm = mmap(f

quick random row selection in Postgres

爱⌒轻易说出口 提交于 2019-11-26 12:51:01
I have a table in postgres that contains couple of millions of rows. I have checked on the internet and I found the following SELECT myid FROM mytable ORDER BY RANDOM() LIMIT 1; it works, but it's really slow... is there another way to make that query, or a direct way to select a random row without reading all the table? by the way 'myid' is an integer but it can be an empty field. thanks NPE You might want to experiment with OFFSET , as in SELECT myid FROM mytable OFFSET floor(random()*N) LIMIT 1; The N is the number of rows in mytable . You may need to first do a SELECT COUNT(*) to figure

How to insert characters to a file using C#

旧巷老猫 提交于 2019-11-26 09:56:54
问题 I have a huge file, where I have to insert certain characters at a specific location. What is the easiest way to do that in C# without rewriting the whole file again. 回答1: Filesystems do not support "inserting" data in the middle of a file. If you really have a need for a file that can be written to in a sorted kind of way, I suggest you look into using an embedded database. You might want to take a look at SQLite or BerkeleyDB. Then again, you might be working with a text file or a legacy