Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mraduldubey/b0c57912c90220f2181c445b66c7c32f to your computer and use it in GitHub Desktop.
Save mraduldubey/b0c57912c90220f2181c445b66c7c32f to your computer and use it in GitHub Desktop.
Basic usage of boot multi index container
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/ordered_index.hpp>
using namespace std;
int main()
{
struct Video
{
uint64_t start_ts, end_ts;
std::string path = "";
Video(std::string &_path , uint64_t _start_ts) : path(_path), start_ts(_start_ts) {}
Video(std::string _path, uint64_t _start_ts, uint64_t _end_ts) : path(_path), start_ts(_start_ts), end_ts(_end_ts) {}
/* this operators can be used to order the default index
bool operator<(const employee& e)const { return id < e.id; } */
/* returns the length of the name field - can be used to order indices
std::size_t name_length()const { return name.size(); } */
};
using namespace boost::multi_index;
// tags
struct videoPath {};
struct videoTS {};
// define the multi index
typedef boost::multi_index_container<
Video,
indexed_by<
// sort by less<string> on path
ordered_unique<tag<videoPath>, member<Video, std::string, &Video::path> >,
// sort by less<int> on videoTS
ordered_non_unique<tag<videoTS>, member<Video, uint64_t, &Video::start_ts> >
>
> VideoCache;
// create the actual object
VideoCache cache;
// insert
Video sample("c:/data/100.mp4", 100, 150);
cache.insert(sample);
Video sample1("c:/data/200.mp4", 200, 250);
cache.insert(sample1);
Video sample2("c:/data/300.mp4", 300, 350);
cache.insert(sample2);
// count
VideoCache::index<videoTS>::type& videoTSIndex = cache.get<videoTS>();
std::cout << "100 startTS count " << videoTSIndex.count(100)<< "\n";
std::cout << "100.mp4 count " << cache.count("c:/data/100.mp4")<< "\n";
std::cout << "300.mp4 count " << cache.count("c:/data/300.mp4")<< "\n";
std::cout << "200.mp4 count " << cache.count("c:/data/200.mp4")<< "\n";
// bound / range search operations
int val = 100;
// iterator to elem not less than val
auto lb = videoTSIndex.lower_bound(val);
std::cout << "lower bound <" << lb->path << "," << lb->start_ts << "," << lb->end_ts << ">\n";
std::cout << "prev elem: it is starting elem <" << bool(lb == videoTSIndex.begin()) << ">\n";
/* upper bound - iter to elem greater than val - not required for our application
auto ub = videoTSIndex.upper_bound(val); */
val = 200;
lb = videoTSIndex.lower_bound(val);
std::cout << "lower bound <" << lb->path << "," << lb->start_ts << "," << lb->end_ts << ">\n";
auto prev = --lb;
std::cout << "prev elem <" << prev->path << "," << prev->start_ts << "," << prev->end_ts << ">\n";
val = 250;
lb = videoTSIndex.lower_bound(val);
std::cout << "lower bound <" << lb->path << "," << lb->start_ts << "," << lb->end_ts << ">\n";
prev = --lb;
std::cout << "prev elem <" << prev->path << "," << prev->start_ts << "," << prev->end_ts << ">\n";
// search
std::cout << "Searching for 200 <" << bool(videoTSIndex.find(200) == videoTSIndex.end()) << ">\n";
std::cout << "Searching for 250 <" << bool(videoTSIndex.find(250) == videoTSIndex.end()) << ">\n";
// lower bound based on path
std::string tempPath = "c:/data/250.mp4";
auto lb2 = cache.lower_bound(tempPath);
std::cout << "lower bound <" << lb2->path << "," << lb2->start_ts << "," << lb2->end_ts << ">\n";
auto prev2 = --lb2;
std::cout << "prev elem <" << prev2->path << "," << prev2->start_ts << "," << prev2->end_ts << ">\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment