-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisland_counter.cpp
More file actions
69 lines (56 loc) · 1.87 KB
/
island_counter.cpp
File metadata and controls
69 lines (56 loc) · 1.87 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
#include <stdexcept>
#include <cstdlib>
#include <boost/python.hpp>
#include "island_counter.h"
using namespace std;
namespace bpy = boost::python;
void IslandCounter::add_position(int pos)
{
positions.push_back(pos);
}
bpy::tuple IslandCounter::maximum_stretch(int island_length)
{
if (positions.size() == 0)
throw runtime_error("The position vector is empty!");
size_t size_biggest_island = 1;
size_t size_current_island = 1;
size_t index_first_elem_biggest_island = 0;
size_t index_last_elem_biggest_island = 0;;
size_t index_first_elem_current_island = 0;
size_t index_last_elem_current_island = 0;
for (size_t i = 1; i < positions.size(); ++i)
{
if (abs(positions[i - 1] - positions[i]) <= island_length)
{
++size_current_island;
index_last_elem_current_island = i;
}
else
{
if (size_current_island > size_biggest_island)
{
index_first_elem_biggest_island = index_first_elem_current_island;
index_last_elem_biggest_island = index_last_elem_current_island;
size_biggest_island = size_current_island;
}
index_first_elem_current_island = i;
size_current_island = 1;
}
}
//handle the case
if (size_current_island > size_biggest_island)
{
size_biggest_island = size_current_island;
index_last_elem_biggest_island = positions.size() - 1;
}
return bpy::make_tuple(positions[index_first_elem_biggest_island], positions[index_last_elem_biggest_island]);
}
void IslandCounter::read(bpy::object reader)
{
for (bpy::stl_input_iterator<bpy::object> it = reader;
it != bpy::stl_input_iterator<bpy::object> (); ++it)
{
int pos = bpy::extract<int>(it->attr("POS"));
positions.push_back(pos);
}
}