此源可以使用自定义顺序使用整数对向量的优先级队列:
std::vector < std::tuple < unsigned int, int, int > > targets_vector;
// Related to 'thread_ticket' and 'targets_vector'. Useful as a
// pointer to a variable unique per thread.
// TODO Not standard (__thread).
static __thread std::tuple < unsigned int, int, int > effective_target;
// Related to 'effective_target' initialization. Useful as a boolean
// variable unique per thread.
// TODO Not standard (__thread).
static __thread bool effective_target_filled = false;
struct NearerTarget
{
bool operator()(
const std::vector < std::pair < int, int > > & lhs,
const std::vector < std::pair < int, int > > & rhs) const
{
std::pair < int, int > lhs_back = lhs.back();
std::pair < int, int > rhs_back = rhs.back();
if (effective_target_filled == false)
{
unsigned int i = 0;
bool found = false;
lock.lock();
while (! found && i < targets_vector.size())
{
if (std::get<0>(targets_vector[i]) == thread_ticket)
{
found = true;
}
else
{
i++;
}
}
lock.unlock();
assert(found);
effective_target = targets_vector[i];
effective_target_filled = true;
}
int target_first = std::get<1>(effective_target);
int target_second = std::get<2>(effective_target);
unsigned int lhs_distance =
std::abs(lhs_back.first - target_first) +
std::abs(lhs_back.second - target_second);
unsigned int rhs_distance =
std::abs(rhs_back.first - target_first) +
std::abs(rhs_back.second - target_second);
return (lhs_distance > rhs_distance);
}
};
int function(
const int start_x, const int start_y, const int target_x,
const int target_y)
{
// Generate a ticket.
lock.lock();
thread_ticket = tickets_so_far++;
lock.unlock();
// Initialize data structure.
std::pair < int, int > n_start (start_x, start_y);
std::vector < std::pair < int, int > > starting_path (1, n_start);
std::priority_queue <
std::vector < std::pair < int, int > >,
std::vector < std::vector < std::pair < int, int > > >,
NearerTarget
> active_paths;
active_paths.push(starting_path);
// Initialize target pair.
std::pair < int, int > n_target (target_x, target_y);
// Add target pair to the shared data structure.
lock.lock();
targets_vector.push_back(std::make_tuple(
thread_ticket, n_target.first, n_target.second));
lock.unlock();
// [...]
}
在尝试为优先级队列使用预分配器策略时遇到麻烦。如图所示, easy 至少比其他想要做的更容易预先分配std::vector < T >
。但我没有得到预分配这种std::priority_queue < 3? >
的语法。
// Initialize data structure.
std::pair < int, int > n_start (start_x, start_y);
std::vector < std::pair < int, int > > starting_path (1, n_start);
// Allocator container for the priority queue.
std::vector < unsigned char > pq_allocator;
pq_allocator.reserve(
10 * (std::abs(start_x - target_x) +
std::abs(start_y - target_y)));
// Another argument for creating the preallocated priority queue.
NearerTarget nt;
std::priority_queue <
std::vector < std::pair < int, int > >,
std::vector < std::vector < std::pair < int, int > > >,
NearerTarget
> active_paths(
// std::less < unsigned char > (),
nt,
// std::move(
pq_allocator
// )
);
这让我得到了g++
追踪:
find_path.cpp: In function ‘int FindPath(int, int, int, int, const unsigned cha
r*, int, int, int*, int)’:
find_path.cpp:215:5: error: no matching function for call to ‘std::priority_que
ue<std::vector<std::pair<int, int> >, std::vector<std::vector<std::pair<int, in
t> > >, NearerTarget>::priority_queue(NearerTarget&, std::vector<unsigned char>
&)’
);
^
find_path.cpp:215:5: note: candidates are:
In file included from /usr/local/include/c++/4.8.4/queue:64:0,
from find_path.cpp:12:
/usr/local/include/c++/4.8.4/bits/stl_queue.h:453:9: note: template<class _Inpu
tIterator> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_Input
Iterator, _InputIterator, const _Compare&, _Sequence&&)
priority_queue(_InputIterator __first, _InputIterator __last,
^
/usr/local/include/c++/4.8.4/bits/stl_queue.h:453:9: note: template argument
deduction/substitution failed:
find_path.cpp:215:5: note: deduced conflicting types for parameter ‘_InputIte
rator’ (‘NearerTarget’ and ‘std::vector<unsigned char>’)
);
^
In file included from /usr/local/include/c++/4.8.4/queue:64:0,
from find_path.cpp:12:
/usr/local/include/c++/4.8.4/bits/stl_queue.h:442:9: note: template<class _Inpu
tIterator> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_Input
Iterator, _InputIterator, const _Compare&, const _Sequence&)
priority_queue(_InputIterator __first, _InputIterator __last,
^
/usr/local/include/c++/4.8.4/bits/stl_queue.h:442:9: note: template argument
deduction/substitution failed:
find_path.cpp:215:5: note: deduced conflicting types for parameter ‘_InputIte
rator’ (‘NearerTarget’ and ‘std::vector<unsigned char>’)
);
^
In file included from /usr/local/include/c++/4.8.4/queue:64:0,
from find_path.cpp:12:
/usr/local/include/c++/4.8.4/bits/stl_queue.h:408:7: note: std::priority_queue<
_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, _Sequence&&) [with _
Tp = std::vector<std::pair<int, int> >; _Sequence = std::vector<std::vector<std
::pair<int, int> > >; _Compare = NearerTarget]
priority_queue(const _Compare& __x = _Compare(),
^
/usr/local/include/c++/4.8.4/bits/stl_queue.h:408:7: note: no known conversio
--More--_
情况对你来说是否熟悉?有什么建议?有想法吗?
201510111727 UTC :已编辑源和输出。