Add read write functions for some std containers.

This commit is contained in:
vng 2011-10-21 15:26:04 +03:00 committed by Alex Zolotarev
parent 6fb963c435
commit 485c280e20
4 changed files with 69 additions and 28 deletions

View file

@ -75,4 +75,5 @@ HEADERS += \
trie.hpp \
trie_builder.hpp \
trie_reader.hpp \
mmap_reader.hpp \
mmap_reader.hpp \
read_write_utils.hpp \

View file

@ -1,11 +1,27 @@
#include "../base/SRC_FIRST.hpp"
#include "file_container.hpp"
#include "varint.hpp"
#include "read_write_utils.hpp"
#include "write_to_sink.hpp"
#include "internal/file_data.hpp"
template <class TSource> void Read(TSource & src, FilesContainerBase::Info & i)
{
rw::Read(src, i.m_tag);
i.m_offset = ReadVarUint<uint64_t>(src);
i.m_size = ReadVarUint<uint64_t>(src);
}
template <class TSink> void Write(TSink & sink, FilesContainerBase::Info const & i)
{
rw::Write(sink, i.m_tag);
WriteVarUint(sink, i.m_offset);
WriteVarUint(sink, i.m_size);
}
/////////////////////////////////////////////////////////////////////////////
// FilesContainerBase
/////////////////////////////////////////////////////////////////////////////
@ -18,18 +34,7 @@ void FilesContainerBase::ReadInfo(ReaderT & reader)
ReaderSource<ReaderT> src(reader);
src.Skip(offset);
uint32_t const count = ReadVarUint<uint32_t>(src);
m_info.resize(count);
for (uint32_t i = 0; i < count; ++i)
{
uint32_t const tagSize = ReadVarUint<uint32_t>(src);
m_info[i].m_tag.resize(tagSize);
src.Read(&m_info[i].m_tag[0], tagSize);
m_info[i].m_offset = ReadVarUint<uint64_t>(src);
m_info[i].m_size = ReadVarUint<uint64_t>(src);
}
rw::Read(src, m_info);
}
/////////////////////////////////////////////////////////////////////////////
@ -233,18 +238,7 @@ void FilesContainerW::Finish()
sort(m_info.begin(), m_info.end(), LessInfo());
uint32_t const count = m_info.size();
WriteVarUint(writer, count);
for (uint32_t i = 0; i < count; ++i)
{
size_t const tagSize = m_info[i].m_tag.size();
WriteVarUint(writer, tagSize);
writer.Write(&m_info[i].m_tag[0], tagSize);
WriteVarUint(writer, m_info[i].m_offset);
WriteVarUint(writer, m_info[i].m_size);
}
rw::Write(writer, m_info);
m_bFinished = true;
}

View file

@ -7,8 +7,7 @@
class FilesContainerBase
{
protected:
public:
typedef string Tag;
struct Info
@ -21,6 +20,7 @@ protected:
Info(Tag const & tag, uint64_t offset) : m_tag(tag), m_offset(offset) {}
};
protected:
struct LessInfo
{
bool operator() (Info const & t1, Info const & t2) const

View file

@ -0,0 +1,46 @@
#pragma once
#include "varint.hpp"
#include "../std/string.hpp"
#include "../std/vector.hpp"
namespace rw
{
template <class TSink>
void Write(TSink & sink, string const & s)
{
uint32_t const count = static_cast<uint32_t>(s.size());
WriteVarUint(sink, count);
if (!s.empty())
sink.Write(&s[0], count);
}
template <class TSource>
void Read(TSource & src, string & s)
{
uint32_t const count = ReadVarUint<uint32_t>(src);
s.resize(count);
if (count > 0)
src.Read(&s[0], count);
}
template <class TSink, class T>
void Write(TSink & sink, vector<T> const & v)
{
uint32_t const count = static_cast<uint32_t>(v.size());
WriteVarUint(sink, count);
for (uint32_t i = 0; i < count; ++i)
Write(sink, v[i]);
}
template <class TSource, class T>
void Read(TSource & src, vector<T> & v)
{
uint32_t const count = ReadVarUint<uint32_t>(src);
v.resize(count);
for (size_t i = 0; i < count; ++i)
Read(src, v[i]);
}
}