BiVariant moved to lemon/bits/variant.h
authordeba
Fri, 11 Aug 2006 14:55:33 +0000
changeset 2177416a7030b7e3
parent 2176 0f647e65ecad
child 2178 0d7c0f96a5ee
BiVariant moved to lemon/bits/variant.h
lemon/Makefile.am
lemon/bits/utility.h
lemon/bits/variant.h
lemon/graph_adaptor.h
     1.1 --- a/lemon/Makefile.am	Fri Aug 11 14:55:02 2006 +0000
     1.2 +++ b/lemon/Makefile.am	Fri Aug 11 14:55:33 2006 +0000
     1.3 @@ -110,6 +110,7 @@
     1.4  	lemon/bits/mingw32_time.h \
     1.5  	lemon/bits/traits.h \
     1.6  	lemon/bits/utility.h \
     1.7 +	lemon/bits/variant.h \
     1.8  	lemon/bits/vector_map.h
     1.9  
    1.10  concept_HEADERS += \
     2.1 --- a/lemon/bits/utility.h	Fri Aug 11 14:55:02 2006 +0000
     2.2 +++ b/lemon/bits/utility.h	Fri Aug 11 14:55:33 2006 +0000
     2.3 @@ -35,8 +35,6 @@
     2.4  #ifndef LEMON_BITS_UTILITY_H
     2.5  #define LEMON_BITS_UTILITY_H
     2.6  
     2.7 -#include <lemon/error.h>
     2.8 -
     2.9  ///\file
    2.10  ///\brief Miscellaneous basic utilities
    2.11  ///
    2.12 @@ -76,216 +74,6 @@
    2.13      InvalidType();
    2.14    };
    2.15  
    2.16 -  template <bool left, bool right>
    2.17 -  struct CTOr {
    2.18 -    static const bool value = true;
    2.19 -  };
    2.20 -
    2.21 -  template <>
    2.22 -  struct CTOr<false, false> {
    2.23 -    static const bool value = false;
    2.24 -  };
    2.25 -
    2.26 -  template <bool left, bool right>
    2.27 -  struct CTAnd {
    2.28 -    static const bool value = false;
    2.29 -  };
    2.30 -
    2.31 -  template <>
    2.32 -  struct CTAnd<true, true> {
    2.33 -    static const bool value = true;
    2.34 -  };
    2.35 -
    2.36 -  template <int left, int right>
    2.37 -  struct CTEqual {
    2.38 -    static const bool value = false;
    2.39 -  };
    2.40 -
    2.41 -  template <int val>
    2.42 -  struct CTEqual<val, val> {
    2.43 -    static const bool value = true;
    2.44 -  };
    2.45 -
    2.46 -
    2.47 -  template <int left, int right>
    2.48 -  struct CTLess {
    2.49 -    static const bool value = left < right;
    2.50 -  };
    2.51 -
    2.52 -  template <int left>
    2.53 -  struct CTLess<left, 0> {
    2.54 -    static const bool value = false;
    2.55 -  };
    2.56 -
    2.57 -  template <int right>
    2.58 -  struct CTLess<0, right> {
    2.59 -    static const bool value = true;
    2.60 -  };
    2.61 -
    2.62 -  template <>
    2.63 -  struct CTLess<0, 0> {
    2.64 -    static const bool value = false;
    2.65 -  };
    2.66 -
    2.67 -  template <>
    2.68 -  struct CTLess<1, 1> {
    2.69 -    static const bool value = false;
    2.70 -  };
    2.71 -
    2.72 -  template <bool less, int left, int right>
    2.73 -  struct CTMaxImpl {
    2.74 -    static const int value = left;    
    2.75 -  };
    2.76 -
    2.77 -  template <int left, int right>
    2.78 -  struct CTMaxImpl<true, left, right> {
    2.79 -    static const int value = right;
    2.80 -  };
    2.81 -  
    2.82 -  template <int left, int right>
    2.83 -  struct CTMax {
    2.84 -    static const int value = 
    2.85 -    CTMaxImpl<CTLess<left, right>::value, left, right>::value;
    2.86 -  };
    2.87 -
    2.88 -
    2.89 -  /// \brief Simple Variant type with two type
    2.90 -  ///
    2.91 -  /// Simple Variant type with two type
    2.92 -  template <typename _First, typename _Second>
    2.93 -  class BiVariant {
    2.94 -  public:
    2.95 -
    2.96 -    typedef _First First;
    2.97 -    typedef _Second Second;
    2.98 -
    2.99 -    struct WrongStateError : public lemon::LogicError {
   2.100 -    public:
   2.101 -      virtual const char* what() const throw() {
   2.102 -        return "lemon::BiVariant::WrongStateError";
   2.103 -      }
   2.104 -    };
   2.105 -
   2.106 -    BiVariant() {
   2.107 -      flag = true;
   2.108 -      new(reinterpret_cast<First*>(data)) First();
   2.109 -    }
   2.110 -
   2.111 -    BiVariant(const First& first) {
   2.112 -      flag = true;
   2.113 -      new(reinterpret_cast<First*>(data)) First(first);
   2.114 -    }
   2.115 -
   2.116 -    BiVariant(const Second& second) {
   2.117 -      flag = false;
   2.118 -      new(reinterpret_cast<Second*>(data)) Second(second);
   2.119 -    }
   2.120 -
   2.121 -    BiVariant(const BiVariant& bivariant) {
   2.122 -      flag = bivariant.flag;
   2.123 -      if (flag) {
   2.124 -        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
   2.125 -      } else {
   2.126 -        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
   2.127 -      }
   2.128 -    }
   2.129 -
   2.130 -    ~BiVariant() {
   2.131 -      destroy();
   2.132 -    }
   2.133 -
   2.134 -    BiVariant& setFirst() {
   2.135 -      destroy();
   2.136 -      flag = true;
   2.137 -      new(reinterpret_cast<First*>(data)) First();   
   2.138 -      return *this;
   2.139 -    }
   2.140 -
   2.141 -    BiVariant& setFirst(const First& first) {
   2.142 -      destroy();
   2.143 -      flag = true;
   2.144 -      new(reinterpret_cast<First*>(data)) First(first);   
   2.145 -      return *this;
   2.146 -    }
   2.147 -
   2.148 -    BiVariant& setSecond() {
   2.149 -      destroy();
   2.150 -      flag = false;
   2.151 -      new(reinterpret_cast<Second*>(data)) Second();   
   2.152 -      return *this;
   2.153 -    }
   2.154 -
   2.155 -    BiVariant& setSecond(const Second& second) {
   2.156 -      destroy();
   2.157 -      flag = false;
   2.158 -      new(reinterpret_cast<Second*>(data)) Second(second);   
   2.159 -      return *this;
   2.160 -    }
   2.161 -
   2.162 -    BiVariant& operator=(const First& first) {
   2.163 -      return setFirst(first);
   2.164 -    }
   2.165 -
   2.166 -    BiVariant& operator=(const Second& second) {
   2.167 -      return setSecond(second);
   2.168 -    }
   2.169 -
   2.170 -
   2.171 -    BiVariant& operator=(const BiVariant& bivariant) {
   2.172 -      if (this == &bivariant) return *this;
   2.173 -      destroy();
   2.174 -      flag = bivariant.flag;
   2.175 -      if (flag) {
   2.176 -        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
   2.177 -      } else {
   2.178 -        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
   2.179 -      }
   2.180 -      return *this;
   2.181 -    }
   2.182 -
   2.183 -    First& first() {
   2.184 -      LEMON_ASSERT(flag, WrongStateError());
   2.185 -      return *reinterpret_cast<First*>(data); 
   2.186 -    }
   2.187 -
   2.188 -    const First& first() const { 
   2.189 -      LEMON_ASSERT(flag, WrongStateError());
   2.190 -      return *reinterpret_cast<const First*>(data); 
   2.191 -    }
   2.192 -
   2.193 -    operator First&() { return first(); }
   2.194 -    operator const First&() const { return first(); }
   2.195 -
   2.196 -    Second& second() { 
   2.197 -      LEMON_ASSERT(!flag, WrongStateError());
   2.198 -      return *reinterpret_cast<Second*>(data); 
   2.199 -    }
   2.200 -
   2.201 -    const Second& second() const { 
   2.202 -      LEMON_ASSERT(!flag, WrongStateError());
   2.203 -      return *reinterpret_cast<const Second*>(data); 
   2.204 -    }
   2.205 -
   2.206 -    operator Second&() { return second(); }
   2.207 -    operator const Second&() const { return second(); }
   2.208 -
   2.209 -    bool firstState() const { return flag; }
   2.210 -    bool secondState() const { return !flag; }
   2.211 -
   2.212 -  private:
   2.213 -
   2.214 -    void destroy() {
   2.215 -      if (flag) {
   2.216 -        reinterpret_cast<First*>(data)->~First();
   2.217 -      } else {
   2.218 -        reinterpret_cast<Second*>(data)->~Second();
   2.219 -      }
   2.220 -    }
   2.221 -    
   2.222 -    char data[CTMax<sizeof(First), sizeof(Second)>::value];
   2.223 -    bool flag;
   2.224 -  };
   2.225 -
   2.226    template <typename T>
   2.227    struct Wrap {
   2.228      const T &value;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/lemon/bits/variant.h	Fri Aug 11 14:55:33 2006 +0000
     3.3 @@ -0,0 +1,239 @@
     3.4 +/* -*- C++ -*-
     3.5 + *
     3.6 + * This file is a part of LEMON, a generic C++ optimization library
     3.7 + *
     3.8 + * Copyright (C) 2003-2006
     3.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    3.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    3.11 + *
    3.12 + * Permission to use, modify and distribute this software is granted
    3.13 + * provided that this copyright notice appears in all copies. For
    3.14 + * precise terms see the accompanying LICENSE file.
    3.15 + *
    3.16 + * This software is provided "AS IS" with no warranty of any kind,
    3.17 + * express or implied, and with no claim as to its suitability for any
    3.18 + * purpose.
    3.19 + *
    3.20 + */
    3.21 +
    3.22 +#ifndef LEMON_BITS_VARIANT_H
    3.23 +#define LEMON_BITS_VARIANT_H
    3.24 +
    3.25 +#include <lemon/error.h>
    3.26 +
    3.27 +namespace lemon {
    3.28 +
    3.29 +  template <bool left, bool right>
    3.30 +  struct CTOr {
    3.31 +    static const bool value = true;
    3.32 +  };
    3.33 +
    3.34 +  template <>
    3.35 +  struct CTOr<false, false> {
    3.36 +    static const bool value = false;
    3.37 +  };
    3.38 +
    3.39 +  template <bool left, bool right>
    3.40 +  struct CTAnd {
    3.41 +    static const bool value = false;
    3.42 +  };
    3.43 +
    3.44 +  template <>
    3.45 +  struct CTAnd<true, true> {
    3.46 +    static const bool value = true;
    3.47 +  };
    3.48 +
    3.49 +  template <int left, int right>
    3.50 +  struct CTEqual {
    3.51 +    static const bool value = false;
    3.52 +  };
    3.53 +
    3.54 +  template <int val>
    3.55 +  struct CTEqual<val, val> {
    3.56 +    static const bool value = true;
    3.57 +  };
    3.58 +
    3.59 +
    3.60 +  template <int left, int right>
    3.61 +  struct CTLess {
    3.62 +    static const bool value = left < right;
    3.63 +  };
    3.64 +
    3.65 +  template <int left>
    3.66 +  struct CTLess<left, 0> {
    3.67 +    static const bool value = false;
    3.68 +  };
    3.69 +
    3.70 +  template <int right>
    3.71 +  struct CTLess<0, right> {
    3.72 +    static const bool value = true;
    3.73 +  };
    3.74 +
    3.75 +  template <>
    3.76 +  struct CTLess<0, 0> {
    3.77 +    static const bool value = false;
    3.78 +  };
    3.79 +
    3.80 +  template <>
    3.81 +  struct CTLess<1, 1> {
    3.82 +    static const bool value = false;
    3.83 +  };
    3.84 +
    3.85 +  template <bool less, int left, int right>
    3.86 +  struct CTMaxImpl {
    3.87 +    static const int value = left;    
    3.88 +  };
    3.89 +
    3.90 +  template <int left, int right>
    3.91 +  struct CTMaxImpl<true, left, right> {
    3.92 +    static const int value = right;
    3.93 +  };
    3.94 +  
    3.95 +  template <int left, int right>
    3.96 +  struct CTMax {
    3.97 +    static const int value = 
    3.98 +    CTMaxImpl<CTLess<left, right>::value, left, right>::value;
    3.99 +  };
   3.100 +
   3.101 +
   3.102 +  /// \brief Simple Variant type with two type
   3.103 +  ///
   3.104 +  /// Simple Variant type with two type
   3.105 +  template <typename _First, typename _Second>
   3.106 +  class BiVariant {
   3.107 +  public:
   3.108 +
   3.109 +    typedef _First First;
   3.110 +    typedef _Second Second;
   3.111 +
   3.112 +    struct WrongStateError : public lemon::LogicError {
   3.113 +    public:
   3.114 +      virtual const char* what() const throw() {
   3.115 +        return "lemon::BiVariant::WrongStateError";
   3.116 +      }
   3.117 +    };
   3.118 +
   3.119 +    BiVariant() {
   3.120 +      flag = true;
   3.121 +      new(reinterpret_cast<First*>(data)) First();
   3.122 +    }
   3.123 +
   3.124 +    BiVariant(const First& first) {
   3.125 +      flag = true;
   3.126 +      new(reinterpret_cast<First*>(data)) First(first);
   3.127 +    }
   3.128 +
   3.129 +    BiVariant(const Second& second) {
   3.130 +      flag = false;
   3.131 +      new(reinterpret_cast<Second*>(data)) Second(second);
   3.132 +    }
   3.133 +
   3.134 +    BiVariant(const BiVariant& bivariant) {
   3.135 +      flag = bivariant.flag;
   3.136 +      if (flag) {
   3.137 +        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
   3.138 +      } else {
   3.139 +        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
   3.140 +      }
   3.141 +    }
   3.142 +
   3.143 +    ~BiVariant() {
   3.144 +      destroy();
   3.145 +    }
   3.146 +
   3.147 +    BiVariant& setFirst() {
   3.148 +      destroy();
   3.149 +      flag = true;
   3.150 +      new(reinterpret_cast<First*>(data)) First();   
   3.151 +      return *this;
   3.152 +    }
   3.153 +
   3.154 +    BiVariant& setFirst(const First& first) {
   3.155 +      destroy();
   3.156 +      flag = true;
   3.157 +      new(reinterpret_cast<First*>(data)) First(first);   
   3.158 +      return *this;
   3.159 +    }
   3.160 +
   3.161 +    BiVariant& setSecond() {
   3.162 +      destroy();
   3.163 +      flag = false;
   3.164 +      new(reinterpret_cast<Second*>(data)) Second();   
   3.165 +      return *this;
   3.166 +    }
   3.167 +
   3.168 +    BiVariant& setSecond(const Second& second) {
   3.169 +      destroy();
   3.170 +      flag = false;
   3.171 +      new(reinterpret_cast<Second*>(data)) Second(second);   
   3.172 +      return *this;
   3.173 +    }
   3.174 +
   3.175 +    BiVariant& operator=(const First& first) {
   3.176 +      return setFirst(first);
   3.177 +    }
   3.178 +
   3.179 +    BiVariant& operator=(const Second& second) {
   3.180 +      return setSecond(second);
   3.181 +    }
   3.182 +
   3.183 +
   3.184 +    BiVariant& operator=(const BiVariant& bivariant) {
   3.185 +      if (this == &bivariant) return *this;
   3.186 +      destroy();
   3.187 +      flag = bivariant.flag;
   3.188 +      if (flag) {
   3.189 +        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
   3.190 +      } else {
   3.191 +        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
   3.192 +      }
   3.193 +      return *this;
   3.194 +    }
   3.195 +
   3.196 +    First& first() {
   3.197 +      LEMON_ASSERT(flag, WrongStateError());
   3.198 +      return *reinterpret_cast<First*>(data); 
   3.199 +    }
   3.200 +
   3.201 +    const First& first() const { 
   3.202 +      LEMON_ASSERT(flag, WrongStateError());
   3.203 +      return *reinterpret_cast<const First*>(data); 
   3.204 +    }
   3.205 +
   3.206 +    operator First&() { return first(); }
   3.207 +    operator const First&() const { return first(); }
   3.208 +
   3.209 +    Second& second() { 
   3.210 +      LEMON_ASSERT(!flag, WrongStateError());
   3.211 +      return *reinterpret_cast<Second*>(data); 
   3.212 +    }
   3.213 +
   3.214 +    const Second& second() const { 
   3.215 +      LEMON_ASSERT(!flag, WrongStateError());
   3.216 +      return *reinterpret_cast<const Second*>(data); 
   3.217 +    }
   3.218 +
   3.219 +    operator Second&() { return second(); }
   3.220 +    operator const Second&() const { return second(); }
   3.221 +
   3.222 +    bool firstState() const { return flag; }
   3.223 +    bool secondState() const { return !flag; }
   3.224 +
   3.225 +  private:
   3.226 +
   3.227 +    void destroy() {
   3.228 +      if (flag) {
   3.229 +        reinterpret_cast<First*>(data)->~First();
   3.230 +      } else {
   3.231 +        reinterpret_cast<Second*>(data)->~Second();
   3.232 +      }
   3.233 +    }
   3.234 +    
   3.235 +    char data[CTMax<sizeof(First), sizeof(Second)>::value];
   3.236 +    bool flag;
   3.237 +  };
   3.238 +
   3.239 +}
   3.240 +
   3.241 +
   3.242 +#endif
     4.1 --- a/lemon/graph_adaptor.h	Fri Aug 11 14:55:02 2006 +0000
     4.2 +++ b/lemon/graph_adaptor.h	Fri Aug 11 14:55:33 2006 +0000
     4.3 @@ -28,6 +28,7 @@
     4.4  ///\author Marton Makai and Balazs Dezso
     4.5  
     4.6  #include <lemon/bits/invalid.h>
     4.7 +#include <lemon/bits/variant.h>
     4.8  #include <lemon/maps.h>
     4.9  
    4.10  #include <lemon/bits/base_extender.h>