GraphLoader.cc

Go to the documentation of this file.
00001 // $Id: GraphLoader.cc 244 2006-07-07 09:11:51Z schultes $
00002 
00003 #include "GraphLoader.h"
00004 #include "GraphData.h"
00005 
00006 #include "AttributeBlob_impl.h"
00007 
00008 #include <stdlib.h>
00009 
00010 namespace VGServer {
00011 
00012 GraphLoader::GraphLoader(const class GraphProperties &gp)
00013     : GraphData(gp)
00014 {
00015     vertex_minnum = 0;
00016     vertex_aidx = vertex_aidxnext = 0;
00017 
00018     edge_minsrc = edge_mintgt = 0;
00019     edge_eidx = 0;
00020     edge_aidx = edge_aidxnext = 0;
00021 
00022     // clears the termination nodes, which are added by GraphData's constructor.
00023     vertices.clear();
00024     edges.clear();
00025 }
00026 
00027 void GraphLoader::reserve(unsigned int vertexnum, unsigned int edgenum,
00028                           unsigned int vertexattrsize, unsigned int edgeattrsize)
00029 {
00030     vertices.reserve(vertexnum);
00031     edges.reserve(edgenum);
00032 
00033     vertexattr.alloc(vertexattrsize);
00034     edgeattr.alloc(edgeattrsize);
00035 }
00036 
00037 class GraphData& GraphLoader::finish()
00038 {
00039     if (vertex_minnum != VERTEX_INVALID) terminate();
00040 
00041     return *this;
00042 }
00043 
00044 void GraphLoader::terminate()
00045 {
00046     if (vertex_minnum > 0)
00047         finishVertexAttrSequence();
00048     
00049     // adds termination vertex of the vertices[].attridx ascending sequence and
00050     // fill in edgeidx for vertices without edges.
00051     {
00052         GraphData::Vertex& vend = vertices.new_back();
00053         vend.attridx = vertex_aidxnext;
00054 
00055         for (unsigned int v = edge_minsrc; v < vertices.size(); v++)
00056         {
00057             vertices[v].edgeidx = edge_eidx;
00058         }
00059     }
00060 
00061     if (edges.size() > 0)
00062         finishEdgeAttrSequence();
00063     
00064     // add termination edge object for edgeidx and attridx
00065     {
00066         GraphData::Edge& eend = edges.new_back();
00067         eend.attridx = edge_aidxnext;
00068     }
00069 
00070     vertex_minnum = VERTEX_INVALID;
00071     edge_minsrc = edge_mintgt = VERTEX_INVALID;
00072 }
00073 
00074 void GraphLoader::addVertex(vertexid_t vid)
00075 {
00076     if (vid < vertex_minnum) throw OrderException();
00077 
00078     if (vertex_minnum > 0)
00079         finishVertexAttrSequence();
00080 
00081     // grow vertex buffer, this code will be deleted as the TpArray auto-grows.
00082 #ifndef NDEBUG
00083     unsigned int oldcap = vertices.capacity();
00084     vertices.reserve(vid+2);
00085     if (oldcap != vertices.capacity()) 
00086         fprintf(stderr,"New vertex capacity: %d\n", vertices.capacity());
00087 #endif
00088 
00089     // create nodes with filled in attridx:
00090     // set all vertex.attridx from the last added+1 vertex up to this to the
00091     // current pointer
00092     for (unsigned int v = vertex_minnum; v <= vid; v++)
00093     {
00094         // figure out if we have to add space for varying attributes?
00095         // no we dont: varying attribute space may not exist
00096         GraphData::Vertex &vnew = vertices.new_back();
00097         assert(vertices.size() == v+1);
00098 
00099         vnew.attridx = vertex_aidxnext;
00100     }
00101 
00102     vertexcount++;
00103 
00104     vertex_minnum = vid+1;
00105 
00106     // position of the default bitfield
00107     vertex_aidx = vertex_aidxnext;
00108 
00109     // clear default bits
00110     vertexattr.zero(vertex_aidx, properties.vertexattr.defbytes);
00111 
00112     // advance the attribute write pointer the number of default bits.
00113     vertex_aidxnext = vertex_aidx + properties.vertexattr.defbytes;
00114 
00115     // reset attribute loading sequence
00116     vertex_lastid = 0;
00117 }
00118 
00119 void GraphLoader::setVertexAttr(vertexid_t vid, unsigned int attrid, const class AnyType &value)
00120 {
00121     if (vid+1 < vertex_minnum) throw OrderException();
00122     if (attrid < vertex_lastid) throw OrderException();
00123 
00124     const AttributePropertiesList &attrlist = properties.vertexattr;
00125 
00126     if (attrid >= attrlist.size()) throw AttributeIdException();
00127 
00128     if (vertex_minnum < vid+1)
00129         addVertex(vid); // add missing vertex entries
00130 
00131     // now vertex_aidx is the right pointer
00132 
00133     // fill in skipped varying attributes
00134     for(unsigned int ai = vertex_lastid; ai < attrid; ai++)
00135     {
00136         if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00137         {
00138             // put the default value of the bool attribute into the default bitfield
00139             vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00140         }
00141         else if (attrlist[ai].varying)
00142         {
00143             // incase we skip over a varying attribute, we have to leave
00144             // space in the attribute value array
00145 
00146             // zeros the value of a varying attribute. writes the default value
00147             // of a varying attribute.
00148             vertexattr.putAnyType(vertex_aidxnext, attrlist[ai]);
00149 
00150             vertex_aidxnext += attrlist[ai].getValueLength();
00151         }
00152         else
00153         {
00154             // for attributes with default values, set the default bit.
00155             vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, true);
00156         }
00157     }
00158 
00159     // now vertex_aidxnext points to the place to insert this value
00160     if (value.getType() == attrlist[attrid].getType())
00161     {
00162         if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00163         {
00164             // put the value in the default bitfield
00165             vertexattr.putBool(vertex_aidx, attrlist[attrid].defbitnum, value.getInteger() != 0);
00166         }
00167         else
00168         {
00169             // save directly as the value parameter has the right type
00170             vertexattr.putAnyType(vertex_aidxnext, value);
00171             vertex_aidxnext += value.getValueLength();
00172         }
00173     }
00174     else
00175     {
00176         AnyType tv = value; // convert value
00177         tv.convertType(attrlist[attrid].getType());
00178 
00179         if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00180         {
00181             // put the value in the default bitfield
00182             vertexattr.putBool(vertex_aidx, attrlist[attrid].defbitnum, tv.getInteger() != 0);
00183         }
00184         else
00185         {
00186             vertexattr.putAnyType(vertex_aidxnext, tv);
00187             vertex_aidxnext += tv.getValueLength();
00188         }
00189     }
00190 
00191     if (not attrlist[attrid].varying && attrlist[attrid].getType() != ATTRTYPE_BOOL)
00192     {
00193         // clear the default bit.
00194         vertexattr.putBool(vertex_aidx, attrlist[attrid].defbitnum, false);
00195     }
00196 
00197     vertex_lastid = attrid+1;
00198 }
00199 
00200 void GraphLoader::finishVertexAttrSequence()
00201 {
00202     const AttributePropertiesList &attrlist = properties.vertexattr;
00203     
00204     for(unsigned int ai = vertex_lastid; ai < attrlist.size(); ai++)
00205     {
00206         if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00207         {
00208             // put the default value of the bool attribute into the default bitfield
00209             vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00210         }
00211         else if (attrlist[ai].varying)
00212         {
00213             // incase we skip over a varying attribute, we have to leave
00214             // space in the attribute value array
00215 
00216             // zeros the value of a varying attribute. writes the default value
00217             // of a varying attribute.
00218             vertexattr.putAnyType(vertex_aidxnext, attrlist[ai]);
00219 
00220             vertex_aidxnext += attrlist[ai].getValueLength();
00221         }
00222         else
00223         {
00224             // for attributes with default values, set the default bit.
00225             vertexattr.putBool(vertex_aidx, attrlist[ai].defbitnum, true);
00226         }
00227     }
00228    
00229     vertex_lastid = attrlist.size()+1;
00230 }
00231 
00232 void GraphLoader::addEdge(vertexid_t src, vertexid_t tgt)
00233 {
00234     if ( not ( (src == edge_minsrc-1 and tgt >= edge_mintgt) or (src >= edge_minsrc) ) )
00235         throw OrderException();
00236 
00237     // check vertex buffer size: cannot add edges to unloaded vertex ids
00238     // (target id doesnt matter, it can be larger)
00239     if (src >= vertices.size())
00240         throw DataLoadedException();
00241 
00242     // this code should also be delete.
00243 #ifndef NDEBUG
00244     unsigned int oldcap = edges.capacity();
00245     edges.reserve(edges.size()+1);
00246     if (oldcap != edges.capacity()) 
00247         fprintf(stderr,"New edge capacity: %d\n", edges.capacity());
00248 #endif
00249 
00250     // fill in remaining varying/default attribute values of last edge
00251     if (edges.size() > 0) finishEdgeAttrSequence();
00252     
00253     // allocate the edge object in the edge adjacency array
00254     GraphData::Edge &enew = edges.new_back();
00255     enew.target = tgt;
00256 
00257     // the added object is right at the end.
00258     assert( edge_eidx+1 == edges.size() );
00259     // see assertion. edge_eidx = edges.size()-1;
00260 
00261     // fill in edgeidx for skipped and this source vertices:
00262     // set all vertex.edgeidx from the last source vertex+1 up to this to the
00263     // current pointer
00264     for (unsigned int v = edge_minsrc; v <= src; v++)
00265     {
00266         vertices[v].edgeidx = edge_eidx;
00267     }
00268 
00269     edge_minsrc = src+1;
00270     edge_mintgt = tgt+1;
00271 
00272     // position of the default bitfield
00273     edge_aidx = edge_aidxnext;
00274     edges[edge_eidx].attridx = edge_aidx;
00275 
00276     edge_eidx++; // eidx points to the next index to be used.
00277 
00278     // clear default bits
00279     edgeattr.zero(edge_aidx, properties.edgeattr.defbytes);
00280 
00281     // advance the attribute write pointer the number of default bits.
00282     edge_aidxnext = edge_aidx + properties.edgeattr.defbytes;
00283 
00284     // reset attribute loading sequence
00285     edge_lastid = 0;
00286 }
00287 
00288 void GraphLoader::setEdgeAttr(vertexid_t src, vertexid_t tgt,
00289                               unsigned int attrid, const class AnyType &value)
00290 {
00291     if ( not ( (src == edge_minsrc-1 and tgt+1 >= edge_mintgt) or (src >= edge_minsrc) ) )
00292         throw OrderException();
00293 
00294     if (attrid < edge_lastid) throw OrderException();
00295 
00296     const AttributePropertiesList &attrlist = properties.edgeattr;
00297 
00298     if (attrid >= attrlist.size()) throw AttributeIdException();
00299 
00300     if (edge_minsrc != src+1 or edge_mintgt != tgt+1)
00301         addEdge(src,tgt); // advance to the right edge
00302 
00303     // now edge_aidx is the right pointer
00304 
00305     // fill in skipped varying attributes
00306     for(unsigned int ai = edge_lastid; ai < attrid; ai++)
00307     {
00308         if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00309         {
00310             // put the default value of the bool attribute into the default bitfield
00311             edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00312         }
00313         else if (attrlist[ai].varying)
00314         {
00315             // incase we skip over a varying attribute, we have to leave
00316             // space in the attribute value array
00317 
00318             // write "default" zero of the varying attribute.
00319             edgeattr.putAnyType(edge_aidxnext, attrlist[ai]);
00320 
00321             edge_aidxnext += attrlist[ai].getValueLength();
00322         }
00323         else
00324         {
00325             // for attributes with default values, set the default bit.
00326             edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, true);
00327         }
00328     }
00329 
00330     // now edge_aidxnext points to the place to insert this value
00331     if (value.getType() == attrlist[attrid].getType())
00332     {
00333         if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00334         {
00335             // put the value in the default bitfield
00336             edgeattr.putBool(edge_aidx, attrlist[attrid].defbitnum, value.getInteger() != 0);
00337         }
00338         else
00339         {
00340             // save directly as the value parameter has the right type
00341             edgeattr.putAnyType(edge_aidxnext, value);
00342             edge_aidxnext += value.getValueLength();
00343         }
00344     }
00345     else
00346     {
00347         AnyType tv = value; // convert value
00348         tv.convertType(attrlist[attrid].getType());
00349 
00350         if (attrlist[attrid].getType() == ATTRTYPE_BOOL)
00351         {
00352             // put the value in the default bitfield
00353             edgeattr.putBool(edge_aidx, attrlist[attrid].defbitnum, tv.getInteger() != 0);
00354         }
00355         else
00356         {
00357             edgeattr.putAnyType(edge_aidxnext, tv);
00358             edge_aidxnext += tv.getValueLength();
00359         }
00360     }
00361 
00362     if (not attrlist[attrid].varying && attrlist[attrid].getType() != ATTRTYPE_BOOL)
00363     {
00364         // clear the default bit.
00365         edgeattr.putBool(edge_aidx, attrlist[attrid].defbitnum, false);
00366     }
00367 
00368     edge_lastid = attrid+1;
00369 }
00370 
00371 void GraphLoader::finishEdgeAttrSequence()
00372 {
00373     const AttributePropertiesList &attrlist = properties.edgeattr;
00374     
00375     for(unsigned int ai = edge_lastid; ai < attrlist.size(); ai++)
00376     {
00377         if (attrlist[ai].getType() == ATTRTYPE_BOOL)
00378         {
00379             // put the default value of the bool attribute into the default bitfield
00380             edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, attrlist[ai].getInteger() != 0);
00381         }
00382         else if (attrlist[ai].varying)
00383         {
00384             // incase we skip over a varying attribute, we have to leave
00385             // space in the attribute value array
00386 
00387             // write "default" zero of the varying attribute.
00388             edgeattr.putAnyType(edge_aidxnext, attrlist[ai]);
00389 
00390             edge_aidxnext += attrlist[ai].getValueLength();
00391         }
00392         else
00393         {
00394             // for attributes with default values, set the default bit.
00395             edgeattr.putBool(edge_aidx, attrlist[ai].defbitnum, true);
00396         }
00397     }
00398     edge_lastid = attrlist.size()+1;
00399 }
00400 
00401 } // namespace VGServer

Generated on Wed Sep 27 14:34:00 2006 for VGServer by  doxygen 1.4.7