// TVectorInsertionsAndDeletions.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2007-2019

#pragma once
#include "TVectorInsertions.h"
#include "TVectorDeletions.h"

namespace ceda
{
template<typename S, typename T, typename I>
struct TVectorInsertionsAndDeletions
{
    using Opid = TOpid<S,T>;
    using VectorInsertions = TVectorInsertions<S,T,I>;
    using VectorDeletions = TVectorDeletions<S,T,I>;
    
    bool operator==(const TVectorInsertionsAndDeletions& rhs) const
    {
        return m_insertions == rhs.m_insertions &&
               m_deletions == rhs.m_deletions;
    }
    bool operator!=(const TVectorInsertionsAndDeletions& rhs) const { return !operator==(rhs); }

    // Called when a local insertion operation is generated to record the insertion interval.
    void LocalInsertInterval_p(const Opid& opid, I p, I n)
    {
        I q = m_deletions.OnLocalInsertInterval_p(p, n);
        m_insertions.LocalInsertInterval_q(opid, q, n);
    }

    // Called when a local deletion operation is generated to record the deletion interval.
    void LocalDeleteInterval_p(const Opid& opid, I p, I n)
    {
        m_deletions.LocalDeleteInterval_p(opid,p,n);
    }
    
    VectorInsertions m_insertions;

    // Note that rhe recorded deletions serve the purpose of recording the PtoQ map.
    // So TPtoQMap is not needed.
    VectorDeletions m_deletions;
};

template<typename Archive, typename S, typename T, typename I>
void Serialise(Archive& ar, const TVectorInsertionsAndDeletions<S,T,I>& x)
{
    ar << x.m_insertions << x.m_deletions;
}

template<typename Archive, typename S, typename T, typename I>
void Deserialise(Archive& ar, TVectorInsertionsAndDeletions<S,T,I>& x)
{
    ar >> x.m_insertions >> x.m_deletions;
}

template<typename S, typename T, typename I>
xostream& operator<<(xostream& os, const TVectorInsertionsAndDeletions<S,T,I>& x)
{
    os << "(i=" << x.m_insertions
       << " d=" << x.m_deletions
       << ')';
	return os;
}

} // namespace ceda

