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
26 namespace _solver_bits {
34 std::vector<ItemT> items;
35 int first_item, last_item, first_free_item;
37 std::vector<int> cross;
42 : first_item(-1), last_item(-1), first_free_item(-1) {
52 int addIndex(int idx) {
54 if (first_free_item == -1) {
56 items.push_back(ItemT());
59 first_free_item = items[n].next;
60 if (first_free_item != -1) {
61 items[first_free_item].prev = -1;
65 if (static_cast<int>(cross.size()) <= idx) {
66 cross.resize(idx + 1, -1);
70 items[n].prev = last_item;
72 if (last_item != -1) {
73 items[last_item].next = n;
82 int addIndex(int idx, int n) {
83 while (n >= static_cast<int>(items.size())) {
84 items.push_back(ItemT());
85 items.back().prev = -1;
86 items.back().next = first_free_item;
87 if (first_free_item != -1) {
88 items[first_free_item].prev = items.size() - 1;
90 first_free_item = items.size() - 1;
92 if (items[n].next != -1) {
93 items[items[n].next].prev = items[n].prev;
95 if (items[n].prev != -1) {
96 items[items[n].prev].next = items[n].next;
98 first_free_item = items[n].next;
101 items[n].index = idx;
102 if (static_cast<int>(cross.size()) <= idx) {
103 cross.resize(idx + 1, -1);
107 items[n].prev = last_item;
109 if (last_item != -1) {
110 items[last_item].next = n;
119 void eraseIndex(int idx) {
122 if (items[n].prev != -1) {
123 items[items[n].prev].next = items[n].next;
125 first_item = items[n].next;
127 if (items[n].next != -1) {
128 items[items[n].next].prev = items[n].prev;
130 last_item = items[n].prev;
133 if (first_free_item != -1) {
134 items[first_free_item].prev = n;
136 items[n].next = first_free_item;
140 while (!cross.empty() && cross.back() == -1) {
145 int maxIndex() const {
146 return cross.size() - 1;
149 void shiftIndices(int idx) {
150 for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
151 cross[i - 1] = cross[i];
152 if (cross[i] != -1) {
153 --items[cross[i]].index;
158 while (!cross.empty() && cross.back() == -1) {
163 void relocateIndex(int idx, int jdx) {
164 cross[idx] = cross[jdx];
165 items[cross[jdx]].index = idx;
168 while (!cross.empty() && cross.back() == -1) {
173 int operator[](int idx) const {
177 int operator()(int fdx) const {
178 return items[fdx].index;
181 void firstItem(int& fdx) const {
185 void nextItem(int& fdx) const {
186 fdx = items[fdx].next;