coin::SpaceSaving<T>::~SpaceSaving(){
	Bucket<T> *bucket = least_bucket_;
	while(bucket->next() != least_bucket_){
		Element<T> *child = bucket->child();
		while(child->next() != bucket->child()){
			Element<T> *prev_child = child;
			child = child->next();
			delete prev_child;
		}
		Bucket<T> *prev_bucket = bucket;
		bucket = bucket->next();
		delete prev_bucket;
	}
};
void coin::WordCounter::write_frequencies(const string& file, int minimum) const{
	ofstream out(file, ios::out|ios::binary|ios::trunc);
	double total = saving_->total();
	out.write((char *)&total, sizeof(double));
    Bucket<string> *bucket = saving_->least_bucket();
    int64_t counter = 0;
    do{
        bucket = bucket->prev();
		if(bucket->count() < minimum){
			bucket = bucket->next();
			break;
		}
        Element<string> *child = bucket->child();
        map<string, Element<string>*> children;
        do{
        	children[child->item()] = child;
            child = child->next();
            ++counter;
        }while(child != bucket->child());
        int childs = children.size();
        out.write((char *)&childs, sizeof(int));
        double count = bucket->count();
        out.write((char *)&count, sizeof(double));
        for(pair<string, Element<string>*> child:children){
        	short size = child.first.size();
         	out.write((char *)&size, sizeof(short));
        	out.write(child.first.c_str(), sizeof(char) * size);
        }
        if(counter > saving_->max_count()){
        	break;
        }
    }while(bucket != saving_->least_bucket());
	for(pair<string, double> dict_count:dict_counts_){
		int childs = 1;
        out.write((char *)&childs, sizeof(int));
        out.write((char *)&dict_count.second, sizeof(double));
		short size = dict_count.first.size();
		out.write((char *)&size, sizeof(short));
		out.write(dict_count.first.c_str(), sizeof(char) * size);
	}
	out.close();
    cerr << counter << " words with at least " << bucket->count() << " frequencies are written" << endl;
}