So I want to store pairs string-path <-> my_file_object
in such container with expected total object size 100 000.
- I need to have search by absolute path to get an object for example I could search for
a/b/c/file.h
) - I need to be capable to list all item names in
folder
like foldera/b/c/
contains filesfile.h
,file.cpp
, folderbin/
and folderbin2/
- I need to be capable to add new
files
by absolute path, and update them. - I need to be capable to del开发者_如何学运维ete
files
.
I wonder about fiew things - is there a container for such things in STL/boost? Is this implementation threadsafe for read?
Also I wonder if it is any way faster than have all my pairs inside of std::map<string, object>
so I would have fast search, fast insert and delete, and folder files listing via code like this?
What requirements do you have that rule out std::map
? As far as I can see it will work well here:
typedef std::map<std::string,file_object_t> paths_t;
paths_t paths;
//I need to have search by absolute path
paths_t::iterator fileIterator = paths.find("a/b/c/file.h");
if( fileIterator != paths.end() ) {
...
}
//I need to be capable to list all item names in folder like folder a/b/c/
std::string folder = "a/b/c/";
paths_t::iterator folderBegin = paths.upper_bound(folder );
paths_t::iterator folderEnd = paths.upper_bound(folder+MAX_CHAR);
std::for_each(folderBegin,folderEnd,...);
//I need to be capable to add new files by absolute path
if( !paths.insert(paths_t::value_type("a/b/c/file.h",file)).second ) {
//add file failed
}
//and update them.
paths["a/b/c/file.h"].some_method_that_updates_file();
//I need to be capable to delete files
paths.erase(fileIterator);
If you want a tree-like structure, use the Boost Graph Library.
If you want a file system abstraction (with access to the file system in question, not some snapshot in memory you seem to be describing), use Boost.Filesystem
精彩评论