-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutility.cpp
More file actions
156 lines (130 loc) · 3.75 KB
/
Copy pathutility.cpp
File metadata and controls
156 lines (130 loc) · 3.75 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright (C) 2018, Jianwen Li (lijwen2748@gmail.com), Iowa State University
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
Author: Jianwen Li
Update Date: October 30, 2017
Utility functions
*/
#include "utility.h"
#include <vector>
#include <algorithm>
using namespace std;
namespace car {
void print (const std::vector<int>& v)
{
for (int i = 0; i < v.size (); i ++)
std::cout << v[i] << " ";
std::cout << std::endl;
}
void print (const hash_set<int>& s)
{
for (hash_set<int>::const_iterator it = s.begin (); it != s.end (); it ++)
std:: cout << *it << " ";
std::cout << std::endl;
}
void print (const hash_set<unsigned>& s)
{
for (hash_set<unsigned>::const_iterator it = s.begin (); it != s.end (); it ++)
std:: cout << *it << " ";
std::cout << std::endl;
}
void print (const hash_map<int, int>& m)
{
for (hash_map<int, int>::const_iterator it = m.begin (); it != m.end (); it ++)
std:: cout << it->first << " -> " << it->second << std::endl;
}
void print (const hash_map<int, std::vector<int> >& m)
{
for (hash_map<int, std::vector<int> >::const_iterator it = m.begin (); it != m.end (); it ++)
{
std::cout << it->first << " -> {";
vector<int> v = it->second;
for (int i = 0; i < v.size (); i ++)
cout << v[i] << " ";
cout << "}" << endl;
}
}
bool comp (int i, int j)
{
return abs (i) < abs(j);
}
//elements in v1, v2 are in order
//check whether v2 is contained in v1
bool imply ( std::vector<int>& v1, std::vector<int>& v2)
{
if (v1.size () < v2.size ())
return false;
std::vector<int>::iterator first1 = v1.begin (), first2 = v2.begin (), last1 = v1.end (), last2 = v2.end ();
while (first2 != last2)
{
if ( (first1 == last1) || comp (*first2, *first1) )
return false;
if ((*first1) == (*first2))
++ first2;
++ first1;
}
return true;
/*
if (v1.size () < v2.size ())
return false;
int j = 0;
for (int i = 0; i < v2.size (); i ++)
{
if (j >= v1.size ())
return false;
while (abs(v1[j]) < abs(v2[i]))
{
j++;
if (j >= v1.size ())
return false;
}
if (v1[j] == v2[i])
j++;
else
return false;
}
return true;
*/
}
std::vector<int> vec_intersect (const std::vector<int>& v1, const std::vector<int>& v2)
{
std::vector<int> res;
std::vector<int>::const_iterator first1 = v1.begin (), first2 = v2.begin (), last1 = v1.end (), last2 = v2.end ();
while (first2 != last2)
{
if (first1 == last1)
break;
if (comp (*first1, *first2))
first1 ++;
else if ((*first1) == (*first2))
{
res.push_back (*first1);
first1 ++;
first2 ++;
}
else
first2 ++;
}
return res;
}
bool is_in (const int id, const std::vector<int>& v, const int begin, const int end){
//v is in order
if (begin > end)
return false;
int pos = (begin+end)/2;
if (v[pos] == id)
return true;
return (is_in (id, v, begin, pos-1) || is_in (id, v, pos+1, end));
}
}