Preventing copying of structures - with recursive functions - c++

a very simplistic (and silly) abstraction of what I'm doing is described below:
class A{
private:
template <typename InIt>
A foo(InIt begin, InIt end, A& a) {
// {begin, ind} is a datastructure containing all "terms" to search for.
auto iter(sub.begin());
auto e(sub.end());
// search trough all elements in original structure.
do
if (FUNC) {
if (++begin != end) {
return iter->foo(begin, end, a.append_values(iter));
//append_values appends a copy of the element's values at iter
//does not copy the sub "trees" of the element at "iter"
//it returns a reference to the appended sub "tree"
} else {
return a;
}
}
} while (++iter != e);
return a;
}
};
Sub is a vector containing objects of class "A" - so effectivelly creating a tree-datastructure. FUNC is a function that must be true for the branch to be "added" to the new tree.
What I'm wondering is: how many copys are "created" if, say, the depth (difference between initial begin, end) is "X". - I'm fearing that for each depth a new copy of "a" is created. This is something I wish to prevent. So should I return by reference? - Or by pointer?

UncleBens indeed gave the correct question I needed to find a "solution".
I completely "forgot" you can obviously use arguments-by-reference as output too. I don't have to return anything as I edit the original "a", not a copy.

Related

Intersection and Union of two sorted lists (return type)

This code is for finding the intersection and union of two sorted lists. The sorted list in inherited from a list class with all the basic functions. The main question is what is the return type of the function.
Is it a pointer to a list or the list itself? How would i display the contents of that "pointer".
template <typename Object>
class sorted_list : public List<Object>{
friend sorted_list<Object>*& slUnion( const sorted_list<Object>& list1, const sorted_list<Object> & list2){
auto i=list1.begin();
auto j=list2.begin();
sorted_list<Object> un;
static sorted_list<Object>* newlist=&un;
while(i!=list1.end() && j!=list2.end()){
if(*i<*j){
un.push_back(*i);
i++;
}
else if(*i>*j){
un.push_back(*j);
j++;
}
else{ //if equal
un.push_back(*i);
i++; j++;
}
}
while(i!=list1.end())
un.push_back(*i++);
while(j!=list2.end())
un.push_back(*j++);
return newlist;
}
};
When the program runs, the "un" in main points to NULL.
int main(){
sorted_list<int> l1;
int i=1;
while(i<10){
l1.push_back(i++);
}
sorted_list<int>l2;
int j=1;
while(j<10){
l2.push_back(j);
j+=2;
}
sorted_list<int> *un = slUnion(l1,l2);
}
You should typically return by value, i.e. sorted_list<Object>. Newer versions of cpp guarantee you that they will not actually make a copy.
What you are doing right now is wrong, because it has undefined behavior. You are using un, which is on the function stack, and return a pointer to it. By the time the function returns un has gone out of scope and the memory location can have been reused. Just completely remove the newlist pointer and return un instead.
You also seem to be confused about classes, methods and functions. As it is, your method does not have to be inside a class, or, since it does not seem to use class state, it can be static, if inside a class. It also does not seem like it would have to be a friend. If you wanted to write this as a member-function, it would look sth like this:
sorted_list<Object>& unionWith(const sorted_list<Object>& rhs) {
// merge this and rhs w deduplication into temp, then swap temp with this
...
return *this;
}
I think what is likely your problem, is that you don't assign &un to newlist, but that you initialize newlist with &un. Initialization is only performed once for a function-static variable, so future iterations of your method just skip that line and the pointer points to where the original version of un was. Try putting the assignment on a different line. That should fix your immediate issue, but the solution with the static pointer is still really bad, because the pointer is shared by all instances.

C++ Sorting Objects in a vector based on member boolean

In my program, I have classes I use for handling projectiles in a game.
class Projectile
{
bool IsActive;
bool GetActive();
//....
};
class Game
{
std::vector<Projectile*> ProjectilesToUpdate;
//....
};
Of course, there is more to it than that, however I'm trying to stay relevant to my current problem.
I want to use std::sort to make it so that all projectiles where IsActive == true are at the far beginning and that any projectile which isn't active is at the very end.
How would I go about doing this?
Basically, you want to create a partition:
std::partition(std::begin(ProjectilesToUpdate),
std::end(ProjectilesToUpdate),
[](Projectile const* p) { return p->GetActive(); }
);
As for the subsidiary questions:
I had to remove the "const" part in the code to make it compile.
That's because your GetActive() method should be const:
bool GetActive() const { return IsActive; }
See Meaning of "const" last in a C++ method declaration?
how can I use this to delete every single object (and pointer to object) that is no longer needed?
You could use smart pointers (such as std::shared_ptr) and no longer care about delete. Thus you could use the Erase–remove idiom as follow:
std::vector<std::shared_ptr<Projectile>> ProjectilesToUpdate;
// :
// :
auto it = std::remove_if(
std::begin(ProjectilesToUpdate),
std::end(ProjectilesToUpdate),
[](std::shared_ptr<Projectile> const& p) { return !p->GetActive(); } // mind the negation
);
ProjectilesToUpdate.erase(it, std::end(ProjectilesToUpdate));
Related question: What is a smart pointer and when should I use one?
If you don't want to use smart pointers, you could use the returned iterator which point to the first element of the second group (i.e. the non active ones) and iterate until the end of the array:
auto begin = std::begin(ProjectilesToUpdate);
auto end = std::end(ProjectilesToUpdate);
auto start = std::partition(begin, end,
[](Projectile const* p) { return p->GetActive(); }
);
for (auto it = start; it != end; ++it) {
delete *it;
}
ProjectilesToUpdate.erase(start, end);
Note that I'm not calling erase inside the loop since it invalidates iterators.
And of course, this last solution is more complex than using smart pointers.

