lemon/bits/utility.h
author deba
Fri, 12 May 2006 09:52:28 +0000
changeset 2077 d687c0033bb5
parent 1993 2115143eceea
child 2078 123f08422c14
permissions -rw-r--r--
Two state Variant
+ Some Compile Time arithmetic
deba@1993
     1
/* -*- C++ -*-
deba@1993
     2
 *
deba@1993
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@1993
     4
 *
deba@1993
     5
 * Copyright (C) 2003-2006
deba@1993
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1993
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1993
     8
 *
deba@1993
     9
 * Permission to use, modify and distribute this software is granted
deba@1993
    10
 * provided that this copyright notice appears in all copies. For
deba@1993
    11
 * precise terms see the accompanying LICENSE file.
deba@1993
    12
 *
deba@1993
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1993
    14
 * express or implied, and with no claim as to its suitability for any
deba@1993
    15
 * purpose.
deba@1993
    16
 *
deba@1993
    17
 */
deba@1993
    18
deba@1993
    19
// This file contains a modified version of the enable_if library from BOOST.
deba@1993
    20
// See the appropriate copyright notice below.
deba@1993
    21
deba@1993
    22
// Boost enable_if library
deba@1993
    23
deba@1993
    24
// Copyright 2003 © The Trustees of Indiana University.
deba@1993
    25
deba@1993
    26
// Use, modification, and distribution is subject to the Boost Software
deba@1993
    27
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
deba@1993
    28
// http://www.boost.org/LICENSE_1_0.txt)
deba@1993
    29
deba@1993
    30
//    Authors: Jaakko Järvi (jajarvi at osl.iu.edu)
deba@1993
    31
//             Jeremiah Willcock (jewillco at osl.iu.edu)
deba@1993
    32
//             Andrew Lumsdaine (lums at osl.iu.edu)
deba@1993
    33
deba@1993
    34
deba@1993
    35
#ifndef LEMON_BITS_UTILITY_H
deba@1993
    36
#define LEMON_BITS_UTILITY_H
deba@1993
    37
deba@2077
    38
#include <lemon/error.h>
deba@2077
    39
deba@1993
    40
///\file
deba@1993
    41
///\brief Miscellaneous basic utilities
deba@1993
    42
///
deba@1993
    43
///\todo Please rethink the organisation of the basic files like this.
deba@1993
    44
///E.g. this file might be merged with invalid.h.
deba@1993
    45
deba@1993
    46
deba@1993
    47
