how to implement lower_bound in struct

I am trying to implement a lower_bound function for memory mapped map value with struct as key and value type, but i have no idea how to do it. Till now i've written this much:
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
#include <ros/ros.h>
// PCL specific includes
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
#include <fstream>
#include <iostream>

#include <map>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>
using namespace boost::interprocess;

    struct MyKey {
        int d0, d1, d2, a0, b0, a1, b1, a2, b2;

        bool operator < (const MyKey& o) const {
        return std::tie(d0, d1, d2, a0, b0, a1, b1, a2, b2) 
                   < std::tie(o.d0, o.d1, o.d2, o.a0, o.b0, o.a1, o.b1, o.a2, o.b2);
        }

        template<class Ar>
        void serialize (Ar& ar, const unsigned int) {
            ar & d0;
            ar & d1;
            // ditto 
        }    
    };

    struct MyValue {
        int p0, p1, p2;

        template<class Ar>
        void serialize(Ar& ar, const unsigned int) {
            ar & p0;
            ar & p1;
            ar & p2;
            //
        }
    };
    
int main (int argc, char** argv)
{
    ros::init (argc, argv, "training");
    ros::NodeHandle nh;
  
    /*std::ofstream s("obj_pattern.bin"); 
    boost::archive::text_oarchive oa(s);
    
    std::map<MyKey, MyValue> pobj;

    for(int i=0;i<1000000;i++) {
        pobj.insert({{i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8}, {i, i+1, i+2}});
    }
    oa << pobj;*/ 
    
    std::map<MyKey, MyValue> pobj;
    file_mapping fm("obj_pattern.bin", read_only);
    mapped_region region(fm, read_only);
    MyKey * addr = (MyKey *)region.get_address();  


    auto it = addr->lower_bound({0, 1,2,3,4,5,6,7,8} );
    if (it != end(pobj)) {
        const MyKey& key = it->first;
        const MyValue& value = it->second;
        std::cout << "found: " << value.p0 << " " << value.p1 << " " << value.p2 << std::endl;
    } 
}
Last edited on
std::map already has lower_bound, it's unclear why you want to implement it again.

Based on the demo, I think what you're asking how to call it. It's map's member function, so you call it using the dot syntax on a map, or using the arrow syntax on a pointer to map, just like any other member function.

In your program, you're using the arrow syntax on a pointer to MyKey, which is not a map.

If you want to learn how to store and retrieve a map from a memory-mapped file using boost, look at the documentation, it's very different from what you have so far: https://www.boost.org/doc/libs/1_69_0/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.containers_explained
Registered users can post here. Sign in or register to post.