1 |
|
|
/* |
2 |
|
|
* Software License Agreement (BSD License) |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2011-2014, Willow Garage, Inc. |
5 |
|
|
* Copyright (c) 2014-2016, Open Source Robotics Foundation |
6 |
|
|
* All rights reserved. |
7 |
|
|
* |
8 |
|
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
|
* modification, are permitted provided that the following conditions |
10 |
|
|
* are met: |
11 |
|
|
* |
12 |
|
|
* * Redistributions of source code must retain the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
|
* * Redistributions in binary form must reproduce the above |
15 |
|
|
* copyright notice, this list of conditions and the following |
16 |
|
|
* disclaimer in the documentation and/or other materials provided |
17 |
|
|
* with the distribution. |
18 |
|
|
* * Neither the name of Open Source Robotics Foundation nor the names of its |
19 |
|
|
* contributors may be used to endorse or promote products derived |
20 |
|
|
* from this software without specific prior written permission. |
21 |
|
|
* |
22 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
23 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
24 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
25 |
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
26 |
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
27 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
28 |
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 |
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
30 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32 |
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33 |
|
|
* POSSIBILITY OF SUCH DAMAGE. |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
/** @author Jia Pan */ |
37 |
|
|
|
38 |
|
|
#ifndef HPP_FCL_BROADPHASE_SIMPLEHASHTABLE_INL_H |
39 |
|
|
#define HPP_FCL_BROADPHASE_SIMPLEHASHTABLE_INL_H |
40 |
|
|
|
41 |
|
|
#include "hpp/fcl/broadphase/detail/simple_hash_table.h" |
42 |
|
|
|
43 |
|
|
#include <iterator> |
44 |
|
|
namespace hpp { |
45 |
|
|
namespace fcl { |
46 |
|
|
|
47 |
|
|
namespace detail { |
48 |
|
|
|
49 |
|
|
//============================================================================== |
50 |
|
|
template <typename Key, typename Data, typename HashFnc> |
51 |
|
|
SimpleHashTable<Key, Data, HashFnc>::SimpleHashTable(const HashFnc& h) : h_(h) { |
52 |
|
|
// Do nothing |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
//============================================================================== |
56 |
|
|
template <typename Key, typename Data, typename HashFnc> |
57 |
|
|
void SimpleHashTable<Key, Data, HashFnc>::init(size_t size) { |
58 |
|
|
if (size == 0) { |
59 |
|
|
HPP_FCL_THROW_PRETTY("SimpleHashTable must have non-zero size.", |
60 |
|
|
std::logic_error); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
table_.resize(size); |
64 |
|
|
table_size_ = size; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
//============================================================================== |
68 |
|
|
template <typename Key, typename Data, typename HashFnc> |
69 |
|
|
void SimpleHashTable<Key, Data, HashFnc>::insert(Key key, Data value) { |
70 |
|
|
std::vector<unsigned int> indices = h_(key); |
71 |
|
|
size_t range = table_.size(); |
72 |
|
|
for (size_t i = 0; i < indices.size(); ++i) |
73 |
|
|
table_[indices[i] % range].push_back(value); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
//============================================================================== |
77 |
|
|
template <typename Key, typename Data, typename HashFnc> |
78 |
|
|
std::vector<Data> SimpleHashTable<Key, Data, HashFnc>::query(Key key) const { |
79 |
|
|
size_t range = table_.size(); |
80 |
|
|
std::vector<unsigned int> indices = h_(key); |
81 |
|
|
std::set<Data> result; |
82 |
|
|
for (size_t i = 0; i < indices.size(); ++i) { |
83 |
|
|
size_t index = indices[i] % range; |
84 |
|
|
std::copy(table_[index].begin(), table_[index].end(), |
85 |
|
|
std::inserter(result, result.end())); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
return std::vector<Data>(result.begin(), result.end()); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
//============================================================================== |
92 |
|
|
template <typename Key, typename Data, typename HashFnc> |
93 |
|
|
void SimpleHashTable<Key, Data, HashFnc>::remove(Key key, Data value) { |
94 |
|
|
size_t range = table_.size(); |
95 |
|
|
std::vector<unsigned int> indices = h_(key); |
96 |
|
|
for (size_t i = 0; i < indices.size(); ++i) { |
97 |
|
|
size_t index = indices[i] % range; |
98 |
|
|
table_[index].remove(value); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
//============================================================================== |
103 |
|
|
template <typename Key, typename Data, typename HashFnc> |
104 |
|
|
void SimpleHashTable<Key, Data, HashFnc>::clear() { |
105 |
|
|
table_.clear(); |
106 |
|
|
table_.resize(table_size_); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
} // namespace detail |
110 |
|
|
} // namespace fcl |
111 |
|
|
} // namespace hpp |
112 |
|
|
|
113 |
|
|
#endif |