deba@416: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@414: * deba@416: * This file is a part of LEMON, a generic C++ optimization library. deba@414: * deba@414: * Copyright (C) 2003-2008 deba@414: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@414: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@414: * deba@414: * Permission to use, modify and distribute this software is granted deba@414: * provided that this copyright notice appears in all copies. For deba@414: * precise terms see the accompanying LICENSE file. deba@414: * deba@414: * This software is provided "AS IS" with no warranty of any kind, deba@414: * express or implied, and with no claim as to its suitability for any deba@414: * purpose. deba@414: * deba@414: */ deba@414: deba@414: #ifndef LEMON_BITS_VARIANT_H deba@414: #define LEMON_BITS_VARIANT_H deba@414: deba@414: #include deba@414: deba@414: /// \file deba@414: /// \brief Variant types deba@414: deba@414: namespace lemon { deba@414: deba@414: namespace _variant_bits { deba@416: deba@414: template deba@414: struct CTMax { deba@414: static const int value = left < right ? right : left; deba@414: }; deba@414: deba@414: } deba@414: deba@414: deba@414: /// \brief Simple Variant type for two types deba@414: /// deba@414: /// Simple Variant type for two types. The Variant type is a type deba@414: /// safe union. The C++ has strong limitations for using unions, by deba@414: /// example we can not store type with non default constructor or deba@414: /// destructor in an union. This class always knowns the current deba@414: /// state of the variant and it cares for the proper construction deba@414: /// and destruction. deba@414: template deba@414: class BiVariant { deba@414: public: deba@414: deba@414: /// \brief The \c First type. deba@414: typedef _First First; deba@414: /// \brief The \c Second type. deba@414: typedef _Second Second; deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// This constructor initalizes to the default value of the \c First deba@414: /// type. deba@414: BiVariant() { deba@414: flag = true; deba@414: new(reinterpret_cast(data)) First(); deba@414: } deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// This constructor initalizes to the given value of the \c First deba@414: /// type. deba@414: BiVariant(const First& f) { deba@414: flag = true; deba@414: new(reinterpret_cast(data)) First(f); deba@414: } deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// This constructor initalizes to the given value of the \c deba@414: /// Second type. deba@414: BiVariant(const Second& s) { deba@414: flag = false; deba@414: new(reinterpret_cast(data)) Second(s); deba@414: } deba@414: deba@414: /// \brief Copy constructor deba@414: /// deba@414: /// Copy constructor deba@414: BiVariant(const BiVariant& bivariant) { deba@414: flag = bivariant.flag; deba@414: if (flag) { deba@416: new(reinterpret_cast(data)) First(bivariant.first()); deba@414: } else { deba@416: new(reinterpret_cast(data)) Second(bivariant.second()); deba@414: } deba@414: } deba@414: deba@414: /// \brief Destrcutor deba@414: /// deba@414: /// Destructor deba@414: ~BiVariant() { deba@414: destroy(); deba@414: } deba@414: deba@414: /// \brief Set to the default value of the \c First type. deba@414: /// deba@414: /// This function sets the variant to the default value of the \c deba@414: /// First type. deba@414: BiVariant& setFirst() { deba@414: destroy(); deba@414: flag = true; deba@416: new(reinterpret_cast(data)) First(); deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Set to the given value of the \c First type. deba@414: /// deba@414: /// This function sets the variant to the given value of the \c deba@414: /// First type. deba@414: BiVariant& setFirst(const First& f) { deba@414: destroy(); deba@414: flag = true; deba@416: new(reinterpret_cast(data)) First(f); deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Set to the default value of the \c Second type. deba@414: /// deba@414: /// This function sets the variant to the default value of the \c deba@414: /// Second type. deba@414: BiVariant& setSecond() { deba@414: destroy(); deba@414: flag = false; deba@416: new(reinterpret_cast(data)) Second(); deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Set to the given value of the \c Second type. deba@414: /// deba@414: /// This function sets the variant to the given value of the \c deba@414: /// Second type. deba@414: BiVariant& setSecond(const Second& s) { deba@414: destroy(); deba@414: flag = false; deba@416: new(reinterpret_cast(data)) Second(s); deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Operator form of the \c setFirst() deba@414: BiVariant& operator=(const First& f) { deba@414: return setFirst(f); deba@414: } deba@414: deba@414: /// \brief Operator form of the \c setSecond() deba@414: BiVariant& operator=(const Second& s) { deba@414: return setSecond(s); deba@414: } deba@414: deba@414: /// \brief Assign operator deba@414: BiVariant& operator=(const BiVariant& bivariant) { deba@414: if (this == &bivariant) return *this; deba@414: destroy(); deba@414: flag = bivariant.flag; deba@414: if (flag) { deba@416: new(reinterpret_cast(data)) First(bivariant.first()); deba@414: } else { deba@416: new(reinterpret_cast(data)) Second(bivariant.second()); deba@414: } deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Reference to the value deba@414: /// deba@414: /// Reference to the value of the \c First type. deba@414: /// \pre The BiVariant should store value of \c First type. deba@414: First& first() { deba@414: LEMON_DEBUG(flag, "Variant wrong state"); deba@414: return *reinterpret_cast(data); deba@414: } deba@414: deba@414: /// \brief Const reference to the value deba@414: /// deba@414: /// Const reference to the value of the \c First type. deba@414: /// \pre The BiVariant should store value of \c First type. deba@414: const First& first() const { deba@414: LEMON_DEBUG(flag, "Variant wrong state"); deba@414: return *reinterpret_cast(data); deba@414: } deba@414: deba@414: /// \brief Operator form of the \c first() deba@414: operator First&() { return first(); } deba@414: /// \brief Operator form of the const \c first() deba@414: operator const First&() const { return first(); } deba@414: deba@414: /// \brief Reference to the value deba@414: /// deba@414: /// Reference to the value of the \c Second type. deba@414: /// \pre The BiVariant should store value of \c Second type. deba@414: Second& second() { deba@414: LEMON_DEBUG(!flag, "Variant wrong state"); deba@414: return *reinterpret_cast(data); deba@414: } deba@414: deba@414: /// \brief Const reference to the value deba@414: /// deba@414: /// Const reference to the value of the \c Second type. deba@414: /// \pre The BiVariant should store value of \c Second type. deba@414: const Second& second() const { deba@414: LEMON_DEBUG(!flag, "Variant wrong state"); deba@414: return *reinterpret_cast(data); deba@414: } deba@414: deba@414: /// \brief Operator form of the \c second() deba@414: operator Second&() { return second(); } deba@414: /// \brief Operator form of the const \c second() deba@414: operator const Second&() const { return second(); } deba@414: deba@414: /// \brief %True when the variant is in the first state deba@414: /// deba@414: /// %True when the variant stores value of the \c First type. deba@414: bool firstState() const { return flag; } deba@414: deba@414: /// \brief %True when the variant is in the second state deba@414: /// deba@414: /// %True when the variant stores value of the \c Second type. deba@414: bool secondState() const { return !flag; } deba@414: deba@414: private: deba@414: deba@414: void destroy() { deba@414: if (flag) { deba@414: reinterpret_cast(data)->~First(); deba@414: } else { deba@414: reinterpret_cast(data)->~Second(); deba@414: } deba@414: } deba@416: deba@414: char data[_variant_bits::CTMax::value]; deba@414: bool flag; deba@414: }; deba@414: deba@414: namespace _variant_bits { deba@416: deba@414: template deba@414: struct Memory { deba@414: deba@414: typedef typename _TypeMap::template Map<_idx>::Type Current; deba@414: deba@414: static void destroy(int index, char* place) { deba@414: if (index == _idx) { deba@414: reinterpret_cast(place)->~Current(); deba@414: } else { deba@414: Memory<_idx - 1, _TypeMap>::destroy(index, place); deba@414: } deba@414: } deba@414: deba@414: static void copy(int index, char* to, const char* from) { deba@414: if (index == _idx) { deba@414: new (reinterpret_cast(to)) deba@414: Current(reinterpret_cast(from)); deba@414: } else { deba@414: Memory<_idx - 1, _TypeMap>::copy(index, to, from); deba@414: } deba@414: } deba@414: deba@414: }; deba@414: deba@414: template deba@414: struct Memory<-1, _TypeMap> { deba@414: deba@414: static void destroy(int, char*) { deba@414: LEMON_DEBUG(false, "Variant wrong index."); deba@414: } deba@414: deba@414: static void copy(int, char*, const char*) { deba@414: LEMON_DEBUG(false, "Variant wrong index."); deba@414: } deba@414: }; deba@414: deba@414: template deba@414: struct Size { deba@416: static const int value = deba@416: CTMax::Type), deba@414: Size<_idx - 1, _TypeMap>::value>::value; deba@414: }; deba@414: deba@414: template deba@414: struct Size<0, _TypeMap> { deba@416: static const int value = deba@414: sizeof(typename _TypeMap::template Map<0>::Type); deba@414: }; deba@414: deba@414: } deba@414: deba@414: /// \brief Variant type deba@414: /// deba@414: /// Simple Variant type. The Variant type is a type safe union. The deba@414: /// C++ has strong limitations for using unions, for example we deba@414: /// cannot store type with non default constructor or destructor in deba@414: /// a union. This class always knowns the current state of the deba@414: /// variant and it cares for the proper construction and deba@414: /// destruction. deba@414: /// deba@414: /// \param _num The number of the types which can be stored in the deba@414: /// variant type. deba@414: /// \param _TypeMap This class describes the types of the Variant. The deba@416: /// _TypeMap::Map::Type should be a valid type for each index deba@414: /// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper deba@414: /// class to define such type mappings up to 10 types. deba@414: /// deba@414: /// And the usage of the class: deba@414: ///\code deba@414: /// typedef Variant<3, VariantTypeMap > MyVariant; deba@414: /// MyVariant var; deba@414: /// var.set<0>(12); deba@414: /// std::cout << var.get<0>() << std::endl; deba@414: /// var.set<1>("alpha"); deba@414: /// std::cout << var.get<1>() << std::endl; deba@414: /// var.set<2>(0.75); deba@414: /// std::cout << var.get<2>() << std::endl; deba@414: ///\endcode deba@414: /// deba@414: /// The result of course: deba@414: ///\code deba@414: /// 12 deba@414: /// alpha deba@414: /// 0.75 deba@414: ///\endcode deba@414: template deba@414: class Variant { deba@414: public: deba@414: deba@414: static const int num = _num; deba@414: deba@414: typedef _TypeMap TypeMap; deba@414: deba@414: /// \brief Constructor deba@414: /// deba@414: /// This constructor initalizes to the default value of the \c type deba@414: /// with 0 index. deba@414: Variant() { deba@414: flag = 0; deba@416: new(reinterpret_cast::Type*>(data)) deba@414: typename TypeMap::template Map<0>::Type(); deba@414: } deba@414: deba@414: deba@414: /// \brief Copy constructor deba@414: /// deba@414: /// Copy constructor deba@414: Variant(const Variant& variant) { deba@414: flag = variant.flag; deba@414: _variant_bits::Memory::copy(flag, data, variant.data); deba@414: } deba@414: deba@414: /// \brief Assign operator deba@414: /// deba@414: /// Assign operator deba@414: Variant& operator=(const Variant& variant) { deba@414: if (this == &variant) return *this; deba@414: _variant_bits::Memory:: deba@414: destroy(flag, data); deba@414: flag = variant.flag; deba@414: _variant_bits::Memory:: deba@414: copy(flag, data, variant.data); deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Destrcutor deba@414: /// deba@414: /// Destructor deba@414: ~Variant() { deba@414: _variant_bits::Memory::destroy(flag, data); deba@414: } deba@414: deba@414: /// \brief Set to the default value of the type with \c _idx index. deba@414: /// deba@414: /// This function sets the variant to the default value of the deba@414: /// type with \c _idx index. deba@414: template deba@414: Variant& set() { deba@414: _variant_bits::Memory::destroy(flag, data); deba@414: flag = _idx; deba@416: new(reinterpret_cast::Type*>(data)) deba@414: typename TypeMap::template Map<_idx>::Type(); deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Set to the given value of the type with \c _idx index. deba@414: /// deba@414: /// This function sets the variant to the given value of the type deba@414: /// with \c _idx index. deba@414: template deba@414: Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) { deba@414: _variant_bits::Memory::destroy(flag, data); deba@414: flag = _idx; deba@416: new(reinterpret_cast::Type*>(data)) deba@414: typename TypeMap::template Map<_idx>::Type(init); deba@414: return *this; deba@414: } deba@414: deba@414: /// \brief Gets the current value of the type with \c _idx index. deba@414: /// deba@414: /// Gets the current value of the type with \c _idx index. deba@414: template deba@414: const typename TypeMap::template Map<_idx>::Type& get() const { deba@414: LEMON_DEBUG(_idx == flag, "Variant wrong index"); deba@414: return *reinterpret_cast::Type*>(data); deba@414: } deba@414: deba@414: /// \brief Gets the current value of the type with \c _idx index. deba@414: /// deba@414: /// Gets the current value of the type with \c _idx index. deba@414: template deba@414: typename _TypeMap::template Map<_idx>::Type& get() { deba@414: LEMON_DEBUG(_idx == flag, "Variant wrong index"); deba@414: return *reinterpret_cast::Type*> deba@416: (data); deba@414: } deba@414: deba@414: /// \brief Returns the current state of the variant. deba@414: /// deba@414: /// Returns the current state of the variant. deba@414: int state() const { deba@414: return flag; deba@414: } deba@414: deba@414: private: deba@416: deba@414: char data[_variant_bits::Size::value]; deba@414: int flag; deba@414: }; deba@414: deba@414: namespace _variant_bits { deba@414: deba@414: template deba@414: struct Get { deba@414: typedef typename Get<_index - 1, typename _List::Next>::Type Type; deba@414: }; deba@414: deba@414: template deba@414: struct Get<0, _List> { deba@414: typedef typename _List::Type Type; deba@414: }; deba@414: deba@414: struct List {}; deba@416: deba@414: template deba@414: struct Insert { deba@414: typedef _List Next; deba@414: typedef _Type Type; deba@414: }; deba@414: deba@416: template deba@414: struct Mapper { deba@414: typedef List L10; deba@414: typedef Insert<_T9, L10> L9; deba@414: typedef Insert<_T8, L9> L8; deba@414: typedef Insert<_T7, L8> L7; deba@414: typedef Insert<_T6, L7> L6; deba@414: typedef Insert<_T5, L6> L5; deba@414: typedef Insert<_T4, L5> L4; deba@414: typedef Insert<_T3, L4> L3; deba@414: typedef Insert<_T2, L3> L2; deba@414: typedef Insert<_T1, L2> L1; deba@414: typedef Insert<_T0, L1> L0; deba@414: typedef typename Get<_idx, L0>::Type Type; deba@414: }; deba@416: deba@414: } deba@414: deba@414: /// \brief Helper class for Variant deba@414: /// deba@414: /// Helper class to define type mappings for Variant. This class deba@414: /// converts the template parameters to be mappable by integer. deba@414: /// \see Variant deba@414: template < deba@416: typename _T0, deba@414: typename _T1 = void, typename _T2 = void, typename _T3 = void, deba@414: typename _T5 = void, typename _T4 = void, typename _T6 = void, deba@414: typename _T7 = void, typename _T8 = void, typename _T9 = void> deba@414: struct VariantTypeMap { deba@414: template deba@414: struct Map { deba@414: typedef typename _variant_bits:: deba@414: Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type deba@414: Type; deba@414: }; deba@414: }; deba@416: deba@414: } deba@414: deba@414: deba@414: #endif