lemon/bits/solver_bits.h
changeset 802 994c7df296c9
parent 459 ed54c0d13df0
child 877 141f9c0db4a3
child 988 8d281761dea4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/bits/solver_bits.h	Thu Dec 10 17:05:35 2009 +0100
     1.3 @@ -0,0 +1,193 @@
     1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library.
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_BITS_SOLVER_BITS_H
    1.23 +#define LEMON_BITS_SOLVER_BITS_H
    1.24 +
    1.25 +#include <vector>
    1.26 +
    1.27 +namespace lemon {
    1.28 +
    1.29 +  namespace _solver_bits {
    1.30 +
    1.31 +    class VarIndex {
    1.32 +    private:
    1.33 +      struct ItemT {
    1.34 +        int prev, next;
    1.35 +        int index;
    1.36 +      };
    1.37 +      std::vector<ItemT> items;
    1.38 +      int first_item, last_item, first_free_item;
    1.39 +
    1.40 +      std::vector<int> cross;
    1.41 +
    1.42 +    public:
    1.43 +
    1.44 +      VarIndex()
    1.45 +        : first_item(-1), last_item(-1), first_free_item(-1) {
    1.46 +      }
    1.47 +
    1.48 +      void clear() {
    1.49 +        first_item = -1;
    1.50 +        first_free_item = -1;
    1.51 +        items.clear();
    1.52 +        cross.clear();
    1.53 +      }
    1.54 +
    1.55 +      int addIndex(int idx) {
    1.56 +        int n;
    1.57 +        if (first_free_item == -1) {
    1.58 +          n = items.size();
    1.59 +          items.push_back(ItemT());
    1.60 +        } else {
    1.61 +          n = first_free_item;
    1.62 +          first_free_item = items[n].next;
    1.63 +          if (first_free_item != -1) {
    1.64 +            items[first_free_item].prev = -1;
    1.65 +          }
    1.66 +        }
    1.67 +        items[n].index = idx;
    1.68 +        if (static_cast<int>(cross.size()) <= idx) {
    1.69 +          cross.resize(idx + 1, -1);
    1.70 +        }
    1.71 +        cross[idx] = n;
    1.72 +
    1.73 +        items[n].prev = last_item;
    1.74 +        items[n].next = -1;
    1.75 +        if (last_item != -1) {
    1.76 +          items[last_item].next = n;
    1.77 +        } else {
    1.78 +          first_item = n;
    1.79 +        }
    1.80 +        last_item = n;
    1.81 +
    1.82 +        return n;
    1.83 +      }
    1.84 +
    1.85 +      int addIndex(int idx, int n) {
    1.86 +        while (n >= static_cast<int>(items.size())) {
    1.87 +          items.push_back(ItemT());
    1.88 +          items.back().prev = -1;
    1.89 +          items.back().next = first_free_item;
    1.90 +          if (first_free_item != -1) {
    1.91 +            items[first_free_item].prev = items.size() - 1;
    1.92 +          }
    1.93 +          first_free_item = items.size() - 1;
    1.94 +        }
    1.95 +        if (items[n].next != -1) {
    1.96 +          items[items[n].next].prev = items[n].prev;
    1.97 +        }
    1.98 +        if (items[n].prev != -1) {
    1.99 +          items[items[n].prev].next = items[n].next;
   1.100 +        } else {
   1.101 +          first_free_item = items[n].next;
   1.102 +        }
   1.103 +
   1.104 +        items[n].index = idx;
   1.105 +        if (static_cast<int>(cross.size()) <= idx) {
   1.106 +          cross.resize(idx + 1, -1);
   1.107 +        }
   1.108 +        cross[idx] = n;
   1.109 +
   1.110 +        items[n].prev = last_item;
   1.111 +        items[n].next = -1;
   1.112 +        if (last_item != -1) {
   1.113 +          items[last_item].next = n;
   1.114 +        } else {
   1.115 +          first_item = n;
   1.116 +        }
   1.117 +        last_item = n;
   1.118 +
   1.119 +        return n;
   1.120 +      }
   1.121 +
   1.122 +      void eraseIndex(int idx) {
   1.123 +        int n = cross[idx];
   1.124 +
   1.125 +        if (items[n].prev != -1) {
   1.126 +          items[items[n].prev].next = items[n].next;
   1.127 +        } else {
   1.128 +          first_item = items[n].next;
   1.129 +        }
   1.130 +        if (items[n].next != -1) {
   1.131 +          items[items[n].next].prev = items[n].prev;
   1.132 +        } else {
   1.133 +          last_item = items[n].prev;
   1.134 +        }
   1.135 +
   1.136 +        if (first_free_item != -1) {
   1.137 +          items[first_free_item].prev = n;
   1.138 +        }
   1.139 +        items[n].next = first_free_item;
   1.140 +        items[n].prev = -1;
   1.141 +        first_free_item = n;
   1.142 +
   1.143 +        while (!cross.empty() && cross.back() == -1) {
   1.144 +          cross.pop_back();
   1.145 +        }
   1.146 +      }
   1.147 +
   1.148 +      int maxIndex() const {
   1.149 +        return cross.size() - 1;
   1.150 +      }
   1.151 +
   1.152 +      void shiftIndices(int idx) {
   1.153 +        for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
   1.154 +          cross[i - 1] = cross[i];
   1.155 +          if (cross[i] != -1) {
   1.156 +            --items[cross[i]].index;
   1.157 +          }
   1.158 +        }
   1.159 +        cross.back() = -1;
   1.160 +        cross.pop_back();
   1.161 +        while (!cross.empty() && cross.back() == -1) {
   1.162 +          cross.pop_back();
   1.163 +        }
   1.164 +      }
   1.165 +
   1.166 +      void relocateIndex(int idx, int jdx) {
   1.167 +        cross[idx] = cross[jdx];
   1.168 +        items[cross[jdx]].index = idx;
   1.169 +        cross[jdx] = -1;
   1.170 +
   1.171 +        while (!cross.empty() && cross.back() == -1) {
   1.172 +          cross.pop_back();
   1.173 +        }
   1.174 +      }
   1.175 +
   1.176 +      int operator[](int idx) const {
   1.177 +        return cross[idx];
   1.178 +      }
   1.179 +
   1.180 +      int operator()(int fdx) const {
   1.181 +        return items[fdx].index;
   1.182 +      }
   1.183 +
   1.184 +      void firstItem(int& fdx) const {
   1.185 +        fdx = first_item;
   1.186 +      }
   1.187 +
   1.188 +      void nextItem(int& fdx) const {
   1.189 +        fdx = items[fdx].next;
   1.190 +      }
   1.191 +
   1.192 +    };
   1.193 +  }
   1.194 +}
   1.195 +
   1.196 +#endif