lemon/bits/utility.h
author deba
Fri, 30 Jun 2006 12:14:36 +0000
changeset 2115 4cd528a30ec1
parent 2077 d687c0033bb5
child 2151 38ec4a930c05
permissions -rw-r--r--
Splitted graph files
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
deba@2077
   110
  template <int left, int right>
deba@2077
   111
  struct CTLess {
deba@2078
   112
    static const bool value = left < right;
deba@2077
   113
  };
deba@2077
   114
deba@2077
   115
  template <int left>
deba@2077
   116
  struct CTLess<left, 0> {
deba@2077
   117
    static const bool value = false;
deba@2077
   118
  };
deba@2077
   119
deba@2077
   120
  template <int right>
deba@2077
   121
  struct CTLess<0, right> {
deba@2077
   122
    static const bool value = true;
deba@2077
   123
  };
deba@2077
   124
deba@2077
   125
  template <>
deba@2077
   126
  struct CTLess<0, 0> {
deba@2077
   127
    static const bool value = false;
deba@2077
   128
  };
deba@2077
   129
deba@2077
   130
  template <>
deba@2077
   131
  struct CTLess<1, 1> {
deba@2077
   132
    static const bool value = false;
deba@2077
   133
  };
deba@2077
   134
deba@2077
   135
  template <bool less, int left, int right>
deba@2077
   136
  struct CTMaxImpl {
deba@2077
   137
    static const int value = left;    
deba@2077
   138
  };
deba@2077
   139
deba@2077
   140
  template <int left, int right>
deba@2077
   141
  struct CTMaxImpl<true, left, right> {
deba@2077
   142
    static const int value = right;
deba@2077
   143
  };
deba@1993
   144
  
deba@2077
   145
  template <int left, int right>
deba@2077
   146
  struct CTMax {
deba@2077
   147
    static const int value = 
deba@2077
   148
    CTMaxImpl<CTLess<left, right>::value, left, right>::value;
deba@2077
   149
  };
deba@2077
   150
deba@2077
   151
deba@2077
   152
  /// \brief Simple Variant type with two type
deba@2077
   153
  ///
deba@2077
   154
  /// Simple Variant type with two type
deba@2077
   155
  template <typename _First, typename _Second>
deba@2077
   156
  class BiVariant {
deba@2077
   157
  public:
deba@2077
   158
deba@2077
   159
    typedef _First First;
deba@2077
   160
    typedef _Second Second;
deba@2077
   161
deba@2077
   162
    struct WrongStateError : public lemon::LogicError {
deba@2077
   163
    public:
deba@2077
   164
      virtual const char* exceptionName() const {
deba@2077
   165
        return "lemon::BiVariant::WrongStateError";
deba@2077
   166
      }
deba@2077
   167
    };
deba@2077
   168
deba@2077
   169
    BiVariant() {
deba@2077
   170
      flag = true;
deba@2077
   171
      new(reinterpret_cast<First*>(data)) First();
deba@2077
   172
    }
deba@2077
   173
deba@2077
   174
    BiVariant(const First& first) {
deba@2077
   175
      flag = true;
deba@2077
   176
      new(reinterpret_cast<First*>(data)) First(first);
deba@2077
   177
    }
deba@2077
   178
deba@2077
   179
    BiVariant(const Second& second) {
deba@2077
   180
      flag = false;
deba@2077
   181
      new(reinterpret_cast<Second*>(data)) Second(second);
deba@2077
   182
    }
deba@2077
   183
deba@2077
   184
    BiVariant(const BiVariant& bivariant) {
deba@2077
   185
      flag = bivariant.flag;
deba@2077
   186
      if (flag) {
deba@2077
   187
        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
deba@2077
   188
      } else {
deba@2077
   189
        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
deba@2077
   190
      }
deba@2077
   191
    }
deba@2077
   192
deba@2077
   193
    ~BiVariant() {
deba@2077
   194
      destroy();
deba@2077
   195
    }
deba@2077
   196
deba@2077
   197
    BiVariant& setFirst() {
deba@2077
   198
      destroy();
deba@2077
   199
      flag = true;
deba@2077
   200
      new(reinterpret_cast<First*>(data)) First();   
deba@2077
   201
      return *this;
deba@2077
   202
    }
deba@2077
   203
deba@2077
   204
    BiVariant& setFirst(const First& first) {
deba@2077
   205
      destroy();
deba@2077
   206
      flag = true;
deba@2077
   207
      new(reinterpret_cast<First*>(data)) First(first);   
deba@2077
   208
      return *this;
deba@2077
   209
    }
deba@2077
   210
deba@2077
   211
    BiVariant& setSecond() {
deba@2077
   212
      destroy();
deba@2077
   213
      flag = false;
deba@2077
   214
      new(reinterpret_cast<Second*>(data)) Second();   
deba@2077
   215
      return *this;
deba@2077
   216
    }
deba@2077
   217
deba@2077
   218
    BiVariant& setSecond(const Second& second) {
deba@2077
   219
      destroy();
deba@2077
   220
      flag = false;
deba@2077
   221
      new(reinterpret_cast<Second*>(data)) Second(second);   
deba@2077
   222
      return *this;
deba@2077
   223
    }
deba@2077
   224
deba@2077
   225
    BiVariant& operator=(const First& first) {
deba@2077
   226
      return setFirst(first);
deba@2077
   227
    }
deba@2077
   228
deba@2077
   229
    BiVariant& operator=(const Second& second) {
deba@2077
   230
      return setSecond(second);
deba@2077
   231
    }
deba@2077
   232
deba@2077
   233
deba@2077
   234
    BiVariant& operator=(const BiVariant& bivariant) {
deba@2077
   235
      if (this == &bivariant) return *this;
deba@2077
   236
      destroy();
deba@2077
   237
      flag = bivariant.flag;
deba@2077
   238
      if (flag) {
deba@2077
   239
        new(reinterpret_cast<First*>(data)) First(bivariant.first());      
deba@2077
   240
      } else {
deba@2077
   241
        new(reinterpret_cast<Second*>(data)) Second(bivariant.second());      
deba@2077
   242
      }
deba@2077
   243
      return *this;
deba@2077
   244
    }
deba@2077
   245
deba@2077
   246
    First& first() {
deba@2077
   247
      LEMON_ASSERT(flag, WrongStateError());
deba@2077
   248
      return *reinterpret_cast<First*>(data); 
deba@2077
   249
    }
deba@2077
   250
deba@2077
   251
    const First& first() const { 
deba@2077
   252
      LEMON_ASSERT(flag, WrongStateError());
deba@2077
   253
      return *reinterpret_cast<const First*>(data); 
deba@2077
   254
    }
deba@2077
   255
deba@2077
   256
    operator First&() { return first(); }
deba@2077
   257
    operator const First&() const { return first(); }
deba@2077
   258
deba@2077
   259
    Second& second() { 
deba@2077
   260
      LEMON_ASSERT(!flag, WrongStateError());
deba@2077
   261
      return *reinterpret_cast<Second*>(data); 
deba@2077
   262
    }
deba@2077
   263
deba@2077
   264
    const Second& second() const { 
deba@2077
   265
      LEMON_ASSERT(!flag, WrongStateError());
deba@2077
   266
      return *reinterpret_cast<const Second*>(data); 
deba@2077
   267
    }
deba@2077
   268
deba@2077
   269
    operator Second&() { return second(); }
deba@2077
   270
    operator const Second&() const { return second(); }
deba@2077
   271
deba@2077
   272
    bool firstState() const { return flag; }
deba@2077
   273
    bool secondState() const { return !flag; }
deba@2077
   274
deba@2077
   275
  private:
deba@2077
   276
deba@2077
   277
    void destroy() {
deba@2077
   278
      if (flag) {
deba@2077
   279
        reinterpret_cast<First*>(data)->~First();
deba@2077
   280
      } else {
deba@2077
   281
        reinterpret_cast<Second*>(data)->~Second();
deba@2077
   282
      }
deba@2077
   283
    }
deba@2077
   284
    
deba@2077
   285
    char data[CTMax<sizeof(First), sizeof(Second)>::value];
deba@2077
   286
    bool flag;
deba@2077
   287
  };
