pubby / flat Goto Github PK
View Code? Open in Web Editor NEWFlat containers for C++
License: Boost Software License 1.0
Flat containers for C++
License: Boost Software License 1.0
Due to the (admittedly braindead) std::vector<bool>
implementation :
#include <flat_set.hpp>
fc::vector_set<bool>* set_a;
void foo()
{
set_a->find(true);
}
$ clang++ -Wreturn-stack-address wtf.cpp -Ipath/to/flat/include
In file included from wtf.cpp:1:
In file included from /home/jcelerier/score/API/3rdparty/flat/include/flat_set.hpp:8:
/home/jcelerier/score/API/3rdparty/flat/include/impl/flat_impl.hpp:72:42: warning: returning reference to local temporary object
[-Wreturn-stack-address]
reference operator*() const { return *underlying; }
^~~~~~~~~~~
/home/jcelerier/score/API/3rdparty/flat/include/impl/flat_impl.hpp:269:61: note: in instantiation of member function
'fc::impl::flat_iterator<std::_Bit_iterator, fc::impl::dummy_iterator<std::_Bit_iterator>,
std::random_access_iterator_tag>::operator*' requested here
if(it == self()->end() || self()->value_comp()(key, *it))
^
wtf.cpp:7:12: note: in instantiation of member function 'fc::impl::flat_container_base<fc::flat_set<std::vector<bool,
std::allocator<bool> >, std::less<void> >, bool, std::vector<bool, std::allocator<bool> >, std::less<void>, int>::find' requested
here
set_a->find(true);
^
1 warning generated.
(of course in itself a set is quite dumb, but it's a case that can happen, for instance in generic code ; maybe a static_assert should be in place to prevent such cases ?)
Why does flat_container_base not inherit from Compare anymore but instead contains an instance of it as a member? That makes the object size larger than it needs to be.
There is an incompatibility to the std containers:
The iterator is always const
, so that writing to all elements via iterator loop is not possible.
#include "include/flat_set.hpp"
#include <iostream>
template <typename T> struct TD;
int main(int argc, char* argv[]) {
fc::vector_set<int> set;
set.insert(2);
set.insert(1);
for (auto& itr : set) {
// TD<decltype(itr)> td;
itr *=2;
}
for (auto& i : set) {
std::cout << i << "\n";
}
}
The type of itr
is <const int &>
but should <int &>
.
I have the following code (
#include <flat/flat_map.hpp>
template<typename K, typename V>
using flat_map = fc::vector_map<K, V>;
flat_map<double,double> m_points;
double m_lastX = 0;
void addPointUnscaled(double x, double y)
{
m_points[x] = y;
const auto end = m_points.end();
if (m_lastX != -1)
{
if (m_lastX < x)
{
auto it1 = m_points.find(m_lastX);
auto it2 = m_points.lower_bound(x);
if (it1 != end && it2 != end)
{
if((it1 + 1) != end)
{
if((it1 + 1) == it2)
m_points.erase(it2);
else
m_points.erase((it1 + 1), it2);
}
}
}
else if (x < m_lastX)
{
auto it1 = m_points.find(x);
auto it2 = m_points.lower_bound(m_lastX);
if (it1 != end && it2 != end)
{
if((it1 + 1) != end)
{
if((it1 + 1) == it2)
m_points.erase(it2);
else
m_points.erase((it1 + 1), it2);
}
}
}
}
m_lastX = x;
}
int main()
{
m_lastX = -1;
addPointUnscaled(0.20, 0.7);
addPointUnscaled(0.22, 0.3);
}
With this map I sometimes get an assert of the underlying vector at the line marked in my code. Do you spot something that looks like it could be wrong ? thanks
With -Wunused-parameter
enabled, every use of flat_map.hpp is generating multiple warnings;
In file included from ../../../libs/vcpkg_installed/x64-linux/include/flat/flat_map.hpp:8:
../../../libs/vcpkg_installed/x64-linux/include/flat/impl/flat_impl.hpp:224:36: warning: unused parameter 'hint' [-Wunused-parameter]
iterator insert(const_iterator hint, value_type const& value)
^
../../../libs/vcpkg_installed/x64-linux/include/flat/impl/flat_impl.hpp:227:36: warning: unused parameter 'hint' [-Wunused-parameter]
iterator insert(const_iterator hint, value_type&& value)
^
../../../libs/vcpkg_installed/x64-linux/include/flat/impl/flat_impl.hpp:248:38: warning: unused parameter 'hint' [-Wunused-parameter]
auto emplace_hint(const_iterator hint, Args&&... args)
^
../../../libs/vcpkg_installed/x64-linux/include/flat/impl/flat_impl.hpp:381:27: warning: unused parameter 'hint' [-Wunused-parameter]
insert(const_iterator hint, P&& value)
^
Could you add [[maybe_unused]]
in front of the hint arguments to suppress these?
Have there really been problems with boost and other flat_map's approaches of having only the key be const ? I understand that in most cases, even if it's slightly inconvenient it can be work-arounded with using the underlying container directly, but consider this code :
struct timespan { int min, max, offset; }
flat_map<int, timespan> m_overticks;
auto node_it = m_overticks.lower_bound(1234);
if (node_it != m_overticks.end())
{
auto& cur = node_it->second;
if (ot < cur.min)
cur.min = ot;
if (ot > cur.max)
{
cur.max = ot;
cur.offset = tick_offset + tick_ms - cur.max;
}
}
Here I don't see an easy way except const_cast'ing the value and I'd rather not resort to that...
Since enabling C++20 support in my project, using Visual Studio 2019 16.11, I am the following warning about std::rel_ops being deprecated.
flat_impl.hpp(103,30): warning C4996: 'std::rel_ops': warning STL4027: The namespace std::rel_ops and its contents are deprecated in C++20. Their use is superseded by C++20's <=> operator and automatic rewrites of relational expressions. You can define _SILENCE_CXX20_REL_OPS_DEPRECATION_WARNING or _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS to acknowledge that you have received this warning
This code
using Map = fc::vector_set<std::string, std::less<>>;
Map m;
auto found = m.find("Key");
does not compile:
flat_impl.hpp:434:30: error: no matching function for call to object of type 'fc::impl::flat_set_base<fc::flat_set<std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >, std::__1::less<void> >, std::__1::basic_string<char>, std::__1::vector<std::__1::basic_string<char>, std::__1::allocator<std::__1::basic_string<char> > >, std::__1::less<void>, int>::value_compare' (aka 'std::__1::less<void>') if (it == self()->end() || self()->value_comp()(std::ref(key), *it))
ending with
no matching function for call to object of type 'std::__1::less<void>' if (__comp(*__m, __value_))
This is on macOS, Xcode 12.4, using libc++. Using "std::ref" in the find/lower_bound function leads to less::operator(...) being ignored because of a substitution failure. When I take out std::ref it does seem to work. Using a vector_map works.
On the other hand:
std::string_view sv("Key"); auto found = m.find(sv);
works with vector_set, but not vector_map; it balks in first_compare, cannot match "reference_wrapper" (in code transparent_key_t) against basic_string_view.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.