forked from morgonfrilla/PyClone
/
partition.py
119 lines (86 loc) · 3.03 KB
/
partition.py
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
'''
This file is part of PyDP.
PyDP 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.
PyDP 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 PyDP. If not, see
<http://www.gnu.org/licenses/>.
Created on 2012-09-20
@author: Andrew Roth
'''
class Partition(object):
def __init__(self):
self.cells = []
@property
def cell_values(self):
return [cell.value for cell in self.cells]
@property
def counts(self):
return [cell.size for cell in self.cells]
@property
def item_values(self):
cell_values = self.cell_values
labels = self.labels
return [cell_values[i] for i in labels]
@property
def labels(self):
labels = [None] * self.number_of_items
for cell_index, cell in enumerate(self.cells):
for item in cell.items:
labels[item] = cell_index
return labels
@property
def number_of_cells(self):
return len(self.cells)
@property
def number_of_items(self):
return sum(self.counts)
def add_cell(self, value):
cell = PartitionCell(value)
self.cells.append(cell)
return cell
def add_item(self, item, cell_index):
self.cells[cell_index].add_item(item)
def get_cell_by_value(self, value):
for cell in self.cells:
if cell.value == value:
return cell
def remove_item(self, item, cell_index):
self.cells[cell_index].remove_item(item)
def remove_empty_cells(self):
for cell in self.cells[:]:
if cell.empty:
self.cells.remove(cell)
def copy(self):
partition = Partition()
for cell_index, cell in enumerate(self.cells):
partition.add_cell(cell.value)
for item in cell.items:
partition.add_item(item, cell_index)
return partition
class PartitionCell(object):
def __init__(self, value):
self.value = value
self._items = []
@property
def empty(self):
if self.size == 0:
return True
else:
return False
@property
def items(self):
return self._items[:]
@property
def size(self):
return len(self._items)
def add_item(self, item):
self._items.append(item)
def remove_item(self, item):
self._items.remove(item)
def __contains__(self, x):
if x in self._items:
return True
else:
return False