Boost logo

Boost Users :

Subject: Re: [Boost-users] Filesystem: Choose random file from directory
From: Charles Mills (charlesm_at_[hidden])
Date: 2012-06-23 17:50:40


How often do you have to do this? Does it truly have to be random?

Does the list of files change? Could you build a list once and index into
it? Can you build a list of partial names, index into that randomly,
retrieve all of the names that match it, and then pick a random member of
that set? Could you generate a random three-character mask, use that as a
wild card, and pick one of the matching files at random, repeating if none
match?

Are they all in one folder?

Charles

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Lars Viklund
Sent: Saturday, June 23, 2012 2:35 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Filesystem: Choose random file from directory

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 mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

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