1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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) {
53 int addIndex(int idx) {
55 if (first_free_item == -1) {
57 items.push_back(ItemT());
60 first_free_item = items[n].next;
61 if (first_free_item != -1) {
62 items[first_free_item].prev = -1;
66 if (static_cast<int>(cross.size()) <= idx) {
67 cross.resize(idx + 1, -1);
71 items[n].prev = last_item;
73 if (last_item != -1) {
74 items[last_item].next = n;
83 int addIndex(int idx, int n) {
84 while (n >= static_cast<int>(items.size())) {
85 items.push_back(ItemT());
86 items.back().prev = -1;
87 items.back().next = first_free_item;
88 if (first_free_item != -1) {
89 items[first_free_item].prev = items.size() - 1;
91 first_free_item = items.size() - 1;
93 if (items[n].next != -1) {
94 items[items[n].next].prev = items[n].prev;
96 if (items[n].prev != -1) {
97 items[items[n].prev].next = items[n].next;
99 first_free_item = items[n].next;
102 items[n].index = idx;
103 if (static_cast<int>(cross.size()) <= idx) {
104 cross.resize(idx + 1, -1);
108 items[n].prev = last_item;
110 if (last_item != -1) {
111 items[last_item].next = n;
120 void eraseIndex(int idx) {
123 if (items[n].prev != -1) {
124 items[items[n].prev].next = items[n].next;
126 first_item = items[n].next;
128 if (items[n].next != -1) {
129 items[items[n].next].prev = items[n].prev;
131 last_item = items[n].prev;
134 if (first_free_item != -1) {
135 items[first_free_item].prev = n;
137 items[n].next = first_free_item;
141 while (!cross.empty() && cross.back() == -1) {
146 int maxIndex() const {
147 return cross.size() - 1;
150 void shiftIndices(int idx) {
151 for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
152 cross[i - 1] = cross[i];
153 if (cross[i] != -1) {
154 --items[cross[i]].index;
159 while (!cross.empty() && cross.back() == -1) {
164 void relocateIndex(int idx, int jdx) {
165 cross[idx] = cross[jdx];
166 items[cross[jdx]].index = idx;
169 while (!cross.empty() && cross.back() == -1) {
174 int operator[](int idx) const {
178 int operator()(int fdx) const {
179 return items[fdx].index;
182 void firstItem(int& fdx) const {
186 void nextItem(int& fdx) const {
187 fdx = items[fdx].next;