bitset.cc 15.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ortools/util/bitset.h"

#include "ortools/base/commandlineflags.h"
#include "ortools/base/logging.h"

DEFINE_int32(bitset_small_bitset_count, 8,
             "threshold to count bits with buckets");

namespace operations_research {

// ---------- Bit Operations ----------

#define BIT_COUNT_RANGE(size, zero)                                           \
  uint##size BitCountRange##size(const uint##size* const bits,                \
                                 uint##size start, uint##size end) {          \
    if (end - start > FLAGS_bitset_small_bitset_count) {                      \
      const int offset_start = BitOffset##size(start);                        \
      const int pos_start = BitPos##size(start);                              \
      const int offset_end = BitOffset##size(end);                            \
      const int pos_end = BitPos##size(end);                                  \
      if (offset_end == offset_start) {                                       \
        return BitCount##size(bits[offset_start] &                            \
                              OneRange##size(pos_start, pos_end));            \
      } else {                                                                \
        uint##size bit_count = zero;                                          \
        bit_count +=                                                          \
            BitCount##size(bits[offset_start] & IntervalUp##size(pos_start)); \
        for (int offset = offset_start + 1; offset < offset_end; ++offset) {  \
          bit_count += BitCount##size(bits[offset]);                          \
        }                                                                     \
        bit_count +=                                                          \
            BitCount##size(bits[offset_end] & IntervalDown##size(pos_end));   \
        return bit_count;                                                     \
      }                                                                       \
    } else {                                                                  \
      uint##size bit_count = zero;                                            \
      for (uint##size i = start; i <= end; ++i) {                             \
        bit_count += IsBitSet##size(bits, i);                                 \
      }                                                                       \
      return bit_count;                                                       \
    }                                                                         \
  }

BIT_COUNT_RANGE(64, GG_ULONGLONG(0))
BIT_COUNT_RANGE(32, 0U)

#undef BIT_COUNT_RANGE

#define IS_EMPTY_RANGE(size)                                               \
  bool IsEmptyRange##size(const uint##size* const bits, uint##size start,  \
                          uint##size end) {                                \
    const int offset_start = BitOffset##size(start);                       \
    const int pos_start = BitPos##size(start);                             \
    const int offset_end = BitOffset##size(end);                           \
    const int pos_end = BitPos##size(end);                                 \
    if (offset_end == offset_start) {                                      \
      if (bits[offset_start] & OneRange##size(pos_start, pos_end)) {       \
        return false;                                                      \
      }                                                                    \
    } else {                                                               \
      if (bits[offset_start] & IntervalUp##size(pos_start)) {              \
        return false;                                                      \
      }                                                                    \
      for (int offset = offset_start + 1; offset < offset_end; ++offset) { \
        if (bits[offset]) {                                                \
          return false;                                                    \
        }                                                                  \
      }                                                                    \
      if (bits[offset_end] & IntervalDown##size(pos_end)) {                \
        return false;                                                      \
      }                                                                    \
    }                                                                      \
    return true;                                                           \
  }

IS_EMPTY_RANGE(64)
IS_EMPTY_RANGE(32)

#undef IS_EMPTY_RANGE

#define LEAST_SIGNIFCANT_BIT_POSITION(size)                                  \
  int##size LeastSignificantBitPosition##size(                               \
      const uint##size* const bits, uint##size start, uint##size end) {      \
    DCHECK_LE(start, end);                                                   \
    if (IsBitSet##size(bits, start)) {                                       \
      return start;                                                          \
    }                                                                        \
    const int offset_start = BitOffset##size(start);                         \
    const int offset_end = BitOffset##size(end);                             \
    const int pos_start = BitPos##size(start);                               \
    if (offset_start == offset_end) {                                        \
      const int pos_end = BitPos##size(end);                                 \
      const uint##size active_range =                                        \
          bits[offset_start] & OneRange##size(pos_start, pos_end);           \
      if (active_range) {                                                    \
        return BitShift##size(offset_start) +                                \
               LeastSignificantBitPosition##size(active_range);              \
      }                                                                      \
      return -1;                                                             \
    } else {                                                                 \
      const uint##size start_mask =                                          \
          bits[offset_start] & IntervalUp##size(pos_start);                  \
      if (start_mask) {                                                      \
        return BitShift##size(offset_start) +                                \
               LeastSignificantBitPosition##size(start_mask);                \
      } else {                                                               \
        for (int offset = offset_start + 1; offset < offset_end; ++offset) { \
          if (bits[offset]) {                                                \
            return BitShift##size(offset) +                                  \
                   LeastSignificantBitPosition##size(bits[offset]);          \
          }                                                                  \
        }                                                                    \
        const int pos_end = BitPos##size(end);                               \
        const uint##size active_range =                                      \
            bits[offset_end] & IntervalDown##size(pos_end);                  \
        if (active_range) {                                                  \
          return BitShift##size(offset_end) +                                \
                 LeastSignificantBitPosition##size(active_range);            \
        } else {                                                             \
          return -1;                                                         \
        }                                                                    \
      }                                                                      \
    }                                                                        \
  }

LEAST_SIGNIFCANT_BIT_POSITION(64)
LEAST_SIGNIFCANT_BIT_POSITION(32)

