lemon/bits/solver_bits.h
changeset 459 ed54c0d13df0
child 519 c786cd201266
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/bits/solver_bits.h	Tue Dec 02 22:48:28 2008 +0100
     1.3 @@ -0,0 +1,191 @@
     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 +namespace lemon {
    1.26 +
    1.27 +  namespace _solver_bits {
    1.28 +
    1.29 +    class VarIndex {
    1.30 +    private:
    1.31 +      struct ItemT {
    1.32 +        int prev, next;
    1.33 +        int index;
    1.34 +      };
    1.35 +      std::vector<ItemT> items;
    1.36 +      int first_item, last_item, first_free_item;
    1.37 +
    1.38 +      std::vector<int> cross;
    1.39 +
    1.40 +    public:
    1.41 +
    1.42 +      VarIndex()
    1.43 +        : first_item(-1), last_item(-1), first_free_item(-1) {
    1.44 +      }
    1.45 +
    1.46 +      void clear() {
    1.47 +        first_item = -1;
    1.48 +        first_free_item = -1;
    1.49 +        items.clear();
    1.50 +        cross.clear();
    1.51 +      }
    1.52 +
    1.53 +      int addIndex(int idx) {
    1.54 +        int n;
    1.55 +        if (first_free_item == -1) {
    1.56 +          n = items.size();
    1.57 +          items.push_back(ItemT());
    1.58 +        } else {
    1.59 +          n = first_free_item;
    1.60 +          first_free_item = items[n].next;
    1.61 +          if (first_free_item != -1) {
    1.62 +            items[first_free_item].prev = -1;
    1.63 +          }
    1.64 +        }
    1.65 +        items[n].index = idx;
    1.66 +        if (static_cast<int>(cross.size()) <= idx) {
    1.67 +          cross.resize(idx + 1, -1);
    1.68 +        }
    1.69 +        cross[idx] = n;
    1.70 +
    1.71 +        items[n].prev = last_item;
    1.72 +        items[n].next = -1;
    1.73 +        if (last_item != -1) {
    1.74 +          items[last_item].next = n;
    1.75 +        } else {
    1.76 +          first_item = n;
    1.77 +        }
    1.78 +        last_item = n;
    1.79 +
    1.80 +        return n;
    1.81 +      }
    1.82 +
    1.83 +      int addIndex(int idx, int n) {
    1.84 +        while (n >= static_cast<int>(items.size())) {
    1.85 +          items.push_back(ItemT());
    1.86 +          items.back().prev = -1;
    1.87 +          items.back().next = first_free_item;
    1.88 +          if (first_free_item != -1) {
    1.89 +            items[first_free_item].prev = items.size() - 1;
    1.90 +          }
    1.91 +          first_free_item = items.size() - 1;
    1.92 +        }
    1.93 +        if (items[n].next != -1) {
    1.94 +          items[items[n].next].prev = items[n].prev;
    1.95 +        }
    1.96 +        if (items[n].prev != -1) {
    1.97 +          items[items[n].prev].next = items[n].next;
    1.98 +        } else {
    1.99 +          first_free_item = items[n].next;
   1.100 +        }
   1.101 +
   1.102 +        items[n].index = idx;
   1.103 +        if (static_cast<int>(cross.size()) <= idx) {
   1.104 +          cross.resize(idx + 1, -1);
   1.105 +        }
   1.106 +        cross[idx] = n;
   1.107 +
   1.108 +        items[n].prev = last_item;
   1.109 +        items[n].next = -1;
   1.110 +        if (last_item != -1) {
   1.111 +          items[last_item].next = n;
   1.112 +        } else {
   1.113 +          first_item = n;
   1.114 +        }
   1.115 +        last_item = n;
   1.116 +
   1.117 +        return n;
   1.118 +      }
   1.119 +
   1.120 +      void eraseIndex(int idx) {
   1.121 +        int n = cross[idx];
   1.122 +
   1.123 +        if (items[n].prev != -1) {
   1.124 +          items[items[n].prev].next = items[n].next;
   1.125 +        } else {
   1.126 +          first_item = items[n].next;
   1.127 +        }
   1.128 +        if (items[n].next != -1) {
   1.129 +          items[items[n].next].prev = items[n].prev;
   1.130 +        } else {
   1.131 +          last_item = items[n].prev;
   1.132 +        }
   1.133 +
   1.134 +        if (first_free_item != -1) {
   1.135 +          items[first_free_item].prev = n;
   1.136 +        }
   1.137 +        items[n].next = first_free_item;
   1.138 +        items[n].prev = -1;
   1.139 +        first_free_item = n;
   1.140 +
   1.141 +        while (!cross.empty() && cross.back() == -1) {
   1.142 +          cross.pop_back();
   1.143 +        }
   1.144 +      }
   1.145 +
   1.146 +      int maxIndex() const {
   1.147 +        return cross.size() - 1;
   1.148 +      }
   1.149 +
   1.150 +      void shiftIndices(int idx) {
   1.151 +        for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
   1.152 +          cross[i - 1] = cross[i];
   1.153 +          if (cross[i] != -1) {
   1.154 +            --items[cross[i]].index;
   1.155 +          }
   1.156 +        }
   1.157 +        cross.back() = -1;
   1.158 +        cross.pop_back();
   1.159 +        while (!cross.empty() && cross.back() == -1) {
   1.160 +          cross.pop_back();
   1.161 +        }
   1.162 +      }
   1.163 +
   1.164 +      void relocateIndex(int idx, int jdx) {
   1.165 +        cross[idx] = cross[jdx];
   1.166 +        items[cross[jdx]].index = idx;
   1.167 +        cross[jdx] = -1;
   1.168 +
   1.169 +        while (!cross.empty() && cross.back() == -1) {
   1.170 +          cross.pop_back();
   1.171 +        }
   1.172 +      }
   1.173 +
   1.174 +      int operator[](int idx) const {
   1.175 +        return cross[idx];
   1.176 +      }
   1.177 +
   1.178 +      int operator()(int fdx) const {
   1.179 +        return items[fdx].index;
   1.180 +      }
   1.181 +
   1.182 +      void firstItem(int& fdx) const {
   1.183 +        fdx = first_item;
   1.184 +      }
   1.185 +
   1.186 +      void nextItem(int& fdx) const {
   1.187 +        fdx = items[fdx].next;
   1.188 +      }
   1.189 +
   1.190 +    };
   1.191 +  }
   1.192 +}
   1.193 +
   1.194 +#endif