c++ - Time/memory efficient work with std::vector -


i'm implementing astar algorithm in each node stored in vector , returned outer object - this:

class astar {    std::vector<node> find()    {       std::vector<node> open;       std::vector<node> closed;       std::vector<node> path;        //a_star algorithm working open , closed vectors       //when path found fill path push_back()        return path;    } } 

in outer object code looks similar this:

class foo {    astar astar;    std::vector<node> path;    void findpath()    {       path = astar.find();    }    //do stuff path } 

reason why findpath() exists want make run in thread, if path found - stuff, else - maybe it's time find path (simplifying). thing bothers me path = astar.find(); because can lot of copying (not mention push_back() in astar class copies value).

possible solutions thought are: passing foo's std::vector<node> path; reference argument astar's find(); or make friendship in between foo , astar foo access private path. prior me time , memory efficiency (time on memory).

first remember c++ allows return value optimization, returning vector value not in problem.

however:

  • it costly repeated allocations of objects might allocated once. theoretically have method take reference or pointer buffer path, must guaranteed have enough memory. way, outer code allocates once , may re-use buffer repeated calls.
  • it costly construct object don't plan on using it. might want method named has_path(const node& source, const node& target), or stand-alone function functionality.

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -