-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathompss_static_map.hpp
More file actions
129 lines (102 loc) · 3.07 KB
/
ompss_static_map.hpp
File metadata and controls
129 lines (102 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2019 Jimmy Aguilar Mena
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OMPSS_STATIC_MAP
#define OMPSS_STATIC_MAP
#include <map>
#include <algorithm>
#include <sstream>
#include "ompss_static_buffer.hpp"
/*
* This is the simplest implementation for a fix sized map with
* templates, keeping the log complexity to search and compatible with
* ompss without using allocation hacks.
*/
template <typename _Key,
typename _Tp,
typename _Alloc = std::allocator<std::pair<const _Key, _Tp>>>
class ompss_static_map
{
public:
typedef _Key key_type;
typedef _Tp mapped_type;
typedef std::pair<const _Key, _Tp> value_type;
typedef _Alloc allocator_type;
// Iterator
typedef value_type *iterator;
typedef const value_type *const_iterator;
ompss_static_map() : _buffer()
{}
ompss_static_map(const std::map<key_type, mapped_type> &in) :
_buffer(in)
{}
ompss_static_map &operator=(const std::map<key_type, mapped_type> &in)
{
_buffer.copy(in);
return *this;
}
mapped_type &operator[] (const key_type &k)
{
iterator it = _buffer.lower_bound(k);
if (it->first != k)
_buffer.insert(it, k).second = mapped_type();
return it->second;
}
iterator begin() { return _buffer.begin(); }
const_iterator begin() const { return _buffer.begin(); }
iterator end() { return _buffer.end(); }
const_iterator end() const { return _buffer.end(); }
mapped_type *data() { return _buffer; }
bool empty() const { return _buffer.empty(); }
void clear() { _buffer.clear(); }
void reserve(std::size_t new_cap) { _buffer.reserve(new_cap); }
std::size_t size() const { return _buffer.size(); }
std::size_t max_size() const { return _buffer.max_size(); }
// Find
iterator find(const key_type &k)
{
return _buffer.find(k);
}
const_iterator find(const key_type &k) const
{
return _buffer.find(k);
}
// Lower bound
const_iterator lower_bound(const key_type &k) const
{
return _buffer.lower_bound(k);
}
iterator lower_bound(const key_type &k)
{
return _buffer.lower_bound(k);
}
private:
omp_static_buffer <_Key, std::pair<const _Key, _Tp>, _Alloc> _buffer;
};
template <typename _Key, typename _Tp>
std::string pairToStr(const std::pair<_Key, _Tp> &in)
{
std::ostringstream ss;
ss << "<" << in.first << ";" << in.second << ">";
return ss.str();
}
template <typename _Key, typename _Tp>
std::ostream& operator<< (std::ostream& os, const std::pair<_Key, _Tp> &in)
{
os << pairToStr(in);
return os;
}
#endif //OMPSS_STATIC_MAP