ResizeableBitset.h

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #ifndef _RESIZEABLE_BITMAP_H_
00017 #define _RESIZEABLE_BITMAP_H_
00018 
00019 #include <vector>
00020 #include <stdint.h>
00021 
00022 class ResizeableBitset {
00023 
00024 public:
00025     ResizeableBitset() : m_bits(0) {
00026     }
00027     ResizeableBitset(size_t size) : m_bits(new std::vector<uint8_t>) {
00028         m_bits->assign((size >> 3) + 1, 0);
00029     }
00030     ResizeableBitset(const ResizeableBitset &b) {
00031         m_bits = new std::vector<uint8_t>(*b.m_bits);
00032     }
00033     ResizeableBitset &operator=(const ResizeableBitset &b) {
00034         if (&b != this) return *this;
00035         delete m_bits;
00036         m_bits = new std::vector<uint8_t>(*b.m_bits);
00037         return *this;
00038     }
00039     ~ResizeableBitset() {
00040         delete m_bits;
00041     }
00042     
00043     void resize(size_t bits) { // losing all data
00044         if (!m_bits || bits < m_bits->size()) {
00045             delete m_bits;
00046             m_bits = new std::vector<uint8_t>;
00047         }
00048         m_bits->assign((bits >> 3) + 1, 0);
00049     }
00050     
00051     bool get(size_t column) const {
00052         return ((*m_bits)[column >> 3]) & (1u << (column & 0x07));
00053     }
00054     
00055     void set(size_t column) {
00056         ((*m_bits)[column >> 3]) |=  (uint8_t(1) << (column & 0x07));
00057     }
00058 
00059     void reset(size_t column) {
00060         ((*m_bits)[column >> 3]) &= ~(uint8_t(1) << (column & 0x07));
00061     }
00062 
00063     void copy(size_t source, size_t dest) {
00064         get(source) ? set(dest) : reset(dest);
00065     }
00066     
00067 private:
00068     std::vector<uint8_t> *m_bits;
00069 };
00070 
00071 
00072 #endif
00073 

Generated on Wed Feb 20 15:45:27 2008 for SonicVisualiser by  doxygen 1.5.1