AttributeProperties.cc

Go to the documentation of this file.
00001 // $Id: AttributeProperties.cc 241 2006-07-05 07:29:52Z bingmann $
00002 
00003 #include "AttributeProperties.h"
00004 #include "GraphProperties.h"
00005 #include "AttributeBlob.h"
00006 #include "AttributeBlob_impl.h"
00007 #include "ByteOutBuffer.h"
00008 
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 namespace VGServer {
00013 
00014 unsigned int AttributePropertiesList::calcDefaultBytes(unsigned int n)
00015 {
00016     if (n == 0) return 0;
00017     return ((n-1) / 8) + 1;
00018 }
00019 
00020 void AttributePropertiesList::calcAttributeLookups()
00021 {
00022     // clears attrname map
00023     attrname_map.clear();
00024 
00025     std::vector<class AttributeProperties>::iterator attr = begin();
00026 
00027     // run from front to back of the attributes and
00028     // 1) fill in the accelerated lookup index for fixed, no-default attributes
00029     // 2) after the first with-default attribute start counting default-bits
00030 
00031     int lookup = 0;
00032     
00033     unsigned int defbitnum = 0;
00034 
00035     unsigned int attrid = 0;
00036 
00037     while(attr != end())
00038     {
00039         if (attr->getType() == ATTRTYPE_BOOL) {
00040             // bool type gets a default bit num to store it's value in. it does
00041             // not interfere with direct lookups
00042             attr->lookup = -1;
00043             attr->varying = false;
00044         }
00045         else if (lookup >= 0 and (attr->varying and attr->isFixedLength()))
00046         {
00047             // fill in the current lookup index and advance it.
00048             attr->lookup = lookup;
00049             lookup += attr->getTypeLength();
00050         }
00051         else {
00052             // one of the tests failed -> no more accelerated lookups
00053             attr->lookup = lookup = -1;
00054         }
00055 
00056         if (not attr->varying)
00057         {
00058             attr->defbitnum = defbitnum++;
00059         }
00060 
00061         // save name in map.
00062         attrname_map[attr->name] = attrid++;
00063         
00064         attr++;
00065     }
00066 
00067     // in the special case that we have zero attributes, then all vertx/edge
00068     // objects would be considered deleted. therefore we'll add just one empty
00069     // byte instead of a "deleted" flag.
00070     if (lookup == 0 and defbitnum == 0)
00071         defbitnum++;
00072 
00073     // now that we know how many default bytes we need, advance the direct
00074     // lookup indices
00075     if (defbitnum > 0)
00076     {
00077         unsigned int defbytenum = calcDefaultBytes(defbitnum);
00078 
00079         for(attr = begin(); attr != end() and attr->lookup >= 0; attr++)
00080         {
00081             attr->lookup += defbytenum;
00082         }
00083     }
00084 
00085     this->defbits = defbitnum;
00086     this->defbytes = calcDefaultBytes(defbitnum);
00087 }
00088 
00089 int AttributePropertiesList::lookupAttributeName(const std::string &s) const
00090 {
00091     std::map<std::string, unsigned int>::const_iterator it = attrname_map.find(s);
00092 
00093     if (it == attrname_map.end()) return -1;
00094     else return it->second;
00095 }
00096 
00097 // two functions which write the binary representation of the
00098 // AttributePropertiesList. See text docus for a longer explanation.
00099 
00100 void AttributeProperties::appendBinaryFormat(class ByteOutBuffer &bob) const
00101 {
00102 /* Attribute Binary Representation:
00103    
00104 +--------+---------+---------------+----------------+
00105 | type   | namelen | name          | default value  |
00106 +--------+---------+---------------+----------------+
00107 | 1 byte | 1 byte  | namelen bytes | in attr repres |
00108 
00109 type is the value from the attrtype enum.
00110 
00111 */
00112     bob.append<unsigned char>(getType());
00113 
00114     bob.append<unsigned char>(static_cast<unsigned char>(name.size()));
00115     bob.appendString(name);
00116 
00117     // put default value.
00118     if (getType() == ATTRTYPE_BOOL)
00119     {
00120         bob.append<unsigned char>(getInteger());
00121     }
00122     else {
00123         bob.appendAnyType(*this);
00124     }
00125 }
00126 
00127 void AttributePropertiesList::appendBinaryFormat(class ByteOutBuffer &bob) const
00128 {
00129 /* Attribute Binary Representation:
00130    
00131 +---------+----------+--------------------+
00132 | attrlen | defbytes | attribute repres.  |
00133 +---------+----------+--------------------+
00134 | 2 bytes | 1 byte   | variable           |
00135 
00136 the number of default bytes are always included.
00137 
00138 */
00139     bob.append<unsigned short>(static_cast<unsigned short>(size()));
00140     bob.append<unsigned char>(defbytes);
00141 
00142     for(const_iterator ai = begin(); ai != end(); ai++) {
00143         ai->appendBinaryFormat(bob);
00144     }
00145 }
00146 
00147 void GraphProperties::appendBinaryFormat(class ByteOutBuffer &bob) const
00148 {
00149 /* Graph Properties Binary Representation:
00150    
00151 +------+----------+------+--------------------+------+------------------+
00152 | 0x10 | directed | 0x01 | vertex attr repres | 0x02 | edge attr repres |
00153 +------+----------+------+--------------------+------+------------------+
00154 | 1b   | 1 byte   | 1b   | variable           | 1b   | variable         |
00155 
00156 */
00157     bob.append<unsigned char>(0x10);
00158     bob.append<unsigned char>(directed);
00159 
00160     bob.append<unsigned char>(0x01);
00161     vertexattr.appendBinaryFormat(bob);
00162 
00163     bob.append<unsigned char>(0x02);
00164     edgeattr.appendBinaryFormat(bob);
00165 
00166     bob.append<unsigned char>(0x00);
00167 }
00168 
00169 template <typename AttributeTinyBlob>
00170 AttributeTinyBlob AttributePropertiesList::createBlankAttributeBlob() const
00171 {
00172     AttributeTinyBlob ab;
00173     unsigned int len = 0;
00174 
00175     // put the default bit field at the beginning
00176     ab.zero(len, defbytes);
00177     len += defbytes;
00178 
00179     // put in the values of the varying attributes
00180     unsigned int ai;
00181     for (ai = 0; ai < size(); ai++)
00182     {
00183         if (not (*this)[ai].varying) break;
00184         
00185         ab.putAnyType(len, (*this)[ai]);
00186         len += (*this)[ai].getValueLength();
00187     }
00188 
00189     // continue for the rest by setting their default bits.
00190     for (; ai < size(); ai++)
00191     {
00192         assert( not (*this)[ai].varying );
00193         assert( (*this)[ai].defbitnum < defbits );
00194 
00195         if ((*this)[ai].getType() == ATTRTYPE_BOOL)
00196         {
00197             // set the boolean in the default bitfield to the attribute's
00198             // default boolean value.
00199             ab.putBool(0, (*this)[ai].defbitnum, (*this)[ai].getInteger() != 0);
00200         }
00201         else
00202         {
00203             ab.putBool(0, (*this)[ai].defbitnum, true);
00204         }
00205     }
00206 
00207     return ab;
00208 }
00209 
00210 // two instanciations for the two TinyBlob types: (this creates the above's function code in the .o file)
00211 
00212 template AttributeVertexTinyBlob AttributePropertiesList::createBlankAttributeBlob<AttributeVertexTinyBlob>() const;
00213 
00214 template AttributeEdgeTinyBlob AttributePropertiesList::createBlankAttributeBlob<AttributeEdgeTinyBlob>() const;
00215 
00216 } // namespace VGServer

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