namespace lemon
deba@1993
    48
{
deba@1993
    49
deba@1993
    50
  /// Basic type for defining "tags". A "YES" condition for \c enable_if.
deba@1993
    51
deba@1993
    52
  /// Basic type for defining "tags". A "YES" condition for \c enable_if.
deba@1993
    53
  ///
deba@1993
    54
  ///\sa False
deba@1993
    55
  ///
deba@1993
    56
  /// \todo This should go to a separate "basic_types.h" (or something)
deba@1993
    57
  /// file.
deba@1993
    58
  struct True {
deba@1993
    59
    ///\e
deba@1993
    60
    static const bool value = true;
deba@1993
    61
  };
deba@1993
    62
deba@1993
    63
  /// Basic type for defining "tags". A "NO" condition for \c enable_if.
deba@1993
    64
deba@1993
    65
  /// Basic type for defining "tags". A "NO" condition for \c enable_if.
deba@1993
    66
  ///
deba@1993
    67
  ///\sa True
deba@1993
    68
  struct False {
deba@1993
    69
    ///\e
deba@1993
    70
    static const bool value = false;
deba@1993
    71
  };
deba@1993
    72
deba@1993
    73
deba@1993
    74
  class InvalidType {
deba@1993
    75
  private:
deba@1993
    76
    InvalidType();
deba@1993
    77
  };
deba@2077
    78
deba@2077
    79
  template <bool left, bool right>
deba@2077
    80
  struct CTOr {
deba@2077
    81
    static const bool value = true;
deba@2077
    82
  };
deba@2077
    83
deba@2077
    84
  template <>
deba@2077
    85
  struct CTOr<false, false> {
deba@2077
    86
    static const bool value = false;
deba@2077
    87
  };
deba@2077
    88
deba@2077
    89
  template <bool left, bool right>
deba@2077
    90
  struct CTAnd {
deba@2077
    91
    static const bool value = false;
deba@2077
    92
  };
deba@2077
    93
deba@2077
    94
  template <>
deba@2077
    95
  struct CTAnd<true, true> {
deba@2077
    96
    static const bool value = true;
deba@2077
    97
  };
deba@2077
    98
deba@2077
    99
  template <int left, int right>
deba@2077
   100
  struct CTEqual {
deba@2077
   101
    static const bool value = false;
deba@2077
   102
  };
deba@2077
   103
deba@2077
   104
  template <int val>
deba@2077
   105
  struct CTEqual<val, val> {
deba@2077
   106
    static const bool value = true;
deba@2077
   107
  };
deba@2077
   108
deba@2077
   109
  template <bool sless, bool sequal, bool bless>
deba@2077
   110
  struct CTLessImpl {
deba@2077
   111
   static const bool value =
deba@2077
   112
   CTOr<sless, CTAnd<sequal, bless>::value>::value;
deba@2077
   113
  };
deba@2077
   114
deba@2077
   115
  template <int left, int right>
deba@2077
   116
  struct CTLess {
deba@2077
   117
    static const bool value = 
deba@2077
   118
    CTLessImpl<CTLess<left >> 1, right >> 1>::value, 
deba@2077
   119
               CTEqual<left >> 1, right >> 1>::value,
deba@2077
   120
               CTLess<left & 1, right & 1>::value >::value;
deba@2077
   121
  };
deba@2077
   122
deba@2077
   123
  template <int left>
deba@2077
   124
  struct CTLess<left, 0> {
deba@2077
   125
    static const bool value = false;
deba@2077
   126
  };
deba@2077
   127
deba@2077
   128
  template <int right>
deba@2077
   129
  struct CTLess<0, right> {
deba@2077
   130
    static const bool value = true;
deba@2077
   131
  };
deba@2077
   132
deba@2077
   133
  template <>
deba@2077
   134
  struct CTLess<0, 0> {
deba@2077
   135
    static const bool value = false;
deba@2077
   136
  };
deba@2077
   137
deba@2077
   138
  template <>
deba@2077
   139
  struct CTLess<1, 1> {
deba@2077
   140
    static const bool value = false;
deba@2077
   141
  };
deba@2077
   142
deba@2077
   143
  template <bool less, int left, int right>
deba@2077
   144
  struct CTMaxImpl {
deba@2077
   145
    static const int value = left;    
deba@2077
   146
  };
deba@2077
   147
deba@2077
   148
  template <int left, int right>
deba@2077
   149
  struct CTMaxImpl<true, left, right> {
deba@2077
   150
    static const int value = right;
deba@2077
   151
  };
deba@1993
   152
  
deba@2077
   153
  template <int left, int right>
deba@2077
   154
  struct CTMax {
deba@2077
   155
    static const int value = 
deba@2077
   156
    CTMaxImpl<CTLess<left, right>::value, left, right>::value;
deba@2077
   157
  };
deba@2077
   158
deba@2077
   159
deba@2077
   160
  /// \brief Simple Variant type with two type
deba@2077
   161
  ///
deba@2077
   162
  /// Simple Variant type with two type
deba@2077
   163
  template <typename _First, typename _Second>
deba@2077
   164
  class BiVariant {
deba@2077
   165
  public:
deba@2077
   166
deba@2077
   167
    typedef _First First;
deba@2077
   168
    typedef _Second Second;
deba@2077
   169
deba@2077
   170
    struct WrongStateError : public lemon::LogicError {
deba@2077
   171
    public:
deba@2077
   172
      virtual const char* exceptionName() const {
deba@2077
   173
        return "lemon::BiVariant::WrongStateError";
deba@2077
   174
      }
deba@2077
   175
    };
deba@2077
   176
deba@2077
   177
    BiVariant() {
deba@2077
   178
      flag = true;
deba@2077
   179
      new(reinterpret_cast<First*>(data)) First();
deba@2077
   180
    }
deba@2077
   181
deba@2077
   182
    BiVariant(const First& first) {
deba@2077
   183
      flag = true;
deba@2077
   184
      new(reinterpret_cast<First*>(data)) First(first);
deba@2077
   185
    }
deba@2077
   186
deba@2077
   187
    BiVariant(const Second& second) {
deba@2077
   188
      flag = false;
deba@2077
   189
      new(reinterpret_cast<Second*>(data)) Second(second);
deba@2077
   190
    }
deba@2077
   191
deba@2077
   192
    BiVariant(const BiVariant& bivariant) {
deba@2077
   193
      flag = bivariant.flag;
deba@2077
   194
      if (flag) {
deba@2077
   195
        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
deba@2077
   196
      } else {
deba@2077
   197
        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
deba@2077
   198
      }
deba@2077
   199
    }
deba@2077
   200
deba@2077
   201
    ~BiVariant() {
deba@2077
   202
      destroy();
deba@2077
   203
    }
deba@2077
   204
deba@2077
   205
    BiVariant& setFirst() {
deba@2077
   206
      destroy();
deba@2077
   207
      flag = true;
deba@2077
   208
      new(reinterpret_cast<First*>(data)) First();   
deba@2077
   209
      return *this;
deba@2077
   210
    }
deba@2077
   211
deba@2077
   212
    BiVariant& setFirst(const First& first) {
deba@2077
   213
      destroy();
deba@2077
   214
      flag = true;
deba@2077
   215
      new(reinterpret_cast<First*>(data)) First(first);   
deba@2077
   216
      return *this;
deba@2077
   217
    }
deba@2077
   218
deba@2077
   219
    BiVariant& setSecond() {
deba@2077
   220
      destroy();
deba@2077
   221
      flag = false;
deba@2077
   222
      new(reinterpret_cast<Second*>(data)) Second();   
deba@2077
   223
      return *this;
deba@2077
   224
    }
deba@2077
   225
deba@2077
   226
    BiVariant& setSecond(const Second& second) {
deba@2077
   227
      destroy();
deba@2077
   228
      flag = false;
deba@2077
   229
      new(reinterpret_cast<Second*>(data)) Second(second);   
deba@2077
   230
      return *this;
deba@2077
   231
    }
deba@2077
   232
deba@2077
   233
    BiVariant& operator=(const First& first) {
deba@2077
   234
      return setFirst(first);
deba@2077
   235
    }
deba@2077
   236
deba@2077
   237
    BiVariant& operator=(const Second& second) {
deba@2077
   238
      return setSecond(second);
deba@2077
   239
    }
deba@2077
   240
deba@2077
   241
deba@2077
   242
    BiVariant& operator=(const BiVariant& bivariant) {
deba@2077
   243
      if (this == &bivariant) return *this;
deba@2077
   244
      destroy();
deba@2077
   245
      flag = bivariant.flag;
deba@2077
   246
      if (flag) {
deba@2077
   247
        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
deba@2077
   248
      } else {
deba@2077
   249
        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
deba@2077
   250
      }
deba@2077
   251
      return *this;
deba@2077
   252
    }
deba@2077
   253
deba@2077
   254
    First& first() {
deba@2077
   255
      LEMON_ASSERT(flag, WrongStateError());
deba@2077
   256
      return *reinterpret_cast<First*>(data); 
deba@2077
   257
    }
deba@2077
   258
deba@2077
   259
    const First& first() const { 
deba@2077
   260
      LEMON_ASSERT(flag, WrongStateError());
deba@2077
   261
      return *reinterpret_cast<const First*>(data); 
deba@2077
   262
    }
deba@2077
   263
deba@2077
   264
    operator First&() { return first(); }
deba@2077
   265
    operator const First&() const { return first(); }
deba@2077
   266
deba@2077
   267
    Second& second() { 
deba@2077
   268
      LEMON_ASSERT(!flag, WrongStateError());
deba@2077
   269
      return *reinterpret_cast<Second*>(data); 
deba@2077
   270
    }
deba@2077
   271
deba@2077
   272
    const Second& second() const { 
deba@2077
   273
      LEMON_ASSERT(!flag, WrongStateError());
deba@2077
   274
      return *reinterpret_cast<const Second*>(data); 
deba@2077
   275
    }
deba@2077
   276
deba@2077
   277
    operator Second&() { return second(); }
deba@2077
   278
    operator const Second&() const { return second(); }
deba@2077
   279
deba@2077
   280
    bool firstState() const { return flag; }
deba@2077
   281
    bool secondState() const { return !flag; }
deba@2077
   282
deba@2077
   283
  private:
deba@2077
   284
deba@2077
   285
    void destroy() {
deba@2077
   286
      if (flag) {
deba@2077
   287
        reinterpret_cast<First*>(data)->~First();
deba@2077
   288
      } else {
deba@2077
   289
        reinterpret_cast<Second*>(data)->~Second();
deba@2077
   290
      }
deba@2077
   291
    }
deba@2077
   292
    
deba@2077
   293
    char data[CTMax<sizeof(First), sizeof(Second)>::value];
deba@2077
   294
    bool flag;
deba@2077
   295
  };
deba@1993
   296
deba@1993
   297
  template <typename T>
deba@1993
   298
  struct Wrap {
deba@1993
   299
    const T &value;
deba@1993
   300
    Wrap(const T &t) : value(t) {}
deba@1993
   301
  };
deba@1993
   302
deba@1993
   303
  /**************** dummy class to avoid ambiguity ****************/
deba@1993
   304
deba@1993
   305
  template<int T> struct dummy { dummy(int) {} };
deba@1993
   306
deba@1993
   307
  /**************** enable_if from BOOST ****************/
deba@1993
   308
 
deba@1993
   309
  template <bool B, class T = void>
deba@1993
   310
  struct enable_if_c {
deba@1993
   311
    typedef T type;
deba@1993
   312
  };
deba@1993
   313
deba@1993
   314
  template <class T>
deba@1993
   315
  struct enable_if_c<false, T> {};
deba@1993
   316
deba@1993
   317
  template <class Cond, class T = void> 
deba@1993
   318
  struct enable_if : public enable_if_c<Cond::value, T> {};
deba@1993
   319
deba@1993
   320
  template <bool B, class T>
deba@1993
   321
  struct lazy_enable_if_c {
deba@1993
   322
    typedef typename T::type type;
deba@1993
   323
  };
deba@1993
   324
deba@1993
   325
  template <class T>
deba@1993
   326
  struct lazy_enable_if_c<false, T> {};
deba@1993
   327
deba@1993
   328
  template <class Cond, class T> 
deba@1993
   329
  struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
deba@1993
   330
deba@1993
   331
deba@1993
   332
  template <bool B, class T = void>
deba@1993
   333
  struct disable_if_c {
deba@1993
   334
    typedef T type;
deba@1993
   335
  };
deba@1993
   336
deba@1993
   337
  template <class T>
deba@1993
   338
  struct disable_if_c<true, T> {};
deba@1993
   339
deba@1993
   340
  template <class Cond, class T = void> 
deba@1993
   341
  struct disable_if : public disable_if_c<Cond::value, T> {};
deba@1993
   342
deba@1993
   343
  template <bool B, class T>
deba@1993
   344
  struct lazy_disable_if_c {
deba@1993
   345
    typedef typename T::type type;
deba@1993
   346
  };
deba@1993
   347
deba@1993
   348
  template <class T>
deba@1993
   349
  struct lazy_disable_if_c<true, T> {};
deba@1993
   350
deba@1993
   351
  template <class Cond, class T> 
deba@1993
   352
  struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
deba@1993
   353
deba@1993
   354
} // namespace lemon
deba@1993
   355
deba@1993
   356
#endif