Iterator over a list pointing to wrong elements

I have 2 variables: std::list lst; and std::list<A*>::iterator lstiter with A being a class. The lst is filled with pointers to the class! B is an other class that holds both variables.
Puts iterator at the begin of the list:
void B::iterstart() {
lstiter = lst.begin();
}
void B::iternext() {
iteratend();
lstiter = ++lstiter;
}
void B::iterprev() {
iteratstart();
lstiter = --lstiter;
}
void B::iteratend() {
if (lst.empty())
throw -1;
if (lstiter == lst.end())
throw "Error: end of list\n";
}
void B::iteratstart() {
if (lst.empty())
throw -1;
if (lstiter == lst.begin())
throw "Error: begin of list\n";
}
(I also have a function that gets the pointer at the element in the list the iterator is pointing too at the moment. Iteratend and iteratstart throw an exception when there are no elements in the list and when I try to go past the last or first element. This is where my problem is!
Now I call: iterstart(); iternext(); iternext(); iternext(); I never get the message!(End of list)
And I do have some other bugs too, Sometimes I call the procedure prev, but I get the return value of the procedure next! (But I want to try to solve the other problem first)
This lstiter = ++lstiter is wrong. With an integer it might work but when used with complicated C++ objects it does not always perform correctly because it depends on the specific implementation of the increment and copy functions. Also, why would you want to do it? All you need is ++lstiter.

generic "out of bounds", "past end" iterator

In my application I have a (unbalanced) tree datastructure. This tree is simply made of "std::list of std::lists" - node holds an arbitrary "list" of sub-nodes. Using this instead of a single list made the rest of the application a lot easier. (The program is about changing moving nodes from one tree to another tree / another part in the tree / to it's own tree).
Now an obvious task is to find a subtree inside a "tree". For non-recursive searches it is simple enough:
subtree_iterator find_subtree(const N& n) {
auto iter(subtrees.begin());
auto e(subtrees.end());
while (iter != e) {
if ((*iter)->name == n) {
return iter;
}
++iter;
}
return e;
}
Which returns an iterator to the subtree position. The problem however starts when I try to implement a multi-level search. Ie, I wish to search for hello.world.test where the dots mark a new level.
Searching worked alright
subtree_iterator find_subtree(const pTree_type& otree, std::string identify) const {
pTree_type tree(otree);
boost::char_separator<char> sep(".");
boost::tokenizer<boost::char_separator<char> > tokens(identify, sep);
auto token_iter(tokens.begin());
auto token_end(tokens.end());
subtree_iterator subtree_iter;
for (auto token_iter(tokens.begin()); token_iter != token_end; ++token_iter) {
std::string subtree_string(*token_iter);
subtree_iter = tree->find_subtree_if(subtree_string);
if (subtree_iter == tree->subtree_end()) {
return otree->subtree_end()
} else {
tree = *subtree_iter;
}
}
return subtree_iter;
}
On first glace it seemed to work "correct", however when I try to use it, it fails. Using it would be like
auto tIn(find_subtree(ProjectTree, "hello.world.test"));
if (tIn != ProjectTree->subtree_end()) {
//rest
}
however that gives a debug assertion error "list iterators not compatible". This isn't too weird: I'm comparing a iterators from different lists to each other. However I could I implement such a thing? My "backup" option would be to return a std::pair<bool,iterator> where the boolean part determines if the tree actually exists. Is there another method, short of making the whole tree single list?
You should not work on iterators internaly. Use nodes instead.
template <typename T>
struct Node {
T item;
Node<T>* next;
};
Then encapsulate your Node in an iterator facade like this :
template<typename T>
class iterator {
private:
Node<T>* node;
public:
...
};
Then use a generic invalid node (when node is nullptr) that is returned whenever end() is reached or returned.
Note that what i suggest is a single linked list (not double linked list as the standard one). this is because you can't go back from an invalid generic end() iterator that point to an invalid null node.
If you don't use iterator operator--() in your algorithms this should be fine.
std::vector<list_iterator> stack to traverse? Where the .back() of the stack is the only one allowed to be equal to end() of the previous one, and .front() is an iterator to the root list?

how to convert iterator of list STL to instance (C++)

this is my first time using the list STL and i'm not sure if what i'm trying to do is possible.
I have class_B which holds a list of class_A, I need a function in class_B that takes an ID, searches the list for an instance with the same ID, and gets a pointer form the list to the instance in that list:
bool class_B::get_pointer(int ID,class_A* pointer2A){
list<class_A>::iterator i;
for(i=class_A.begin();i!=class_A.end();i++){
if((*i).get_id()==ID) {
\\pointer2A=(i);<---------------this is what I'm trying to do
return true;
}
}
pointer2A=NULL;
return false;
}
how do I perform this, is it possible to convert from iterator to instance ?
EDIT:
I'm using this function in a multi-threaded program and I can't return an iterator to the calling function since another thread might delete an element of the list.
Now that I have a pointer to my element(and lets say it's locked so it can't be deleted), and a different thread removed another element and performed a sort on the list, what will happen to the pointer I'm holding ? (I don't know how the list rearranges the elements, is done by copying the elements using a copy c'tor, or by another mean?).
Useless answer was the most helpful in my case (BIG thanks), and yes I should use a reference to the pointer since I'm planing to change it.
You should write this:
pointer2A= &*i;
Here *i returns the object whose address you can get by prepending & as : &*i.
Note that i is not same as &*i. See this topic for more general discussion:
Difference between &(*similarObject) and similarObject? Are they not same?
Anyway, I would suggest you to read the pointer itself as:
class_A* class_B::get_pointer(int ID)
{
//I assume the name of the list is objA, not class_A
for(list<class_A>::iterator i=objA.begin();i!=objA.end();i++)
{
if( i->get_id()==ID)
{
return &*i;
}
}
return NULL; //or nullptr in C++11
}
Or, in C++11, you can use std::find_if as:
auto it = std::find_if(objA.begin(),
objA.end(),
[&](class_A const &a){ return a->get_id() == ID;});
classA *ptr = NULL;
if ( it != objA.end())
ptr = &*it; //get the pointer from iterator
Make sure get_id is a const member function.
if(i->get_id()==ID) {
pointer2A=&*i;
return true;
}
iterators are designed to have similar semantics to pointers, so for example you can write i->get_id() just as if you had a pointer to A.
Similarly, *i yields a reference A&, and &*i converts that back into a pointer - it looks a bit clunky (it would be an identity operation if i were really a pointer), but it's idiomatic.
Note that this won't do what you presumably want anyway - the caller's class_A* pointer2A is passed by value, so only get_pointer's copy of the pointer is modified, and the caller won't see that value. Try this:
bool class_B::get_pointer(int ID, class_A *& pointer2A)
{
list<class_A>::iterator i;
for(i=class_A.begin();i!=class_A.end();i++) {
if(i->get_id()==ID) {
pointer2A=&*i;
return true;
}
}
pointer2A=NULL;
return false;
}
Now pointer2A is passed by reference, so the caller's copy gets modified inside your function.
BTW, you can read the parameter declaration class_A * & pointer2A right-to-left, as "pointer2A is a reference to a pointer to class_A".
If you have an iterator, you can get a raw pointer by simply dereferencing the iterator (which gives you a reference), and then taking the address of that (which gives you a pointer). So, in your case:
pointer2A = &*i;
This might seem like an odd, clumsy way to get a pointer, and it is. But you normally don't care about pointers when you are using the collections & iterators from the Std Lib. Iterators are the glue that hold the "STL" together. That's what you should be dealing with, by and large, rather than raw pointers.
The loop you've written above certainly gets the job done that you wish to accomplish, but there are better* ways to accomplish the same goal. (Better is a subjective term.) In particular, the <algorithm> library provides both std::find and std::find_if which do just what they say they do. They find something in a collection. find will find something that is equal to what you're looking for. find_if will find something that matches some criteria that you specify. The latter is the appropriate algorithm to use here, and there are two main ways to use it.
The first, more "traditional" approach is to use a functor:
struct match_id : public std::unary_function<bool, class_A>
{
match_id(int ID) : id_(id) {};
bool operator()(const class_A* rhs) const
{
if( id_ == rhs->get_id() )
return true;
else
return true;
};
/* ... */
list<class_A>::iterator it = std::find_if(objA.begin(), objA.end(), match_id(ID));
This approach works in C++03 or C++11. Some people don't like it because it is rather verbose. I like it, on the other hand, because the actual buisness logic (the find_if call) is quite succinct and more expressive than an explicit loop.
In C++11, you can use a lambda in place of the functor:
unsigned ID = 42;
std::find_if( objA.begin(), objB.end(), [&ID](const class_A& rhs) -> bool { return rhs.get_id() == ID; } };
There's a tradeoff here. On the pro side, you don't have to write 10 or so lines of code for the functor, but on the con side, the lambda syntax is funky and takes a bit of getting used to.