Is there any way to serialize the type of sparse_hash_map<char *, int> to file?

338 views Asked by At

I tried to code to serialize the type of sparse_hash_map to file, but somehow it just didn't work, compelling it tells me this error message:

/usr/local/include/sparsehash/sparsetable:1763:13: error: no matching function for call to object of type 'CharPointerToIntSerializer'

  if ( !serializer(fp, &*it) )  return false;

        ^~~~~~~~~~

it seems that mymap.serialize method works just fine, it's mymap2.unsirialize method that fails, any ideas about problems in this codes below?

#include <iostream>
#include <sparsehash/sparse_hash_map>
using google::sparse_hash_map;      // namespace where class lives by default

using namespace std;

#define SIZE 13

struct CharPointerToIntSerializer {
  bool operator()(FILE* fp, std::pair<char *, int>* value) const {
    if (fread(value->first, SIZE, 1, fp) != 1) {
      return false;
    }
    if (fread(&(value->second), sizeof(value->second), 1, fp) != 1)
      return false;
    return true;
  }

  // bool operator()(FILE* fp, const std::pair<const char *, int>& value) const {
  bool operator()(FILE* fp, const std::pair<char *, int>& value) const {
    for(int i = 0; i < SIZE; i++){
      if (fwrite(value.first + i, 1, 1, fp) != 1)
    return false;
    }

    if (fwrite(&value.second, sizeof(value.second), 1, fp) != 1)
      return false;
    return true;
  }
};

int main(){
  sparse_hash_map<char*, int> old_map,new_map;
  char *p1, *p2;
  p1 = (char *) malloc(10);
  p2 = (char *) malloc(10);
  strcpy(p1, "hello");
  strcpy(p2, "world");
  old_map[p1] = 1;
  old_map[p2] = 2;

  FILE* fp = fopen("hashtable.txt", "w");
  old_map.serialize(CharPointerToIntSerializer(), fp);
  cout << old_map[p1] << endl;
  fclose(fp);

  FILE* fp_in = fopen("hashtable.txt", "r");
  new_map.unserialize(CharPointerToIntSerializer(), fp_in);
  fclose(fp_in);
  assert(old_map == new_map);
  cout << new_map[p2] << endl;
}
1

There are 1 answers

3
sehe On BEST ANSWER

The value_type of the sparse map is std::pair<K const, V>. So,

bool operator()(FILE* fp, std::pair<char*, int>* value) const;
bool operator()(FILE* fp, std::pair<char*, int> const& value) const;

should be

bool operator()(FILE* fp, std::pair<char* const, int>* value) const;
bool operator()(FILE* fp, std::pair<char* const, int> const& value) const;

respectively.

That compiles, against sparsehash 1.12 (Ubuntu comes with v1.10 packaged!)