SparseBitmap.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #ifndef _OASYS_SPARSE_BITMAP_H_
00018 #define _OASYS_SPARSE_BITMAP_H_
00019 
00020 #include <vector>
00021 #include "config.h"
00022 
00023 #include "../debug/DebugUtils.h"
00024 #include "../debug/Formatter.h"
00025 #include "../serialize/Serialize.h"
00026 #include "../serialize/SerializableVector.h"
00027 #include "../util/StringAppender.h"
00028 
00029 namespace oasys {
00030 
00043 template <typename _inttype_t>
00044 class SparseBitmap : public Formatter,
00045                      public SerializableObject {
00046 public:
00050     SparseBitmap();
00051 
00055     SparseBitmap(const Builder&);
00056 
00060     void set(_inttype_t start, _inttype_t len = 1);
00061 
00065     void clear(_inttype_t start, _inttype_t len = 1);
00066 
00070     bool is_set(_inttype_t start, _inttype_t len = 1);
00071 
00075     bool operator[](_inttype_t val)
00076     {
00077         return is_set(val, 1);
00078     }
00079 
00083     _inttype_t num_set();
00084 
00088     bool empty() { return bitmap_.empty(); }
00089 
00093     size_t num_entries() { return bitmap_.size(); }
00094 
00098     void clear() { bitmap_.clear(); }
00099     
00104     _inttype_t num_contiguous();
00105 
00109     int format(char* bp, size_t len) const;
00110     
00114     void serialize(SerializeAction* a);
00115     
00116 protected:
00117     class Range : public SerializableObject {
00118     public:
00120         Range(_inttype_t start, _inttype_t end)
00121             : start_(start), end_(end) {}
00122 
00124         Range(const Builder&)
00125             : start_(0), end_(0) {}
00126         
00128         void serialize(SerializeAction* a);
00129         
00130         _inttype_t start_;
00131         _inttype_t end_;
00132     };
00133     
00134     typedef SerializableVector<Range> RangeVector;
00135     RangeVector bitmap_;
00136     
00137     void validate();
00138 
00139 public:
00150     class iterator {
00151     public:
00155         iterator();
00156 
00160         _inttype_t operator*();
00161 
00165         iterator& operator++();
00166 
00170         iterator operator++(int);
00171 
00175         iterator operator+(unsigned int diff);
00176 
00180         iterator& operator+=(unsigned int diff);
00181 
00185         iterator& operator--();
00186 
00190         iterator operator--(int);
00191         
00195         iterator operator-(unsigned int diff);
00196 
00200         iterator& operator-=(unsigned int diff);
00201 
00205         bool operator==(const iterator& other);
00206 
00210         bool operator!=(const iterator& other);
00211 
00217         iterator& skip_contiguous();
00218 
00219     private:
00220         friend class SparseBitmap<_inttype_t>;
00221 
00223         iterator(typename RangeVector::iterator iter,
00224                  _inttype_t offset);
00225 
00227         typename RangeVector::iterator iter_;
00228         
00230         _inttype_t offset_;
00231     };
00232 
00236     iterator begin();
00237 
00241     iterator end();
00242 
00246     _inttype_t first() { return *begin(); }
00247 
00251     _inttype_t last() { return *--end(); }
00252 };
00253 
00254 #include "SparseBitmap.tcc"
00255 
00256 } // namespace oasys
00257 
00258 #endif /* _OASYS_SPARSE_BITMAP_H_ */

Generated on Thu Jun 7 16:56:52 2007 for DTN Reference Implementation by  doxygen 1.5.1