00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_WordsBitmap_h
00023 #define moses_WordsBitmap_h
00024
00025 #include <limits>
00026 #include <vector>
00027 #include <iostream>
00028 #include <cstring>
00029 #include <cmath>
00030 #include <cstdlib>
00031 #include "TypeDef.h"
00032 #include "WordsRange.h"
00033
00034 namespace Moses
00035 {
00036 typedef unsigned long WordsBitmapID;
00037
00040 class WordsBitmap
00041 {
00042 friend std::ostream& operator<<(std::ostream& out, const WordsBitmap& wordsBitmap);
00043 protected:
00044 const size_t m_size;
00045 bool *m_bitmap;
00047 WordsBitmap();
00048
00050 void Initialize() {
00051 for (size_t pos = 0 ; pos < m_size ; pos++) {
00052 m_bitmap[pos] = false;
00053 }
00054 }
00055
00056
00057 void Initialize(std::vector<bool> vector) {
00058 size_t vector_size = vector.size();
00059 for (size_t pos = 0 ; pos < m_size ; pos++) {
00060 if (pos < vector_size && vector[pos] == true) m_bitmap[pos] = true;
00061 else m_bitmap[pos] = false;
00062 }
00063 }
00064
00065
00066 public:
00068 WordsBitmap(size_t size, std::vector<bool> initialize_vector)
00069 :m_size (size) {
00070 m_bitmap = (bool*) malloc(sizeof(bool) * size);
00071 Initialize(initialize_vector);
00072 }
00074 WordsBitmap(size_t size)
00075 :m_size (size) {
00076 m_bitmap = (bool*) malloc(sizeof(bool) * size);
00077 Initialize();
00078 }
00080 WordsBitmap(const WordsBitmap ©)
00081 :m_size (copy.m_size) {
00082 m_bitmap = (bool*) malloc(sizeof(bool) * m_size);
00083 for (size_t pos = 0 ; pos < copy.m_size ; pos++) {
00084 m_bitmap[pos] = copy.GetValue(pos);
00085 }
00086 }
00087 ~WordsBitmap() {
00088 free(m_bitmap);
00089 }
00091 size_t GetNumWordsCovered() const {
00092 size_t count = 0;
00093 for (size_t pos = 0 ; pos < m_size ; pos++) {
00094 if (m_bitmap[pos])
00095 count++;
00096 }
00097 return count;
00098 }
00099
00101 size_t GetFirstGapPos() const {
00102 for (size_t pos = 0 ; pos < m_size ; pos++) {
00103 if (!m_bitmap[pos]) {
00104 return pos;
00105 }
00106 }
00107
00108 return NOT_FOUND;
00109 }
00110
00111
00113 size_t GetLastGapPos() const {
00114 for (int pos = (int) m_size - 1 ; pos >= 0 ; pos--) {
00115 if (!m_bitmap[pos]) {
00116 return pos;
00117 }
00118 }
00119
00120 return NOT_FOUND;
00121 }
00122
00123
00125 size_t GetLastPos() const {
00126 for (int pos = (int) m_size - 1 ; pos >= 0 ; pos--) {
00127 if (m_bitmap[pos]) {
00128 return pos;
00129 }
00130 }
00131
00132 return NOT_FOUND;
00133 }
00134
00136 bool GetValue(size_t pos) const {
00137 return m_bitmap[pos];
00138 }
00140 void SetValue( size_t pos, bool value ) {
00141 m_bitmap[pos] = value;
00142 }
00144 void SetValue( size_t startPos, size_t endPos, bool value ) {
00145 for(size_t pos = startPos ; pos <= endPos ; pos++) {
00146 m_bitmap[pos] = value;
00147 }
00148 }
00150 bool IsComplete() const {
00151 return GetSize() == GetNumWordsCovered();
00152 }
00154 bool Overlap(const WordsRange &compare) const {
00155 for (size_t pos = compare.GetStartPos() ; pos <= compare.GetEndPos() ; pos++) {
00156 if (m_bitmap[pos])
00157 return true;
00158 }
00159 return false;
00160 }
00162 size_t GetSize() const {
00163 return m_size;
00164 }
00165
00167 inline int Compare (const WordsBitmap &compare) const {
00168
00169
00170
00171
00172 size_t thisSize = GetSize()
00173 ,compareSize = compare.GetSize();
00174
00175 if (thisSize != compareSize) {
00176 return (thisSize < compareSize) ? -1 : 1;
00177 }
00178 return std::memcmp(m_bitmap, compare.m_bitmap, thisSize * sizeof(bool));
00179 }
00180
00181 bool operator< (const WordsBitmap &compare) const {
00182 return Compare(compare) < 0;
00183 }
00184
00185 inline size_t GetEdgeToTheLeftOf(size_t l) const {
00186 if (l == 0) return l;
00187 while (l && !m_bitmap[l-1]) {
00188 --l;
00189 }
00190 return l;
00191 }
00192
00193 inline size_t GetEdgeToTheRightOf(size_t r) const {
00194 if (r+1 == m_size) return r;
00195 while (r+1 < m_size && !m_bitmap[r+1]) {
00196 ++r;
00197 }
00198 return r;
00199 }
00200
00201
00203 int GetFutureCosts(int lastPos) const ;
00204
00206 WordsBitmapID GetID() const {
00207 CHECK(m_size < (1<<16));
00208
00209 size_t start = GetFirstGapPos();
00210 if (start == NOT_FOUND) start = m_size;
00211
00212 size_t end = GetLastPos();
00213 if (end == NOT_FOUND) end = 0;
00214
00215 CHECK(end < start || end-start <= 16);
00216 WordsBitmapID id = 0;
00217 for(size_t pos = end; pos > start; pos--) {
00218 id = id*2 + (int) GetValue(pos);
00219 }
00220 return id + (1<<16) * start;
00221 }
00222
00224 WordsBitmapID GetIDPlus( size_t startPos, size_t endPos ) const {
00225 CHECK(m_size < (1<<16));
00226
00227 size_t start = GetFirstGapPos();
00228 if (start == NOT_FOUND) start = m_size;
00229
00230 size_t end = GetLastPos();
00231 if (end == NOT_FOUND) end = 0;
00232
00233 if (start == startPos) start = endPos+1;
00234 if (end < endPos) end = endPos;
00235
00236 CHECK(end < start || end-start <= 16);
00237 WordsBitmapID id = 0;
00238 for(size_t pos = end; pos > start; pos--) {
00239 id = id*2;
00240 if (GetValue(pos) || (startPos<=pos && pos<=endPos))
00241 id++;
00242 }
00243 return id + (1<<16) * start;
00244 }
00245
00246 TO_STRING();
00247 };
00248
00249
00250 inline std::ostream& operator<<(std::ostream& out, const WordsBitmap& wordsBitmap)
00251 {
00252 for (size_t i = 0 ; i < wordsBitmap.m_size ; i++) {
00253 out << (wordsBitmap.GetValue(i) ? 1 : 0);
00254 }
00255 return out;
00256 }
00257
00258 }
00259 #endif