00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef _WordPermute_h
00038 #define _WordPermute_h
00039
00040 #include <WordExcludeMask.h>
00041
00042
00043
00044 #define WORD_PERMUTE_OK WORD_EXCLUDE_OK
00045 #define WORD_PERMUTE_END WORD_EXCLUDE_END
00046
00047
00048
00049
00050 #define WORD_PERMUTE_PROXIMITY_NO 0
00051 #define WORD_PERMUTE_PROXIMITY_TOGGLE 1
00052 #define WORD_PERMUTE_PROXIMITY_ONLY 2
00053
00054
00055
00056
00057
00058 class WordPermute : public WordExcludeMask {
00059 public:
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 virtual inline int Initialize(unsigned int length, unsigned int ignore, unsigned int ignore_mask_arg, int nuse_proximity) {
00074 if(WordExcludeMask::Initialize(length, ignore, ignore_mask_arg, 0) != OK)
00075 return NOTOK;
00076
00077 use_proximity = nuse_proximity;
00078 switch(use_proximity) {
00079 case WORD_PERMUTE_PROXIMITY_NO:
00080 proximity = 0;
00081 break;
00082 case WORD_PERMUTE_PROXIMITY_TOGGLE:
00083
00084
00085
00086
00087 proximity = (WordExcludeMask::Maxi() - WordExcludeMask::ExcludedCount()) > 1;
00088 break;
00089 case WORD_PERMUTE_PROXIMITY_ONLY:
00090 proximity = 1;
00091 break;
00092 default:
00093 fprintf(stderr, "WordPermute::Initialize: unexpected use_proximity = %d\n", use_proximity);
00094 return NOTOK;
00095 }
00096 return OK;
00097 }
00098
00099
00100
00101
00102
00103 inline int Proximity() { return proximity; }
00104
00105
00106
00107
00108
00109 inline int UseProximity() { return use_proximity; }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 virtual int Next() {
00121 if(WordExcludeMask::Maxi() <= 1)
00122 return WORD_PERMUTE_END;
00123
00124 int ret = WORD_PERMUTE_OK;
00125 int check_useless = 0;
00126 if(use_proximity == WORD_PERMUTE_PROXIMITY_TOGGLE) {
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 if(proximity) {
00137 proximity = 0;
00138 } else {
00139 proximity = 1;
00140 if((ret = WordExcludeMask::Next()) == WORD_PERMUTE_OK) {
00141
00142
00143
00144 if(NotExcludedCount() <= 1)
00145 proximity = 0;
00146 check_useless = 1;
00147 } else if(ret == WORD_PERMUTE_END)
00148 proximity = 0;
00149 }
00150 } else {
00151 ret = WordExcludeMask::Next();
00152 check_useless = 1;
00153 }
00154
00155 if(check_useless && ret == WORD_PERMUTE_OK) {
00156
00157
00158
00159
00160
00161
00162 if(NotExcludedCount() <= 0) {
00163 ret = WordExcludeMask::Next();
00164 if(ret != WORD_PERMUTE_END) {
00165 fprintf(stderr, "WordPermute::Next: expected WORD_PERMUTE_END\n");
00166 ret = NOTOK;
00167 }
00168 }
00169 }
00170
00171 return ret;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180 virtual inline void Get(String& buffer) const {
00181 WordExcludeMask::Get(buffer);
00182 if(use_proximity == WORD_PERMUTE_PROXIMITY_TOGGLE)
00183 buffer << (proximity ? 'T' : 'F');
00184 }
00185
00186
00187
00188
00189
00190
00191 virtual inline int Set(const String& buffer) {
00192 if(buffer.length() < 1) {
00193 fprintf(stderr, "WordPermute::Set: buffer length < 1\n");
00194 return NOTOK;
00195 }
00196 int ret = OK;
00197 if(use_proximity == WORD_PERMUTE_PROXIMITY_TOGGLE) {
00198 if((ret = WordExcludeMask::Set(buffer.sub(0, buffer.length() - 1))) == OK)
00199 proximity = buffer.last() == 'T';
00200 } else {
00201 ret = WordExcludeMask::Set(buffer);
00202 }
00203
00204 return ret;
00205 }
00206
00207 protected:
00208 int use_proximity;
00209 int proximity;
00210 };
00211
00212 #endif