Skip to content

Instantly share code, notes, and snippets.

@jarodl
Created October 20, 2009 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarodl/214698 to your computer and use it in GitHub Desktop.
Save jarodl/214698 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define DAYS_IN_YEAR 360 // In order to simplify the program
#define DAYS 30 // use 30 days in each month.
#define MONTHS 12 // This still gives a rough estimate on how
// well the hashing functions work.
int hash1(int month, int day);
int hash2(int month, int day);
string formatDate(int month, int day);
int main()
{
// for hash1
string year1[DAYS_IN_YEAR];
// for hash2
string year2[DAYS_IN_YEAR];
int numOfDays = 0;
int collisions1 = DAYS_IN_YEAR;
int collisions2 = DAYS_IN_YEAR;
// clear both arrays
for ( int i = 0; i < DAYS_IN_YEAR; i++ )
year1[i] = year2[i] = "";
for ( int i = 1; i <= MONTHS; i++ ) {
for ( int j = 1; j <= DAYS; j++ ) {
year1[ hash1(i, j) ] = formatDate(i, j);
year2[ hash2(i, j) ] = formatDate(i, j);
}
}
// total collisions for hash1
for ( int i = 0; i < DAYS_IN_YEAR; i++ ) {
if ( year1[i] != "" )
numOfDays += 1;
}
collisions1 -= numOfDays;
numOfDays = 0;
// total collisions for hash2
for ( int i = 0; i < DAYS_IN_YEAR; i++ ) {
if ( year2[i] != "" )
numOfDays += 1;
}
collisions2 -= numOfDays;
//for ( int i = 0; i < DAYS_IN_YEAR; i++ ) {
// cout << "index: " << i << '\t' << year1[i] << endl;
cout << "hash1" << '\t' << "hash2" << endl
<< "-------" << '\t' << "-------" << endl
<< collisions1 << '\t' << collisions2 << endl;
return 0;
}
int hash1(int month, int day)
{
return ( ( month + (3 * day) ) % 101 );
}
int hash2(int month, int day)
{
return ( ( day + ( 3 * month ) ) % 101 );
}
string formatDate(int month, int day)
{
stringstream ss;
ss << month << "/" << day;
return ( ss.str() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment