2013-12-28 00:45:40 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
#include "util/serializer.h"
|
|
|
|
|
|
|
|
#ifndef LEAN_OBJECT_SERIALIZER_BUCKET_SIZE
|
|
|
|
#define LEAN_OBJECT_SERIALIZER_BUCKET_SIZE 8
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
/**
|
|
|
|
\brief Helper class for serializing objects.
|
|
|
|
*/
|
|
|
|
template<class T, class HashFn, class EqFn>
|
|
|
|
class object_serializer : public serializer::extension {
|
|
|
|
std::unordered_map<T, unsigned, HashFn, EqFn> m_table;
|
|
|
|
public:
|
|
|
|
object_serializer(HashFn const & h = HashFn(), EqFn const & e = EqFn()):m_table(LEAN_OBJECT_SERIALIZER_BUCKET_SIZE, h, e) {}
|
2013-12-30 03:59:53 +00:00
|
|
|
|
2013-12-28 00:45:40 +00:00
|
|
|
template<typename F>
|
2013-12-30 03:59:53 +00:00
|
|
|
void write_core(T const & v, char k, F && f) {
|
2013-12-28 00:45:40 +00:00
|
|
|
auto it = m_table.find(v);
|
|
|
|
serializer & s = get_owner();
|
|
|
|
if (it == m_table.end()) {
|
2013-12-30 03:59:53 +00:00
|
|
|
s.write_char(k + 1);
|
2013-12-28 00:45:40 +00:00
|
|
|
f();
|
|
|
|
m_table.insert(std::make_pair(v, m_table.size()));
|
|
|
|
} else {
|
2013-12-30 03:59:53 +00:00
|
|
|
s.write_char(0);
|
2013-12-28 00:45:40 +00:00
|
|
|
s.write_unsigned(it->second);
|
|
|
|
}
|
|
|
|
}
|
2013-12-30 03:59:53 +00:00
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void write(T const & v, F && f) {
|
|
|
|
write_core(v, 0, f);
|
|
|
|
}
|
2013-12-28 00:45:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Helper class for deserializing objects.
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
class object_deserializer : public deserializer::extension {
|
|
|
|
std::vector<T> m_table;
|
|
|
|
public:
|
|
|
|
template<typename F>
|
2013-12-30 03:59:53 +00:00
|
|
|
T read_core(F && f) {
|
2013-12-28 00:45:40 +00:00
|
|
|
deserializer & d = get_owner();
|
2013-12-30 03:59:53 +00:00
|
|
|
char c = d.read_char();
|
|
|
|
if (c > 0) {
|
|
|
|
T r = f(c-1);
|
2013-12-28 00:45:40 +00:00
|
|
|
m_table.push_back(r);
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
unsigned i = d.read_unsigned();
|
2013-12-31 01:58:20 +00:00
|
|
|
if (i >= m_table.size())
|
2013-12-31 02:05:38 +00:00
|
|
|
throw_corrupted_file();
|
2013-12-28 00:45:40 +00:00
|
|
|
return m_table[i];
|
|
|
|
}
|
|
|
|
}
|
2013-12-30 03:59:53 +00:00
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
T read(F && f) {
|
|
|
|
return read_core([&](char ) { return f(); });
|
|
|
|
}
|
2013-12-28 00:45:40 +00:00
|
|
|
};
|
|
|
|
}
|