/* summaryio.h
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2020, 2021 Askar
    Almatov

    This file is part of Trafficmeter.

    Trafficmeter is free software: you can redistribute it and/or modify it under the terms of the GNU General
    Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    Trafficmeter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
    implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
    License for more details.

    You should have received a copy of the GNU General Public License along with Trafficmeter. If not, see
    <http://www.gnu.org/licenses/>.
*/

#ifndef __SUMMARYIO_H
#define __SUMMARYIO_H 1

#include <cstdint>

namespace trafficmeter
{
    /**********************************************************************************************************/
    class SummaryIo
    {
    public:
                                        SummaryIo();

                                        SummaryIo
                                        (
                                            uintmax_t  nFlowsIn,
                                            uintmax_t  nPacketsIn,
                                            uintmax_t  nBytesIn,
                                            uintmax_t  nFlowsOut,
                                            uintmax_t  nPacketsOut,
                                            uintmax_t  nBytesOut
                                        );

        SummaryIo&                      operator+=( const SummaryIo& );
        uintmax_t                       nFlowsIn() const;
        uintmax_t                       nPacketsIn() const;
        uintmax_t                       nBytesIn() const;
        uintmax_t                       nFlowsOut() const;
        uintmax_t                       nPacketsOut() const;
        uintmax_t                       nBytesOut() const;
        int                             compareFlows( const SummaryIo& ) const;
        int                             comparePackets( const SummaryIo& ) const;
        int                             compareBytes( const SummaryIo& ) const;

    protected:
        uintmax_t                       nFlowsIn_;
        uintmax_t                       nPacketsIn_;
        uintmax_t                       nBytesIn_;
        uintmax_t                       nFlowsOut_;
        uintmax_t                       nPacketsOut_;
        uintmax_t                       nBytesOut_;
    };
}

/*
****************************************************************************************************************
****************************************************************************************************************
*/

namespace trafficmeter
{
    //----------------------------------------------------------------------------------------------------------
    inline
    SummaryIo::SummaryIo() :
        nFlowsIn_( 0 ),
        nPacketsIn_( 0 ),
        nBytesIn_( 0 ),
        nFlowsOut_( 0 ),
        nPacketsOut_( 0 ),
        nBytesOut_( 0 )
    {
    }

    //----------------------------------------------------------------------------------------------------------
    inline
    SummaryIo::SummaryIo
    (
        uintmax_t   nFlowsIn,
        uintmax_t   nPacketsIn,
        uintmax_t   nBytesIn,
        uintmax_t   nFlowsOut,
        uintmax_t   nPacketsOut,
        uintmax_t   nBytesOut
    ) :
        nFlowsIn_( nFlowsIn ),
        nPacketsIn_( nPacketsIn ),
        nBytesIn_( nBytesIn ),
        nFlowsOut_( nFlowsOut ),
        nPacketsOut_( nPacketsOut ),
        nBytesOut_( nBytesOut )
    {
    }

    //----------------------------------------------------------------------------------------------------------
    inline SummaryIo&
    SummaryIo::operator+=( const SummaryIo& s )
    {
        nFlowsIn_ += s.nFlowsIn_;
        nPacketsIn_ += s.nPacketsIn_;
        nBytesIn_ += s.nBytesIn_;
        nFlowsOut_ += s.nFlowsOut_;
        nPacketsOut_ += s.nPacketsOut_;
        nBytesOut_ += s.nBytesOut_;
        return *this;
    }

    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    SummaryIo::nFlowsIn() const
    {
        return nFlowsIn_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    SummaryIo::nPacketsIn() const
    {
        return nPacketsIn_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    SummaryIo::nBytesIn() const
    {
        return nBytesIn_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    SummaryIo::nFlowsOut() const
    {
        return nFlowsOut_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    SummaryIo::nPacketsOut() const
    {
        return nPacketsOut_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    SummaryIo::nBytesOut() const
    {
        return nBytesOut_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline int
    SummaryIo::compareFlows( const SummaryIo& s ) const
    {
        uintmax_t   lhsMax = ( nFlowsIn_ < nFlowsOut_ )? nFlowsOut_ : nFlowsIn_;
        uintmax_t   rhsMax = ( s.nFlowsIn_ < s.nFlowsOut_ )? s.nFlowsOut_ : s.nFlowsIn_;

        if ( lhsMax != rhsMax )
        {
            return ( lhsMax < rhsMax )? -1 : 1;
        }

        uintmax_t   lhsSum = nFlowsIn_ + nFlowsOut_;
        uintmax_t   rhsSum = s.nFlowsIn_ + s.nFlowsOut_;

        if ( lhsSum != rhsSum )
        {
            return ( lhsSum < rhsSum )? -1 : 1;
        }

        return 0;
    }

    //----------------------------------------------------------------------------------------------------------
    inline int
    SummaryIo::comparePackets( const SummaryIo& s ) const
    {
        uintmax_t   lhsMax = ( nPacketsIn_ < nPacketsOut_ )? nPacketsOut_ : nPacketsIn_;
        uintmax_t   rhsMax = ( s.nPacketsIn_ < s.nPacketsOut_ )? s.nPacketsOut_ : s.nPacketsIn_;

        if ( lhsMax != rhsMax )
        {
            return ( lhsMax < rhsMax )? -1 : 1;
        }

        uintmax_t   lhsSum = nPacketsIn_ + nPacketsOut_;
        uintmax_t   rhsSum = s.nPacketsIn_ + s.nPacketsOut_;

        if ( lhsSum != rhsSum )
        {
            return ( lhsSum < rhsSum )? -1 : 1;
        }

        return 0;
    }

    //----------------------------------------------------------------------------------------------------------
    inline int
    SummaryIo::compareBytes( const SummaryIo& s ) const
    {
        uintmax_t   lhsMax = ( nBytesIn_ < nBytesOut_ )? nBytesOut_ : nBytesIn_;
        uintmax_t   rhsMax = ( s.nBytesIn_ < s.nBytesOut_ )? s.nBytesOut_ : s.nBytesIn_;

        if ( lhsMax != rhsMax )
        {
            return ( lhsMax < rhsMax )? -1 : 1;
        }

        uintmax_t   lhsSum = nBytesIn_ + nBytesOut_;
        uintmax_t   rhsSum = s.nBytesIn_ + s.nBytesOut_;

        if ( lhsSum != rhsSum )
        {
            return ( lhsSum < rhsSum )? -1 : 1;
        }

        return 0;
    }
}

#endif  // __SUMMARYIO_H
