Subversion Repositories HomeAutomation

Rev

Rev 1598 | Rev 1990 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Bitset.h"
  22.  
  23. #include <math.h>
  24. #include <string.h>
  25.  
  26. #include <boost/lexical_cast.hpp>
  27.  
  28. namespace atom {
  29. namespace common {
  30.  
  31. Bitset::Bitset(unsigned int count)
  32. {
  33.     unsigned int byte_size = (int)ceil(count / 8);
  34.    
  35.     this->count_ = count;
  36.     this->bytes_ = new unsigned char[byte_size];
  37.    
  38.     memset(this->bytes_, 0, byte_size);
  39. }
  40.  
  41. Bitset::Bitset(const Byteset& set)
  42. {
  43.     this->count_ = set.GetSize() * 8;
  44.     this->bytes_ = new unsigned char[set.GetSize()];
  45.  
  46.     memcpy(this->bytes_, set.Get(), set.GetSize());
  47. }
  48.  
  49. Bitset::~Bitset()
  50. {
  51.     delete [] this->bytes_;
  52. }
  53.  
  54. unsigned int Bitset::GetCount()
  55. {
  56.     return this->count_;
  57. }
  58.  
  59. unsigned char* Bitset::GetBytes() const
  60. {
  61.     return this->bytes_;
  62. }
  63.  
  64. unsigned long Bitset::Read(unsigned int position, unsigned int length)
  65. {
  66.     unsigned long value = 0;
  67.    
  68.     for (unsigned int index = 0; index < length; index++)
  69.     {
  70.         value = (value << 1) | this->Get(position + index);
  71.     }
  72.    
  73.     return value;
  74. }
  75.  
  76. void Bitset::Write(unsigned int position, unsigned int length, long unsigned value)
  77. {
  78.     for (unsigned int index = 0; index < length; index++)
  79.     {
  80.         if (value & 0x01)
  81.         {
  82.             this->Set(position + (length - index - 1));
  83.         }
  84.         else
  85.         {
  86.             this->Unset(position + (length - index - 1));
  87.         }
  88.        
  89.         value = (value >> 1);
  90.     }
  91. }
  92.  
  93. int Bitset::Set(unsigned int position)
  94. {
  95.     if (position >= this->count_)
  96.     {
  97.         return -1;
  98.     }
  99.    
  100.     this->bytes_[position / 8] |= (0x00000001 << (7 - (position % 8)));
  101.  
  102.     return 0;
  103. }
  104.  
  105. int Bitset::Unset(unsigned int position)
  106. {
  107.     if (position >= this->count_)
  108.     {
  109.         return -1;
  110.     }
  111.    
  112.     this->bytes_[position / 8] &= ~(0x00000001 << (7 - (position % 8)));
  113.    
  114.     return 0;
  115. }
  116.  
  117. int Bitset::Get(unsigned int position)
  118. {
  119.     if (position >= this->count_)
  120.     {
  121.         return -1;
  122.     }
  123.    
  124.     return (this->bytes_[position / 8] & (0x00000001 << (7 - (position % 8))) ? 1 : 0);
  125. }
  126.  
  127. std::string Bitset::ToDebugString()
  128. {
  129.     std::string debug_string;
  130.    
  131.     for (unsigned int n = 0; n < this->count_; n++)
  132.     {
  133.         debug_string += boost::lexical_cast<std::string>(this->Get(n));
  134.     }
  135.    
  136.     return debug_string;
  137. }
  138.  
  139. }; // namespace type
  140. }; // namespace atom
  141.