#undef LEAST_SIGNIFCANT_BIT_POSITION

#define MOST_SIGNIFICANT_BIT_POSITION(size)                                  \
  int##size MostSignificantBitPosition##size(                                \
      const uint##size* const bits, uint##size start, uint##size end) {      \
    DCHECK_GE(end, start);                                                   \
    if (IsBitSet##size(bits, end)) {                                         \
      return end;                                                            \
    }                                                                        \
    const int offset_start = BitOffset##size(start);                         \
    const int offset_end = BitOffset##size(end);                             \
    const int pos_end = BitPos##size(end);                                   \
    if (offset_start == offset_end) {                                        \
      const int pos_start = BitPos##size(start);                             \
      const uint##size active_range =                                        \
          bits[offset_start] & OneRange##size(pos_start, pos_end);           \
      if (active_range) {                                                    \
        return BitShift##size(offset_end) +                                  \
               MostSignificantBitPosition##size(active_range);               \
      } else {                                                               \
        return -1;                                                           \
      }                                                                      \
    } else {                                                                 \
      const uint##size end_mask =                                            \
          bits[offset_end] & IntervalDown##size(pos_end);                    \
      if (end_mask) {                                                        \
        return BitShift##size(offset_end) +                                  \
               MostSignificantBitPosition##size(end_mask);                   \
      } else {                                                               \
        for (int offset = offset_end - 1; offset > offset_start; --offset) { \
          if (bits[offset]) {                                                \
            return BitShift##size(offset) +                                  \
                   MostSignificantBitPosition##size(bits[offset]);           \
          }                                                                  \
        }                                                                    \
        const int pos_start = BitPos##size(start);                           \
        const uint##size active_range =                                      \
            bits[offset_start] & IntervalUp##size(pos_start);                \
        if (active_range) {                                                  \
          return BitShift##size(offset_start) +                              \
                 MostSignificantBitPosition##size(active_range);             \
        } else {                                                             \
          return -1;                                                         \
        }                                                                    \
      }                                                                      \
    }                                                                        \
  }

MOST_SIGNIFICANT_BIT_POSITION(64)
MOST_SIGNIFICANT_BIT_POSITION(32)

#undef MOST_SIGNIFICANT_BIT_POSITION

#define UNSAFE_LEAST_SIGNIFICANT_BIT_POSITION(size)                       \
  int##size UnsafeLeastSignificantBitPosition##size(                      \
      const uint##size* const bits, uint##size start, uint##size end) {   \
    DCHECK_LE(start, end);                                                \
    DCHECK(IsBitSet##size(bits, end));                                    \
    if (IsBitSet##size(bits, start)) {                                    \
      return start;                                                       \
    }                                                                     \
    const int offset_start = BitOffset##size(start);                      \
    const int offset_end = BitOffset##size(end);                          \
    const int pos_start = BitPos##size(start);                            \
    const uint##size start_mask =                                         \
        bits[offset_start] & IntervalUp##size(pos_start);                 \
    if (start_mask) {                                                     \
      return BitShift##size(offset_start) +                               \
             LeastSignificantBitPosition##size(start_mask);               \
    }                                                                     \
    for (int offset = offset_start + 1; offset <= offset_end; ++offset) { \
      if (bits[offset]) {                                                 \
        return BitShift##size(offset) +                                   \
               LeastSignificantBitPosition##size(bits[offset]);           \
      }                                                                   \
    }                                                                     \
    return -1;                                                            \
  }

UNSAFE_LEAST_SIGNIFICANT_BIT_POSITION(64)
UNSAFE_LEAST_SIGNIFICANT_BIT_POSITION(32)

#undef UNSAFE_LEAST_SIGNIFICANT_BIT_POSITION

#define UNSAFE_MOST_SIGNIFICANT_BIT_POSITION(size)                        \
  int##size UnsafeMostSignificantBitPosition##size(                       \
      const uint##size* const bits, uint##size start, uint##size end) {   \
    DCHECK_GE(end, start);                                                \
    DCHECK(IsBitSet##size(bits, start));                                  \
    if (IsBitSet##size(bits, end)) {                                      \
      return end;                                                         \
    }                                                                     \
    const int offset_start = BitOffset##size(start);                      \
    const int offset_end = BitOffset##size(end);                          \
    const int pos_end = BitPos##size(end);                                \
    const uint##size end_mask =                                           \
        bits[offset_end] & IntervalDown##size(pos_end);                   \
    if (end_mask) {                                                       \
      return BitShift##size(offset_end) +                                 \
             MostSignificantBitPosition##size(end_mask);                  \
    }                                                                     \
    for (int offset = offset_end - 1; offset >= offset_start; --offset) { \
      if (bits[offset]) {                                                 \
        return BitShift##size(offset) +                                   \
               MostSignificantBitPosition##size(bits[offset]);            \
      }                                                                   \
    }                                                                     \
    return -1;                                                            \
  }

UNSAFE_MOST_SIGNIFICANT_BIT_POSITION(64)
UNSAFE_MOST_SIGNIFICANT_BIT_POSITION(32)

#undef UNSAFE_MOST_SIGNIFICANT_BIT_POSITION

}  // namespace operations_research