473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

STL tree implementation -directory structure

From searching around, Ive seen this question asked numerous times, but
havent seen a usable answer unfortuantly.

What Im trying to do is something I would have thought would be quite
common, but there just seems to be no information on the topic that I can
find. Basically I need to store a directory structure in memory, from a flat
list of files and paths. ie given:

C:\dir\dir1\abc.txt
C:\dir\toop.txt
C:\temp\acd\test.txt

etc I need to create a directory structure, and represent this in a
tree-view control and give it basic 'explorer' style functionality. I think
I would need to use either map or hash_map, which itself contains a link to
another map. I have started with the following:

typedef stdex::hash_map<std::string, hm> hm;

Obviously thats not really possible, so instead I have:

typedef stdex::hash_map<std::string, void*> hm;

And just use NULL if there are no children, or create a "new" hm if there
are children. The std::string is the full path. I havent really tried this
fully, but I would suspect it would work. For holding the files, I would
probably just use a pair instead, which has both a std::list and a void* to
another hash_map/list.

While I think this would probably work, I get the feeling I am reinventing
the wheel here, yet I havent seen anything like this anywhere else. Which
gives me the impression I am probably doing something wrong :) Surely there
is an example for holding a hierarchial file directory type structure in
memory somewhere around? All Ive seen is overly complex amateur
implementations of fully fledged tree templates. Can someone give me a hand
with this please ?

Cheers,
Shane
Jul 23 '05 #1
4 9218
On 2005-03-20, shane <in*****@hotmail.com> wrote:
From searching around, Ive seen this question asked numerous times, but
havent seen a usable answer unfortuantly.

What Im trying to do is something I would have thought would be quite
common, but there just seems to be no information on the topic that I can
find. Basically I need to store a directory structure in memory, from a flat
list of files and paths. ie given:

C:\dir\dir1\abc.txt
C:\dir\toop.txt
C:\temp\acd\test.txt

etc I need to create a directory structure, and represent this in a
tree-view control and give it basic 'explorer' style functionality. I think
I would need to use either map or hash_map, which itself contains a link to
another map. I have started with the following:

typedef stdex::hash_map<std::string, hm> hm;

Obviously thats not really possible, so instead I have:

typedef stdex::hash_map<std::string, void*> hm;


So you're trying to get around the "infinite recursion" issue, but you have
another problem also -- managing those pointers. As is, you don't have a
class that manages your memory.

Both these problems really call for a wrapper class you can make the
template parameter a pointer to your wrapper (you would need a wrapper to
make the class interface typesafe anyway), so the layer of indirection
takes out the self reference.

But since you can put type information in that pointer, you can make it
a smart pointer instead, so you can get around memory management problems.

class X {
typedef stdex::map<std::string, some_smart_pointer<X> > hm;
hm themap;
public:
// put some wrappers for your member functions here
};

depending on the details it might be convenient to use private inheritance.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2
>>
typedef stdex::hash_map<std::string, hm> hm;

Obviously thats not really possible, so instead I have:

typedef stdex::hash_map<std::string, void*> hm;


So you're trying to get around the "infinite recursion" issue, but you
have
another problem also -- managing those pointers. As is, you don't have a
class that manages your memory.

Both these problems really call for a wrapper class you can make the
template parameter a pointer to your wrapper (you would need a wrapper to
make the class interface typesafe anyway), so the layer of indirection
takes out the self reference.

But since you can put type information in that pointer, you can make it
a smart pointer instead, so you can get around memory management problems.

class X {
typedef stdex::map<std::string, some_smart_pointer<X> > hm;
hm themap;
public:
// put some wrappers for your member functions here
};

depending on the details it might be convenient to use private
inheritance.


Thanks for the info. Theres a possibility I may not even need to use the
tree structure, I still havent finalised the implementation details.

What I found was the following code would compile and run ok:

extern class treeitemsx;
typedef std::map<std::string, treeitemsx> mtypex;
class treeitemsx {
public:
mtypex mtree;
} mbase;
typedef std::pair<std::string, treeitemsx> Map_Pair;

Note the lack of a *. This seems like it shouldnt run at all. ie, as soon as
you insert a value into the mtree, it should run out of stack space.
However, the map object doesnt try and create a treeitemsx object until you
do an insertion so it should be ok. I dont explcitly set the map_pair.second
value when I do an insertion, but I guess it gets created anyway. ie

Map_Pair x;
x.first="string";
mbase.mtree.insert(x);

Would this kind of code be dangerous? Im thinking about putting a bool in
the class to indicate whether there are children or not, but I guess I could
just check if mtree is empty. I suppose the disadvantage of using this over
a pointer is it has a child class with a map object even if its not
necessary (ie in the case of no children). Whereas with a pointer scheme you
can just use NULL.

