solver  1.0
Utils.cpp
1 /*
2  This file is part of the EnergyOptimizatorOfRoboticCells program.
3 
4  EnergyOptimizatorOfRoboticCells is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  EnergyOptimizatorOfRoboticCells is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with EnergyOptimizatorOfRoboticCells. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include <fstream>
19 #include <type_traits>
20 #include <set>
21 #include "RoboticLine.h"
22 #include "Shared/Utils.h"
23 
24 using namespace std;
25 
26 uint64_t pack(const uint32_t& v1, const uint32_t& v2) {
27  uint64_t r = v1;
28  r <<= 32; r |= v2;
29  return r;
30 }
31 
32 pair<uint32_t, uint32_t> unpack(const uint64_t& v) {
33  return { v>>32, v & 0x00000000ffffffff };
34 }
35 
36 template <class T, typename enable_if<is_pointer<T>::value>::type* = nullptr>
37 uintptr_t cast(T v) {
38  return reinterpret_cast<uintptr_t>(v);
39 }
40 
41 template <class T, typename enable_if<is_integral<T>::value>::type* = nullptr>
42 uintptr_t cast(T v) {
43  return static_cast<uintptr_t>(v);
44 }
45 
46 template <class C>
47 uintptr_t elementWiseHash(const C& v) {
48  uintptr_t hash = 0;
49  for (typename C::const_iterator it = v.cbegin(); it != v.cend(); ++it)
50  hash ^= cast(*it);
51 
52  return hash;
53 }
54 
55 template <class C>
56 uintptr_t orderWiseHash(const C& v) {
57  uintptr_t hash = 1ul, i = 1;
58  constexpr uintptr_t R = 3144134277;
59  for (typename C::const_iterator it = v.cbegin(); it != v.cend(); ++it, ++i) {
60  uintptr_t h = cast(*it);
61  hash *= R+2*h*i;
62  hash ^= h;
63  }
64 
65  hash >>= 2;
66 
67  return hash;
68 }
69 
70 template <class T>
71 inline uint64_t hashOW(const T& v) {
72  return orderWiseHash(v);
73 }
74 
75 template <class T>
76 inline uintptr_t hashEW(const T& v) {
77  return elementWiseHash(v);
78 }
79 
80 template uint64_t hashOW<vector<uint32_t>>(const vector<uint32_t>&);
81 template uint64_t hashOW<vector<StaticActivity*>>(const vector<StaticActivity*>&);
82 template uintptr_t hashEW<vector<Location*>>(const vector<Location*>&);
83 template uintptr_t hashEW<set<Location*>>(const set<Location*>&);
84 
85 bool fileExists(const string& pathToFile) {
86  bool exists = false;
87  ifstream in(pathToFile.c_str());
88  if (in.good())
89  exists = true;
90 
91  in.close();
92  return exists;
93 }
94 
STL namespace.
bool fileExists(const std::string &pathToFile)
It checks the existence of the file.
Definition: Utils.cpp:85
Various auxiliary functions used across the program.
uint64_t hashOW(const C &v)
It calculates a hash of the container, the order of elements influences (Order Wise) the hash value...
uintptr_t hashEW(const C &v)
It calculates a hash of the container, the order of elements does not influence (Element Wise) the ha...
std::pair< uint32_t, uint32_t > unpack(const uint64_t &v)
It unpacks two uint32_t numbers from uint64_t data type.
Definition: Utils.cpp:32
uint64_t pack(const uint32_t &v1, const uint32_t &v2)
It packs two uint32_t numbers to uint64_t data type.
Definition: Utils.cpp:26
The file contains various classes devoted to abstract representation of the robotic cell...