/*****************************************************************************
 * $LastChangedDate: 2009-12-28 02:30:09 -0500 (Mon, 28 Dec 2009) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Multivar class.
 *//*
 * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#ifndef BASE_MULTIVAR_HH
#define BASE_MULTIVAR_HH 1

#include <algorithm>

namespace base {

////////////////////////////////////////////////////////////////////////////////
/// @brief A variable with multiple values of the same type.
///
/// Tested by base/tests/src/multivar.cc
///
template<typename T=int>
class Multivar
{
public:
    Multivar( void ) { }
    ~Multivar() { }

    /// Clear all values.
    void
    Clear( void )
    {
        mValues.clear();
    }

    /// Remove value.
    void
    Remove( T value )
    {
        mValues.remove( value );
    }

    /// Replace all values with this value (replacement).
    void
    Set( T value )
    {
        mValues.clear();
        mValues.push_back( value );
    }

    /// Add an additional value (additive).
    void
    Add( T value )
    {
        // Add value unless present.
        if ( not Query(value) )
            mValues.push_back( value );
    }

    /// Add or remove value.
    void
    Toggle( T value )
    {
        if ( Query(value) )
            Remove( value );
        else
            Add( value );
    }

    /// @return True if multivar has this value.
    bool
    Query( T value ) const
    {
        return std::find( mValues.begin(), mValues.end(), value ) != mValues.end();
    }

    /// @return True if multivar is empty.
    bool
    IfEmpty( void ) const
    {
        return mValues.empty();
    }

private:
    std::list<T> mValues;  ///< multiple values of same type
};

} // namespace base

#endif // BASE_MULTIVAR_HH
