Space optimisation

Hi I am writing a program for a variant of LZW. I have implemented the algorithm correctly but the compression rotio is not good and some times it is incresing the size of the file then reducing..I am coping the code here ..Please suggest me how I can make it efficient.
Thanks

#include <string>
#include <map>
// Compress a string to a list of output symbols.
// The result will be written to the output iterator
// starting at "result";the final iterator is returned.
using namespace std;

template <typename Iterator>
Iterator compress(const std::string &uncompressed, Iterator result) {
// Build the dictionary.
int dictSize = 256;

std::map<std::string, int> dictionary;

for (int i = 0; i < 256; i++)
dictionary[std::string(1, i)] = i;
std::string w;
for (std::string::const_iterator it = uncompressed.begin();
it != uncompressed.end(); ++it) {
char c = *it;
std::string wc = w + c;
if (dictionary.count(wc))
w = wc;
else {
*result++ = dictionary[w];
// Add wc to the dictionary.
dictionary[wc] = dictSize++;
w = std::string(1, c);
}
}
// Output the code for w.
if (!w.empty())
*result++ = dictionary[w];
return result;
}

#include <iostream>
#include <iterator>
#include <vector>
#include<fstream.h>
#include <iosfwd>
#include<bitset>

int main() {
std::string newstr = "";
string input_file_name;
cout << "enter file name :";
cin >> input_file_name;
int dot_pos = input_file_name.find( '.' );
string base ( input_file_name.substr( 0, dot_pos ) );


std::ifstream file_in;

file_in.open(input_file_name.c_str());

while (!file_in.eof()) {
char c = file_in.get();
newstr.push_back(c);
}


std::vector<int> compressed;
compress(newstr, std::back_inserter(compressed));

std::ofstream file_out("test1", ios::binary);


std::vector<int>::iterator i;

for (i = compressed.begin(); i < compressed.end() - 1; ++i) {

file_out <<*i;
file_out << endl;
}
return 0;
}
I've not catch your idea, but I did some optimization with backward compatibility
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
#include <iosfwd>
#include <iostream>
#include <fstream>

#include <string>
#include <map>
#include <vector>
#include <bitset>

#include <algorithm>
#include <iterator>

// Compress a string to a list of output symbols.
// The result will be written to the output iterator
// starting at "result";the final iterator is returned.
template <typename Iterator>
void compress(const std::string &uncompressed, Iterator result) {
    // Build the dictionary.
    int dictSize = 256;

    typedef std::map<std::string, int> StringToInt;
     StringToInt dictionary;

    for (int i = 0; i < 256; i++)
        dictionary.insert(StringToInt::value_type(std::string(1, i), i));

    std::string w;
    for (std::string::const_iterator it(uncompressed.begin()), end(uncompressed.end()); it != end; ++it) {
        char c(*it);
        std::string wc(w + c);

        StringToInt::iterator i(dictionary.lower_bound(wc));
        if (i != dictionary.end() && i->first == wc) {
            w = wc;
        } else {
            *result++ = dictionary[w];
            // Add wc to the dictionary.
            dictionary[wc] = dictSize++;
            w = std::string(1, c);
        }
    }
    // Output the code for w.
    if (!w.empty())
        *result++ = dictionary[w];
}

int main() {
    std::cout << "enter file name :";
    std::string input_file_name("");
    std::cin >> input_file_name;

    std::string newstr((std::istreambuf_iterator<char>(std::ifstream(input_file_name.c_str()))), std::istreambuf_iterator<char>());

    std::vector<int> compressed;
    compressed.reserve(newstr.size());
    compress(newstr, std::back_inserter(compressed));

    std::copy(compressed.begin(), compressed.end(), std::ostream_iterator <int>(std::ofstream("test1", std::ios::binary), "\n"));
    return 0;
}
Topic archived. No new replies allowed.