forked from anubhav-gres/VAL-4.2.08
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartitions.h
145 lines (121 loc) · 3.77 KB
/
Partitions.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
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
/************************************************************************
* Copyright 2008, Strathclyde Planning Group,
* Department of Computer and Information Sciences,
* University of Strathclyde, Glasgow, UK
* http://planning.cis.strath.ac.uk/
*
* Maria Fox, Richard Howey and Derek Long - VAL
* Stephen Cresswell - PDDL Parser
*
* This file is part of VAL, the PDDL validator.
*
* VAL 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 2 of the License, or
* (at your option) any later version.
*
* VAL 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 VAL. If not, see <http://www.gnu.org/licenses/>.
*
************************************************************************/
#ifndef __PARTITIONS
#define __PARTITIONS
/* This code is designed to support a simple problem in managing partitions.
The idea is that we have a collection of objects that are going to start
off as individuals and, over a series of operations, we are going to merge
them into sets of increasing size. The sets will together form a partition
of the union of all the objects at all times. At the end we want each object
to be mapped to its partition so that we can associate each object with
a unique structure that is identical for all the objects in the same partition.
*/
#include <list>
#include <map>
template<class _Key, class _PData, class _PDataCombine>
class Partitioner {
private:
typedef std::pair<_PData,list< _Key > > _pdata;
typedef std::map<_Key,_pdata> _pmap;
_pmap partitiondata;
_PDataCombine combine;
int partitions;
struct partitionStruct {
_Key key;
partitionStruct * next;
partitionStruct(_Key k) : key(k), next(0) {};
};
typedef map<_Key,partitionStruct *> PElink;
PElink pelements;
partitionStruct * trace(partitionStruct * p) const
{
while(p->next) p = p->next;
return p;
};
public:
Partitioner(_PDataCombine c) : combine(c), partitions(0)
{};
void add(_Key k,_PData p)
{
if(pelements.find(k) != pelements.end()) return;
list<_Key> sk;
sk.push_front(k);
partitiondata.insert(make_pair(k,make_pair(p,sk)));
pelements[k] = new partitionStruct(k);
partitions++;
};
bool contains(_Key k) const
{
return pelements.find(k) != pelements.end();
};
void setData(_Key k,_PData p)
{
if(pelements.find(k) == pelements.end())
{
add(k,p);
}
else
{
partitiondata[trace(pelements[k])->key].first = p;
};
};
void connect(_Key k1,_Key k2)
{
if(pelements.find(k1) == pelements.end() ||
pelements.find(k2) == pelements.end()) return;
partitionStruct * e1 = trace(pelements[k1]);
partitionStruct * e2 = trace(pelements[k2]);
if(e1==e2) return;
partitiondata[e1->key].second.merge(partitiondata[e2->key].second);
partitiondata[e1->key].first =
combine(partitiondata[e1->key].first,partitiondata[e2->key].first);
partitiondata.erase(e2->key);
e2->next = e1;
partitions--;
};
_PData getData(_Key k)
{
return partitiondata.find(trace(pelements.find(k)->second)->key)->second.first;
};
int count() const
{
return partitions;
};
_Key partition(_Key k) const
{
if(pelements.find(k) == pelements.end()) return k;
return trace(pelements[k])->key;
};
typedef typename _pmap::const_iterator PSI;
PSI begin() {return partitiondata.begin();};
PSI end() {return partitiondata.end();};
typedef const pair<_Key, _pdata > DataSource;
static _PData grabData(DataSource & p)
{
return p.second.first;
};
};
#endif