1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| #include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
using namespace std;
#define FILTER_SIZE 10
#define NUM_HASHES 7
#define WORD_BUF_SIZE 32
#define FILTER_SIZE_BYTES (1 << (FILTER_SIZE - 3))
#define FILTER_BITMASK ((1 << FILTER_SIZE) - 1)
/* function de hashage */
unsigned int RSHash (unsigned char *, unsigned int);
unsigned int DJBHash (unsigned char *, unsigned int);
unsigned int FNVHash (unsigned char *, unsigned int);
unsigned int JSHash (unsigned char *, unsigned int);
unsigned int PJWHash (unsigned char *, unsigned int);
unsigned int SDBMHash(unsigned char *, unsigned int);
unsigned int DEKHash (unsigned char *, unsigned int);
/* fonction de help */
void Verifir_Sys(unsigned char[], char *file,char *file1);
void insert_word(unsigned char[], string word);
int in_dict(unsigned char[], string word);
void get_hashes(unsigned int[], string word);
int main(int argc, char *argv[])
{
unsigned char filter[FILTER_SIZE_BYTES];
if(argc < 3){
cout<<"le nombre de parametre a fournir au fonction est insuffisant"<<endl;
}
else
Verifir_Sys(filter,argv[1],argv[2]);
return 1;
}
void Verifir_Sys(unsigned char filter[], char *filename,char *filename1)
{
string AdrePortSour;
string AdrePortDest;
int test;
fstream fichier(filename);
fstream fichiersysAck(filename1);
if ( !fichier ) { // si le fichier n'est pas trouv�
cout << "fichier inexistant";
} else { // sinon
bool continuer = true; // indicateur de fin de fichier
while( continuer ) {
string ch; // chaine contenant une ligne du fichier
fichier >> ch; // ranger une ligne dans ch
if ( ch != "" ){
if(ch=="S"){ //si la ligne n'est pas vide faire
fichier >> ch;
AdrePortSour=AdrePortSour+ch;
fichier >> ch;
AdrePortSour=AdrePortSour+" "+ch;
cout<<AdrePortSour<<endl;
AdrePortSour="";
insert_word(filter,AdrePortSour);
}
if(ch=="SA"){ //si la ligne n'est pas vide faire
fichier >> ch;
fichier >> ch;
fichier >> ch;
fichier >> ch;
AdrePortDest=AdrePortDest+ch;
fichier >> ch;
AdrePortDest=AdrePortDest+" "+ch;
cout<<AdrePortDest<<endl;
AdrePortDest="";
test=in_dict(filter,AdrePortDest);
if(test==0)
fichiersysAck <<AdrePortDest;
}
}
else // sinon
continuer = false; // sortir de la boucle de lecture
}
}
}
void insert_word(unsigned char filter[], string str)
{
unsigned int hash[NUM_HASHES];
int i;
get_hashes(hash, str);
for (i = 0; i < NUM_HASHES; i++) {
hash[i] = (hash[i] >> FILTER_SIZE) ^
(hash[i] & FILTER_BITMASK);
filter[hash[i] >> 3] |= 1 << (hash[i] & 7);
}
}
int in_dict(unsigned char filter[], string str)
{
unsigned int hash[NUM_HASHES];
int i;
get_hashes(hash, str);
for (i = 0; i < NUM_HASHES; i++) {
hash[i] = (hash[i] >> FILTER_SIZE) ^
(hash[i] & FILTER_BITMASK);
if (!(filter[hash[i] >> 3] & (1 << (hash[i] & 7))))
return 0;
}
return 1;
}
void get_hashes(unsigned int hash[], string in)
{
unsigned char *str = (unsigned char *)in;//voici ce que je veux changer par sa equivalente en string
int pos = strlen(in);// et celle la aussi convertir un sring en entier
hash[0] = RSHash (str, pos);
hash[1] = DJBHash (str, pos);
hash[2] = FNVHash (str, pos);
hash[3] = JSHash (str, pos);
hash[4] = PJWHash (str, pos);
hash[5] = SDBMHash(str, pos);
hash[6] = DEKHash (str, pos);
}
/****************\
| Hash Functions |
\****************/
unsigned int RSHash(unsigned char *str, unsigned int len)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = hash * a + (*str);
a = a * b;
}
return hash;
}
unsigned int JSHash(unsigned char *str, unsigned int len)
{
unsigned int hash = 1315423911;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash ^= ((hash << 5) + (*str) + (hash >> 2));
}
return hash;
}
unsigned int PJWHash(unsigned char *str, unsigned int len)
{
const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
const unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
const unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
const unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (hash << OneEighth) + (*str);
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return hash;
}
unsigned int SDBMHash(unsigned char *str, unsigned int len)
{
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = (*str) + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
unsigned int DJBHash(unsigned char *str, unsigned int len)
{
unsigned int hash = 5381;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
unsigned int DEKHash(unsigned char *str, unsigned int len)
{
unsigned int hash = len;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
}
return hash;
}
unsigned int FNVHash(unsigned char *str, unsigned int len)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash *= fnv_prime;
hash ^= (*str);
}
return hash;
} |
Partager