wirate
December 27th, 2011, 04:32 PM
#include <fstream>
#include <iostream>
using namespace std;
bool find_in_file(char*);
void insert_in_file(char*);
inline bool isNull(char* word);
int main()
{
char word[25];
for(int i = 0; i < 10; i++)
{
cin >> word;
if( find_in_file(word) )
cout << "found" << endl;
else
insert_in_file(word);
}
system("pause");
}
bool find_in_file(char* word)
{
ifstream file;
file.open("file.dat", ios::in);
char contents[655][25] = {0};
file.read(reinterpret_cast<char*>(contents), 16*1024);
file.close();
int i = 0;
while( !isNull(contents[i]) )
{
if( strcmp(contents[i], word) == 0)
return true;
if( strcmp(contents[i], word) < 0 )
i = 2*i + 2;
else
i = 2*i + 1;
}
return false;
}
void insert_in_file(char* word)
{
fstream file;
file.open("file.dat", ios::in | ios::binary);
char contents[655][25] = {0};
file.read(reinterpret_cast<char*>(contents), 16*1024);
file.close();
file.open("file.dat", ios::in | ios::out | ios::binary);
if( isNull(contents[0]) )
{
file.write(word, 25);
file.close();
return;
}
int parent;
int current = 0;
while( !isNull(contents[current]) )
{
parent = current;
if( strcmp( contents[current], word ) < 0 )
current = current*2 + 2;
else if ( strcmp( contents[current], word ) > 0)
current = current*2 + 1;
else
return;
}
int insertAt;
if( strcmp(contents[parent], word ) < 0 )
insertAt = parent*2 + 2;
else
insertAt = parent*2 + 1;
file.seekp(insertAt*25, ios_base::beg);
file.write(reinterpret_cast<const char*>(word), 25);
file.close();
}
inline bool isNull(char* word)
{
return word[0] == 0;
}
The above code (I am the ashamed author :( )implements a binary search tree on file. It assumes a size of around 16K as max for the file. The tree is stored in this format:
0 root
1 left child of root - L
2 right child of root - R
3 left child of L - LL
4 right child of L - LR
5 left child of R - RL
6 right child of R - RR
and so on. In the absence of a child, an empty node is inserted. Now I have to do the same thing on PHP. How is it possible since as far as I know, PHP does not provide binary file access. Eagerly looking forward to your responses :)
#include <iostream>
using namespace std;
bool find_in_file(char*);
void insert_in_file(char*);
inline bool isNull(char* word);
int main()
{
char word[25];
for(int i = 0; i < 10; i++)
{
cin >> word;
if( find_in_file(word) )
cout << "found" << endl;
else
insert_in_file(word);
}
system("pause");
}
bool find_in_file(char* word)
{
ifstream file;
file.open("file.dat", ios::in);
char contents[655][25] = {0};
file.read(reinterpret_cast<char*>(contents), 16*1024);
file.close();
int i = 0;
while( !isNull(contents[i]) )
{
if( strcmp(contents[i], word) == 0)
return true;
if( strcmp(contents[i], word) < 0 )
i = 2*i + 2;
else
i = 2*i + 1;
}
return false;
}
void insert_in_file(char* word)
{
fstream file;
file.open("file.dat", ios::in | ios::binary);
char contents[655][25] = {0};
file.read(reinterpret_cast<char*>(contents), 16*1024);
file.close();
file.open("file.dat", ios::in | ios::out | ios::binary);
if( isNull(contents[0]) )
{
file.write(word, 25);
file.close();
return;
}
int parent;
int current = 0;
while( !isNull(contents[current]) )
{
parent = current;
if( strcmp( contents[current], word ) < 0 )
current = current*2 + 2;
else if ( strcmp( contents[current], word ) > 0)
current = current*2 + 1;
else
return;
}
int insertAt;
if( strcmp(contents[parent], word ) < 0 )
insertAt = parent*2 + 2;
else
insertAt = parent*2 + 1;
file.seekp(insertAt*25, ios_base::beg);
file.write(reinterpret_cast<const char*>(word), 25);
file.close();
}
inline bool isNull(char* word)
{
return word[0] == 0;
}
The above code (I am the ashamed author :( )implements a binary search tree on file. It assumes a size of around 16K as max for the file. The tree is stored in this format:
0 root
1 left child of root - L
2 right child of root - R
3 left child of L - LL
4 right child of L - LR
5 left child of R - RL
6 right child of R - RR
and so on. In the absence of a child, an empty node is inserted. Now I have to do the same thing on PHP. How is it possible since as far as I know, PHP does not provide binary file access. Eagerly looking forward to your responses :)