deba@1993
   288
deba@1993
   289
  template <typename T>
deba@1993
   290
  struct Wrap {
deba@1993
   291
    const T &value;
deba@1993
   292
    Wrap(const T &t) : value(t) {}
deba@1993
   293
  };
deba@1993
   294
deba@1993
   295
  /**************** dummy class to avoid ambiguity ****************/
deba@1993
   296
deba@1993
   297
  template<int T> struct dummy { dummy(int) {} };
deba@1993
   298
deba@1993
   299
  /**************** enable_if from BOOST ****************/
deba@1993
   300
 
deba@1993
   301
  template <bool B, class T = void>
deba@1993
   302
  struct enable_if_c {
deba@1993
   303
    typedef T type;
deba@1993
   304
  };
deba@1993
   305
deba@1993
   306
  template <class T>
deba@1993
   307
  struct enable_if_c<false, T> {};
deba@1993
   308
deba@1993
   309
  template <class Cond, class T = void> 
deba@1993
   310
  struct enable_if : public enable_if_c<Cond::value, T> {};
deba@1993
   311
deba@1993
   312
  template <bool B, class T>
deba@1993
   313
  struct lazy_enable_if_c {
deba@1993
   314
    typedef typename T::type type;
deba@1993
   315
  };
deba@1993
   316
deba@1993
   317
  template <class T>
deba@1993
   318
  struct lazy_enable_if_c<false, T> {};
deba@1993
   319
deba@1993
   320
  template <class Cond, class T> 
deba@1993
   321
  struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
deba@1993
   322
deba@1993
   323
deba@1993
   324
  template <bool B, class T = void>
deba@1993
   325
  struct disable_if_c {
deba@1993
   326
    typedef T type;
deba@1993
   327
  };
deba@1993
   328
deba@1993
   329
  template <class T>
deba@1993
   330
  struct disable_if_c<true, T> {};
deba@1993
   331
deba@1993
   332
  template <class Cond, class T = void> 
deba@1993
   333
  struct disable_if : public disable_if_c<Cond::value, T> {};
deba@1993
   334
deba@1993
   335
  template <bool B, class T>
deba@1993
   336
  struct lazy_disable_if_c {
deba@1993
   337
    typedef typename T::type type;
deba@1993
   338
  };
deba@1993
   339
deba@1993
   340
  template <class T>
deba@1993
   341
  struct lazy_disable_if_c<true, T> {};
deba@1993
   342
deba@1993
   343
  template <class Cond, class T> 
deba@1993
   344
  struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
deba@1993
   345
deba@1993
   346
} // namespace lemon
deba@1993
   347
deba@1993
   348
#endif