1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_BITS_SOLVER_BITS_H
20 #define LEMON_BITS_SOLVER_BITS_H
24 namespace _solver_bits {
32 std::vector<ItemT> items;
33 int first_item, last_item, first_free_item;
35 std::vector<int> cross;
40 : first_item(-1), last_item(-1), first_free_item(-1) {
50 int addIndex(int idx) {
52 if (first_free_item == -1) {
54 items.push_back(ItemT());
57 first_free_item = items[n].next;
58 if (first_free_item != -1) {
59 items[first_free_item].prev = -1;
63 if (static_cast<int>(cross.size()) <= idx) {
64 cross.resize(idx + 1, -1);
68 items[n].prev = last_item;
70 if (last_item != -1) {
71 items[last_item].next = n;
80 int addIndex(int idx, int n) {
81 while (n >= static_cast<int>(items.size())) {
82 items.push_back(ItemT());
83 items.back().prev = -1;
84 items.back().next = first_free_item;
85 if (first_free_item != -1) {
86 items[first_free_item].prev = items.size() - 1;
88 first_free_item = items.size() - 1;
90 if (items[n].next != -1) {
91 items[items[n].next].prev = items[n].prev;
93 if (items[n].prev != -1) {
94 items[items[n].prev].next = items[n].next;
96 first_free_item = items[n].next;
100 if (static_cast<int>(cross.size()) <= idx) {
101 cross.resize(idx + 1, -1);
105 items[n].prev = last_item;
107 if (last_item != -1) {
108 items[last_item].next = n;
117 void eraseIndex(int idx) {
120 if (items[n].prev != -1) {
121 items[items[n].prev].next = items[n].next;
123 first_item = items[n].next;
125 if (items[n].next != -1) {
126 items[items[n].next].prev = items[n].prev;
128 last_item = items[n].prev;
131 if (first_free_item != -1) {
132 items[first_free_item].prev = n;
134 items[n].next = first_free_item;
138 while (!cross.empty() && cross.back() == -1) {
143 int maxIndex() const {
144 return cross.size() - 1;
147 void shiftIndices(int idx) {
148 for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
149 cross[i - 1] = cross[i];
150 if (cross[i] != -1) {
151 --items[cross[i]].index;
156 while (!cross.empty() && cross.back() == -1) {
161 void relocateIndex(int idx, int jdx) {
162 cross[idx] = cross[jdx];
163 items[cross[jdx]].index = idx;
166 while (!cross.empty() && cross.back() == -1) {
171 int operator[](int idx) const {
175 int operator()(int fdx) const {
176 return items[fdx].index;
179 void firstItem(int& fdx) const {
183 void nextItem(int& fdx) const {
184 fdx = items[fdx].next;