However, would this scheme manage memory ok? if mbase went out of scope,
would all the nested mtree's be freed?
Jul 23 '05 #3
On 2005-03-20, shane <in*****@hotmail.com> wrote:
Thanks for the info. Theres a possibility I may not even need to use the
tree structure, I still havent finalised the implementation details.

What I found was the following code would compile and run ok:

extern class treeitemsx;
typedef std::map<std::string, treeitemsx> mtypex;
class treeitemsx {
public:
mtypex mtree;
} mbase;
typedef std::pair<std::string, treeitemsx> Map_Pair;

Note the lack of a *. This seems like it shouldnt run at all. ie, as soon as
you insert a value into the mtree, it should run out of stack space.
However, the map object doesnt try and create a treeitemsx object until you
do an insertion so it should be ok.
I think it works for the same reason as my solution. It doesn't run out of
stack space because the map doesn't really contain an item of type treeitemsx,
it allocates them dynamically. To put it another way, it contains pointers to
the free store where more maps may be created. So you get a similar indirection
to what you have in my solution.
Would this kind of code be dangerous? Im thinking about putting a bool in
the class to indicate whether there are children or not, but I guess I could
just check if mtree is empty.
First, the fact that you want to be able to check whether the tree is empty
or not suggests that you should make a member function that does that.

bool empty () const { return mtree.empty(); }

When you have a choice between a member function and a data member, it's
good practice to put a member function in regardless (perhaps make it an
accessor)

Generally, it's good practice to avoid having redundant members in a class,
because it makes class invariants more complicated. For example, in this
case, any function that adds to or erases from mtree would need to check
whether mtree was empty and update the data member. In this instance, it
doesn't really help much to add the data member (if the computation were
very expensive, that would be an argument for having the data member).
I suppose the disadvantage of using this over
a pointer is it has a child class with a map object even if its not
necessary (ie in the case of no children). Whereas with a pointer scheme you
can just use NULL.
I worked out what you mean ... but your usage of "child class" in this
context is incorrect.
However, would this scheme manage memory ok? if mbase went out of scope,
would all the nested mtree's be freed?


Yes, it does what you want it to do.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #4
shane wrote:
From searching around, Ive seen this question asked numerous times, but
havent seen a usable answer unfortuantly.

What Im trying to do is something I would have thought would be quite
common, but there just seems to be no information on the topic that I can
find. Basically I need to store a directory structure in memory, from a flat
list of files and paths. ie given:

C:\dir\dir1\abc.txt
C:\dir\toop.txt
C:\temp\acd\test.txt


I think what you have is structurally a tree. But what you may want to
look at to represent it is a "composite" pattern. That is, a
directory-entry object may contain file entries (eg. names, paths, other
file stats) and other directory entries. (eg. subdirectories)

As such, the directory entry objects will build up their own N-way tree,
and internally you can use one or more STL data structures (eg. vector
or list) to maintain the contents.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: FISH | last post by:
Ever have one of those days when you're not sure if it's you who's gone mad, or the rest of the world? I have an Open Source project on SourceForge for communication with YSMG - Yahoo's IM...
8
by: Jay Donnell | last post by:
Is there a way to use the tarfile module to recursively compress the contents of a directory and maintain the directory structure in the tar archive? Simply doing os.system('tar -czvf ' +...
2
by: Jignesh | last post by:
Can someone please tell me how to "import" a directory structure into a project in Microsoft Visual C++ .NET 2003. I have an extremeley complex directory structure consisting of thousands of...
8
by: James Owens | last post by:
I'm a relative newbie, interested in storing the information from several server directories and subdirectories in XML so that I can present it selectively using XSL (all files updated today or...
1
by: soni29 | last post by:
when creating a project with namespace like: wrox.csharp.basics.overflowtest is it good practice to have the directory structure like that also: c:\wrox\csharp\basics\overflowtext.cs Also if it...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
2
by: SharepointKida | last post by:
Hi I am building an web based .NET Application which allows users to specify a directory location from it's machine, and when it press publish it uploads all the files and subfolders folders from...
11
by: Micah | last post by:
I'm looking for a simple tree implementation: 0-n children, 1 root. All the nice methods would be appreciated (getLeaves, isLeaf, isRoot, depthfirst, breadthfirst,...) That's really all I need. I...
17
by: Jason Doucette | last post by:
I am converting a C-style unit into a C++ class. I have an implementation function that was defined in the .cpp file (so it was hidden from the interface that exists in the .h file). It uses a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.