Boost logo

Boost Users :

Subject: Re: [Boost-users] Filesystem: Choose random file from directory
From: Lars Viklund (zao_at_[hidden])
Date: 2012-06-23 17:35:21


On Sat, Jun 23, 2012 at 12:27:39PM -0700, Charles Mills wrote:
> Generate a random number 'n' between 1 and 500K and then take the nth file
> from the directory.

I would say that the core question is probably, how do I get a cheap
indexed lookup in a directory.

You've got to consider that directories, much like SQL tables, have no
real innate sort order in most filesystems. All you have is the order
given by the OS when doing an opendir/FindFirstFile, unless you use some
ordered access method.

You can't really ask through some interface that you want the Nth file,
just that you want to iterate through the contents to get an implicit
sequence of files.

You could do a linear lookup to accumulate the paths into a container
and select from that, that would be the cheap solution, but the time
used may be rather high, particularly for such a degenerate case with
uncommonly many files.

-- 
Lars Viklund | zao_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net