-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgeneric_utils.h
48 lines (35 loc) · 1.2 KB
/
generic_utils.h
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
/** generic_utils.h -*- C++ -*-
Mathieu Stefani, 30 April 2014
Copyright (c) 2014 Datacratic. All rights reserved.
Various generic utilities
*/
#pragma once
#include <algorithm>
#include <type_traits>
namespace Datacratic {
/** Given a particular container, returns the index of a partircular element
based on member variable.
Example:
struct Foo {
int id;
};
std::vector<Foo> foos { Foo(0), Foo(1), Foo(3), Foo(6) };
int i1 = indexOf(foos, &Foo::id, 3);
assert(i1 == 2);
int i2 = indexOf(foos, &Foo::id, 9);
assert(i2 == -1);
*/
template<typename Container, typename Value, typename Class, typename Member>
int indexOf(const Container &container, Class Member::*ptr, const Value &value)
{
static_assert(std::is_same<typename Container::value_type, Member>::value,
"Member does not match container value type");
auto it =
std::find_if(std::begin(container), std::end(container),
[&](const Member &member) { return member.*ptr == value; });
if (it == std::end(container)) {
return -1;
}
return std::distance(std::begin(container), it);
}
} // namespace Datacratic