-
Notifications
You must be signed in to change notification settings - Fork 0
/
Box.h
75 lines (60 loc) · 2.07 KB
/
Box.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
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
#ifndef BOX__H
#define BOX__H
#include <shared_mutex>
#include "BoxInfo.h"
/* Box represents a person on the Board. A box contains a unique id and the group it belongs to. It also contains the width and height of the box. */
class Box{
public:
Box(int id, int group, int width, int height);
Box() = default;
Box(const Box& o);
Box(Box&& o) noexcept;
Box& operator= (const Box& o) = delete;
Box& operator= (Box&& o) noexcept = delete;
~Box() noexcept = default;
/* No reason to lock getId(), getGroupId(), getHeight(), getWidth() methods with a shared_lock since _id, _groupid, _width, and _height are immutable.
*/
int getId() const;
int getGroupId() const;
int getHeight() const;
int getWidth() const;
/* getLevel() is protected with a shared_lock using mutex _mm. It can be called at the same time as getInfo(). It can not be called at the same time as upLevel().
*/
int getLevel() const;
/* getInfo() is protected with a shared_lock using mutex _mm. It can be called at the same time as getLevel(). It can not be called at the same time as upLevel().
*/
BoxInfo getInfo() const;
/* upLevel() is protected with a unique_lock using mutex _mm. It can not be accessed by two threads at the same time. It can not be accessed as the same time as getLevel() or getInfo().
*/
void upLevel();
bool operator== (const Box& o) const;
private:
const int _id = -1;
const int _groupid = -1;
int _level = 0;
int _width = -1;
int _height = -1;
mutable std::shared_mutex _mm;
};
namespace std
{
template<>
struct hash<Box>
{
size_t operator()(const Box& b) const
{
// prime numbers
int A = 32059;
int B = 54059;
int C = 76963;
int D = 86969;
unsigned int h = 97;
h = (h) ^ (b.getId() * A);
h = (h) ^ (b.getGroupId() * B);
h = (h) ^ (b.getWidth() * C);
h = (h) ^ (b.getHeight() * D);
return h;
}
};
}
#endif