| 138 | void Route::reserve(size_t size) { nodes.reserve(size); } |
| 139 | |
| 140 | void Route::insert(size_t idx, Node *node) |
| 141 | { |
| 142 | assert(0 < idx && idx < nodes.size()); |
| 143 | auto const isDepot = node->client() < data.numDepots(); |
| 144 | |
| 145 | if (isDepot) // is depot, so we need to insert a copy into our own memory |
| 146 | { |
| 147 | if (depots_.size() == depots_.capacity()) // then we reallocate and |
| 148 | { // must update references |
| 149 | depots_.reserve(depots_.size() + 1); |
| 150 | for (auto &depot : depots_) |
| 151 | nodes[depot.idx()] = &depot; |
| 152 | } |
| 153 | |
| 154 | node = &depots_.emplace_back(node->client()); |
| 155 | } |
| 156 | |
| 157 | if (numTrips() > maxTrips()) |
| 158 | throw std::invalid_argument("Vehicle cannot perform this many trips."); |
| 159 | |
| 160 | nodes.insert(nodes.begin() + idx, node); |
| 161 | node->assign(this, idx, nodes[idx - 1]->trip()); |
| 162 | |
| 163 | for (size_t after = idx; after != nodes.size(); ++after) |
| 164 | { |
| 165 | nodes[after]->idx_ = after; |
| 166 | if (isDepot) // then we need to bump each following trip index |
| 167 | nodes[after]->trip_++; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void Route::push_back(Node *node) { insert(nodes.size() - 1, node); } |
| 172 | |