00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifdef HAVE_CONFIG_H
00011 #include <config.h>
00012 #endif
00013
00014 #ifdef HAVE_UNISTD_H
00015 #include <unistd.h>
00016 #endif
00017
00018 #include <htString.h>
00019
00020 #include <WordExclude.h>
00021
00022
00023
00024
00025 static inline void show_bits(unsigned int result)
00026 {
00027 int i;
00028 for(i = 0; i < 10; i++) {
00029 fprintf(stderr, "%c", (result & (1 << i)) ? '1' : '0');
00030 }
00031 fprintf(stderr, " (0x%08x - %15d)\n", result, result);
00032 }
00033
00034 int WordExclude::Initialize(unsigned int length, unsigned int, unsigned int, int)
00035 {
00036 if(length > WORD_EXCLUDE_MAX) {
00037 fprintf(stderr, "WordExclude::Initialize: length must be < %d\n", WORD_EXCLUDE_MAX);
00038 return NOTOK;
00039 }
00040
00041 mask = 0;
00042 bits = 0;
00043 maxi = length;
00044
00045 return OK;
00046 }
00047
00048 inline unsigned int WordExclude::Permute(unsigned int mask, unsigned int bits)
00049 {
00050 unsigned int bits_cleared = 0;
00051 unsigned int j;
00052 for(j = 0; j < bits; j++) {
00053 if(mask & (1 << j)) {
00054 bits_cleared++;
00055 mask &= ~(1 << j);
00056 } else {
00057 if(bits_cleared) {
00058 bits_cleared--;
00059 mask |= (1 << j);
00060 break;
00061 }
00062 }
00063 }
00064
00065 if(j >= bits)
00066 return 0;
00067
00068 for(j = 0; j < bits_cleared; j++)
00069 mask |= (1 << j);
00070
00071 return mask;
00072 }
00073
00074 int WordExclude::Next()
00075 {
00076 mask = Permute(mask, maxi);
00077
00078 int ret = WORD_EXCLUDE_OK;
00079
00080 if(mask == 0) {
00081 bits++;
00082 if(bits > maxi)
00083 ret = WORD_EXCLUDE_END;
00084 else {
00085 unsigned int i;
00086 for(i = 0; i < bits; i++)
00087 mask |= (1 << i);
00088 ret = WORD_EXCLUDE_OK;
00089 }
00090 }
00091
00092 if(verbose > 2) show_bits(mask);
00093
00094 return ret;
00095 }
00096
00097 void WordExclude::Get(String& buffer) const
00098 {
00099 buffer.trunc();
00100 unsigned int i;
00101 for(i = 0; i < maxi; i++) {
00102 buffer << ((mask & (1 << i)) ? '1' : '0');
00103 }
00104 }
00105
00106 int WordExclude::Set(const String& buffer)
00107 {
00108 if(Initialize(buffer.length(), 0, 0, 0) == NOTOK)
00109 return NOTOK;
00110 unsigned int i;
00111 for(i = 0; i < maxi; i++) {
00112 if(buffer[i] == '1') {
00113 mask |= (1 << i);
00114 bits++;
00115 }
00116 }
00117 return OK;
00118 }
00119