Sign in to follow this  

Returning by value from datastructure

This topic is 2657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey.

Lets assume language C++.

We have a generic datastructure Tree< T, R >, that accepts any kind of data for its leaves ( primitives, objects, structs, pointers... ) and accepts any primitive as key ( float, int, long, bool... )


The datastructure would implement two public methods:

void insert( T payLoad, R key );
T fetch( R key );

This would seem to be a nice solution, but what the heck are you supposed to do if you try and query the datastructure for key that has no payload associated with it?

The function prototype promises a return value, and even that is by value, so just throwing NULL is not an option. For generic Stacks and Queues, it would simply be a matter of "peeking" whether the thing was empty, but with a Tree, you would be forced to do something like

if(Exists(R)){ T t = Fetch(R);}

Traversing the tree twice for every query is not exactly delightful though for me.

Alternatively I could throw an exception on "not found" event, but that be mixing exception handling code with regular program flow, so thats a no-go too.

Another solution I have thought would involve setting up a dummy object on stack along with a boolean variable. On call, pointers to both would be passed to the Tree, and on succesful query, the bool would be set to true through the pointer to signal meaningful data content on the other variable. Naturally, this won't be too elegant or even possible with complex class hierarchies.

I would hate to be forced to return pointers, since the generic nature of the datastructure would suffer. ( For example: pointer to byte is just waste in this context ).

Share this post


Link to post
Share on other sites
Quote:
Original post by rmed002
Lets assume language C++.

We have a generic datastructure Tree< T, R >, that accepts any kind of data for its leaves ( primitives, objects, structs, pointers... ) and accepts any primitive as key ( float, int, long, bool... )


Sounds like std::map. All your other questions are answered there.

Share this post


Link to post
Share on other sites
It does seem like I have little choice in the matter.

I was hoping to bypass the need for "additional" components and deal with the entries directly. Apparently its not that easy or practical afterall.

Then again, In any serious application I would be better off using STL anyways, so my little excercise is becoming more and more pointless by the minute. ^^

Curious regardless and thanks for help. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by rmed002
It does seem like I have little choice in the matter.

I was hoping to bypass the need for "additional" components and deal with the entries directly. Apparently its not that easy or practical afterall.


See std::map::operator[] !

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
Quote:
Original post by rmed002
It does seem like I have little choice in the matter.

I was hoping to bypass the need for "additional" components and deal with the entries directly. Apparently its not that easy or practical afterall.


See std::map::operator[] !


I wish std::map provided operator[]! rather than plain old operator [] [grin]

Share this post


Link to post
Share on other sites

This topic is 2657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this