LbmSegment.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/lucidera/bitmap/LbmSegment.cpp#8 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2006-2009 LucidEra, Inc.
00005 // Copyright (C) 2006-2009 The Eigenbase Project
00006 //
00007 // This program is free software; you can redistribute it and/or modify it
00008 // under the terms of the GNU General Public License as published by the Free
00009 // Software Foundation; either version 2 of the License, or (at your option)
00010 // any later version approved by The Eigenbase Project.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 #include "fennel/common/CommonPreamble.h"
00023 #include "fennel/lucidera/bitmap/LbmSegment.h"
00024 
00025 FENNEL_BEGIN_CPPFILE("$Id: //open/dev/fennel/lucidera/bitmap/LbmSegment.cpp#8 $");
00026 
00027 uint LbmSegment::byteArray2Value(PBuffer array, uint arraySize)
00028 {
00029     uint value = 0;
00030     while (arraySize > 0) {
00031         value = value * (uint)(1 << LbmOneByteSize) + array[arraySize - 1];
00032         arraySize --;
00033     }
00034     return value;
00035 }
00036 
00037 
00038 uint LbmSegment::value2ByteArray(uint value, PBuffer array, uint arraySize)
00039 {
00040     assert(value != 0);
00041 
00042     uint size = 0;
00043 
00044     while (value > 0 && size < arraySize) {
00045         array[size] = (uint8_t)(value & 0xff);
00046         value = value >> LbmOneByteSize;
00047         size ++;
00048     }
00049     /*
00050      * If value is non-zero, it means that the value cannot be encoded
00051      * within an array of arraySize. Return 0 in that case.
00052      */
00053     if (value > 0) {
00054         size = 0;
00055     }
00056 
00057     return size;
00058 }
00059 
00060 uint LbmSegment::computeSpaceForZeroBytes(uint nZeroBytes)
00061 {
00062     if (nZeroBytes <= LbmZeroLengthCompact) {
00063         return 0;
00064     }
00065 
00066     uint size = 0;
00067     while (nZeroBytes > 0) {
00068         nZeroBytes = nZeroBytes >> LbmOneByteSize;
00069         size++;
00070     }
00071 
00072     return size;
00073 }
00074 
00075 void LbmSegment::readSegDescAndAdvance(
00076     PBuffer &pSegDesc, uint &bmSegLen, uint &zeroBytes)
00077 {
00078     // should only be called in the case where the bit segment has
00079     // a descriptor
00080     assert(pSegDesc != NULL);
00081     bmSegLen = (*pSegDesc >> LbmHalfByteSize) + 1;
00082     uint zeroLen = (*pSegDesc & LbmZeroLengthMask);
00083 
00084     // advance past the initial length byte
00085     pSegDesc++;
00086 
00087     if (zeroLen <= LbmZeroLengthCompact) {
00088         zeroBytes = zeroLen;
00089     } else {
00090         zeroBytes =
00091             byteArray2Value(pSegDesc, zeroLen - LbmZeroLengthCompact);
00092         pSegDesc += zeroLen - LbmZeroLengthCompact;
00093     }
00094 }
00095 
00096 uint LbmSegment::computeSegDescLength(PBuffer segDesc)
00097 {
00098     uint segDescLength = 0;
00099 
00100     while (segDesc < pSegDescEnd) {
00101         uint segBytes;
00102         uint zeroBytes;
00103         readSegDescAndAdvance(segDesc, segBytes, zeroBytes);
00104         segDescLength += computeSpaceForZeroBytes(zeroBytes) + 1;
00105     }
00106 
00107     return segDescLength;
00108 }
00109 
00110 uint LbmSegment::computeSegLength(PBuffer segDesc)
00111 {
00112     uint segLength = 0;
00113 
00114     while (segDesc < pSegDescEnd) {
00115         uint segBytes;
00116         uint zeroBytes;
00117         readSegDescAndAdvance(segDesc, segBytes, zeroBytes);
00118         segLength += segBytes;
00119     }
00120 
00121     return segLength;
00122 }
00123 
00124 uint LbmSegment::countSegments()
00125 {
00126     uint count = 0;
00127 
00128     PBuffer segDesc = pSegDescStart;
00129     while (segDesc < pSegDescEnd) {
00130         uint segBytes;
00131         uint zeroBytes;
00132         readSegDescAndAdvance(segDesc, segBytes, zeroBytes);
00133         count++;
00134     }
00135 
00136     return count;
00137 }
00138 
00139 FENNEL_END_CPPFILE("$Id: //open/dev/fennel/lucidera/bitmap/LbmSegment.cpp#8 $");
00140 
00141 // End LbmSegment.cpp

Generated on Mon Jun 22 04:00:19 2009 for Fennel by  doxygen 1.5.1