00001
00002
00003 #include "Changelist.h"
00004 #include "AttributeBlob_impl.h"
00005
00006 #include <algorithm>
00007 #include <iostream>
00008
00009 namespace VGServer {
00010
00011 Changelist::Changelist(const class GraphData &_graph)
00012 : graph(_graph)
00013 {
00014 }
00015
00016 void Changelist::clear()
00017 {
00018 vertexchglist.clear();
00019 vertexaddlist.clear();
00020
00021 edgechglist.clear();
00022 edgeaddlist.clear();
00023 }
00024
00025 bool Changelist::addVertex(vertexid_t id)
00026 {
00027 vertexchglist_t::iterator v = vertexchglist.find(id);
00028 if (v != vertexchglist.end())
00029 {
00030
00031 if (not v->second.empty()) return false;
00032
00033
00034
00035 AttributeVertexTinyBlob &ab = vertexchglist[id] = graph.getVertexAttrBlob(id);
00036
00037 if (ab.empty()) {
00038
00039 vertexchglist[id] = graph.properties.vertexattr.createBlankAttributeBlob<AttributeVertexTinyBlob>();
00040 }
00041 }
00042 else
00043 {
00044
00045 if (graph.existVertex(id)) return false;
00046
00047
00048 vertexchglist[id] = graph.properties.vertexattr.createBlankAttributeBlob<AttributeVertexTinyBlob>();
00049 }
00050
00051 assert( vertexaddlist.find(id) == vertexaddlist.end() );
00052 vertexaddlist.insert(id);
00053
00054 return true;
00055 }
00056
00057 bool Changelist::addVertex(vertexid_t id, const AttributeVertexTinyBlob &vb)
00058 {
00059 vertexchglist_t::iterator v = vertexchglist.find(id);
00060 if (v != vertexchglist.end())
00061 {
00062
00063 if (not v->second.empty()) return false;
00064
00065
00066 vertexchglist[id] = vb;
00067 }
00068 else
00069 {
00070
00071 if (graph.existVertex(id)) return false;
00072
00073
00074 vertexchglist[id] = vb;
00075 }
00076
00077 assert( vertexaddlist.find(id) == vertexaddlist.end() );
00078 vertexaddlist.insert(id);
00079
00080 return true;
00081 }
00082
00083 bool Changelist::setVertexAttr(vertexid_t id, unsigned int attrid, const AnyType &value)
00084 {
00085 vertexchglist_t::iterator v = vertexchglist.find(id);
00086 if (v != vertexchglist.end())
00087 {
00088
00089 if (v->second.empty()) return false;
00090
00091
00092 v->second.putAttrChainValue(graph.properties.vertexattr, attrid, value);
00093 }
00094 else
00095 {
00096
00097 AttributeVertexTinyBlob ab = graph.getVertexAttrBlob(id);
00098
00099 if (ab.empty()) return false;
00100
00101
00102 ab.putAttrChainValue(graph.properties.vertexattr, attrid, value);
00103
00104
00105 vertexchglist[id] = ab;
00106 }
00107 return true;
00108 }
00109
00110 bool Changelist::setVertexAttr(vertexid_t id, const AttributeVertexTinyBlob &vb)
00111 {
00112 vertexchglist_t::iterator v = vertexchglist.find(id);
00113 if (v != vertexchglist.end())
00114 {
00115
00116 if (v->second.empty()) return false;
00117
00118
00119 v->second = vb;
00120 }
00121 else
00122 {
00123
00124 if (not graph.existVertex(id)) return false;
00125
00126
00127 vertexchglist[id] = vb;
00128 }
00129 return true;
00130 }
00131
00132 bool Changelist::delVertex(vertexid_t id)
00133 {
00134 vertexchglist_t::iterator v = vertexchglist.find(id);
00135 if (v != vertexchglist.end())
00136 {
00137
00138 if (v->second.empty()) return false;
00139
00140
00141 v->second = AttributeVertexTinyBlob();
00142
00143 assert( vertexaddlist.find(id) != vertexaddlist.end() );
00144 vertexaddlist.erase(id);
00145 }
00146 else
00147 {
00148
00149 if (not graph.existVertex(id)) return false;
00150
00151
00152 vertexchglist[id] = AttributeVertexTinyBlob();
00153 }
00154 return true;
00155 }
00156
00157 bool Changelist::addEdge(vertexid_t src, vertexid_t tgt)
00158 {
00159 edgechglist_t::iterator e = find_edgechglist(src,tgt);
00160
00161 if (e != edgechglist.end())
00162 {
00163
00164 if (not e->second.empty()) return false;
00165
00166
00167
00168 AttributeEdgeTinyBlob &ab = e->second = graph.getEdgeAttrBlob(src,tgt);
00169
00170 if (ab.empty()) {
00171
00172 e->second = graph.properties.edgeattr.createBlankAttributeBlob<AttributeEdgeTinyBlob>();
00173 }
00174 }
00175 else
00176 {
00177
00178 if (graph.existEdge(src,tgt)) return false;
00179
00180
00181 edgechglist.insert( edgechglist_t::value_type(vertexidpair_t(src,tgt),
00182 graph.properties.edgeattr.createBlankAttributeBlob<AttributeEdgeTinyBlob>()) );
00183 }
00184
00185 assert( edgeaddlist.find( vertexidpair_t(src,tgt) ) == edgeaddlist.end() );
00186 edgeaddlist.insert( vertexidpair_t(src,tgt) );
00187
00188 return true;
00189 }
00190
00191 bool Changelist::addEdge(vertexid_t src, vertexid_t tgt, const AttributeEdgeTinyBlob &eb)
00192 {
00193 edgechglist_t::iterator e = find_edgechglist(src,tgt);
00194
00195 if (e != edgechglist.end())
00196 {
00197
00198 if (not e->second.empty()) return false;
00199
00200
00201 e->second = eb;
00202 }
00203 else
00204 {
00205
00206 if (graph.existEdge(src,tgt)) return false;
00207
00208
00209 edgechglist.insert( edgechglist_t::value_type(vertexidpair_t(src,tgt), eb) );
00210 }
00211
00212 assert( edgeaddlist.find( vertexidpair_t(src,tgt) ) == edgeaddlist.end() );
00213 edgeaddlist.insert( vertexidpair_t(src,tgt) );
00214
00215 return true;
00216 }
00217
00218 bool Changelist::setEdgeAttr(vertexid_t src, vertexid_t tgt, unsigned int attrid, const AnyType &value)
00219 {
00220 edgechglist_t::iterator e = find_edgechglist(src,tgt);
00221
00222 if (e != edgechglist.end())
00223 {
00224
00225 if (e->second.empty()) return false;
00226
00227
00228 e->second.putAttrChainValue(graph.properties.edgeattr, attrid, value);
00229 }
00230 else
00231 {
00232
00233 AttributeEdgeTinyBlob ab = graph.getEdgeAttrBlob(src,tgt);
00234
00235 if (ab.empty()) return false;
00236
00237
00238
00239 ab.putAttrChainValue(graph.properties.edgeattr, attrid, value);
00240
00241
00242 edgechglist.insert( edgechglist_t::value_type(vertexidpair_t(src,tgt), ab) );
00243 }
00244 return true;
00245 }
00246
00247 bool Changelist::setEdgeAttr(vertexid_t src, vertexid_t tgt, const AttributeEdgeTinyBlob &eb)
00248 {
00249 edgechglist_t::iterator e = find_edgechglist(src,tgt);
00250
00251 if (e != edgechglist.end())
00252 {
00253
00254 if (e->second.empty()) return false;
00255
00256
00257 e->second = eb;
00258 }
00259 else
00260 {
00261
00262 if (not graph.existEdge(src,tgt)) return false;
00263
00264
00265 edgechglist.insert( edgechglist_t::value_type(vertexidpair_t(src,tgt), eb) );
00266 }
00267 return true;
00268 }
00269
00270 bool Changelist::delEdge(vertexid_t src, vertexid_t tgt)
00271 {
00272 edgechglist_t::iterator e = find_edgechglist(src,tgt);
00273
00274 if (e != edgechglist.end())
00275 {
00276
00277 if (e->second.empty()) return false;
00278
00279
00280 e->second = AttributeEdgeTinyBlob();
00281
00282 assert( edgeaddlist.find( vertexidpair_t(src,tgt) ) != edgeaddlist.end() );
00283 edgeaddlist.erase( vertexidpair_t(src,tgt) );
00284 }
00285 else
00286 {
00287
00288 if (not graph.existEdge(src,tgt)) return false;
00289
00290
00291 edgechglist.insert( edgechglist_t::value_type(vertexidpair_t(src,tgt),
00292 AttributeEdgeTinyBlob()) );
00293 }
00294 return true;
00295 }
00296
00297 std::vector<Changelist::edgeblobpair_t> Changelist::getEdgeListChange(vertexid_t src) const
00298 {
00299 std::vector<edgeblobpair_t> ev;
00300
00301 unsigned int ec = edgechglist.count(vertexidpair_t(src,0));
00302 if (ec == 0) return ev;
00303
00304 edgechglist_t::const_iterator e = edgechglist.find(vertexidpair_t(src, 0));
00305
00306 while( e != edgechglist.end() )
00307 {
00308 if (e->first.first == src)
00309 {
00310 ev.push_back( edgeblobpair_t(e->first.second, &e->second) );
00311
00312 if (--ec == 0) break;
00313 }
00314
00315 ++e;
00316 }
00317
00318 std::sort( ev.begin(), ev.end() );
00319
00320 return ev;
00321 }
00322
00323 }