<commit_before>/********************************************************************************
Copyright (c) 2012, <NAME>.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

    * Neither the name of libcds nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

********************************************************************************/


#include <libcds/array.h>
#include <libcds/libcds.h>

#include <fstream>
#include <algorithm>

namespace cds {
namespace basic {

using std::istream;
using std::ostream;

using cds::basic::Array;
using cds::basic::ArrayTpl;

Array *Array::Create(cds_word *A, cds_word i, cds_word j, cds_word bpe) {
  cds_word max_value = 0;
  for (cds_word k = i; k <= j; k++) {
    max_value = max(max_value, A[k]);
  }
  Array *ret = Create(j - i + 1, max(msb(max_value), bpe));
  for (cds_word k = i; k <= j; k++) {
    assert(A[k] <= max_value);
    ret->SetField(k - i, A[k]);
  }
  return ret;
}

template <cds_word bpe> void ArrayTpl<bpe>::Save(ostream &out) const {
  SaveValue(out, bpe);
  SaveValue(out, length_);
  cds_word uint_length = WordsLength(length_, bpe);
  SaveValue(out, data_, uint_length);
}

template <cds_word bpe> ArrayTpl<bpe>::ArrayTpl(istream &input) {
  length_ = LoadValue<cds_word>(input);
  cds_word uint_length = WordsLength(length_, bpe);
  data_ = LoadValue<cds_word>(input, uint_length);
}

template <cds_word bpe> ArrayTpl<bpe> *ArrayTpl<bpe>::Load(istream &input) {
  cds_word id = LoadValue<cds_word>(input);
  if (id != bpe) {
    return NULL;
  }
  return new ArrayTpl<bpe>(input);
}

template <cds_word bpe> ArrayTpl<bpe>::ArrayTpl(cds_word n) {
  length_ = n;
  InitData();
}

template <cds_word bpe> ArrayTpl<bpe>::ArrayTpl(cds_word *A, cds_word i, cds_word j) {
  length_ = j - i + 1;
  InitData();
  for (cds_word k = i; k <= j; k++) {
    SetField(k - i, A[k]);
  }
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::GetSize() const {
  return sizeof(cds_word) * WordsLength(length_, bpe)
         + sizeof(cds_word) * 4
         + sizeof(cds_word *);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::GetLength() const {
  return length_;
}


template <cds_word bpe> ArrayTpl<bpe>::~ArrayTpl() {
  delete [] data_;
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::GetMax() const {
  cds_word max_value = 0;  // default max
  for (cds_word i = 0; i < length_; i++) {
    max_value = max(max_value, GetField(i));
  }
  return max_value;
}

// Implementation based on the STL lower_bound implementation.
template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBound(cds_word value, cds_word ini, cds_word fin) const {
  assert(ini <= fin);
  assert(fin <= length_);
  cds_word count, step;
  count = fin - ini;

  while (count > 0) {
    step = count / 2;
    cds_word pos = ini + step;
    if (GetField(pos) < value) {
      ini = pos + 1;
      count -= step + 1;
    } else {
      count = step;
    }
  }
  return ini;
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBound(cds_word value) const {
  return LowerBound(value, 0, length_);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBoundExp(cds_word value, cds_word ini, cds_word fin) const {
  cds_word sum = 1;
  while (ini + sum < fin) {
    if (GetField(ini + sum) >= value) {
      return LowerBound(value, ini, ini + sum);
    }
    ini += sum;
    sum *= 2;
  }
  return LowerBound(value, ini, fin);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBoundExp(cds_word value) const {
  return LowerBoundExp(value, 0, length_);
}

// Also based on the STL version.
template <cds_word bpe> cds_word ArrayTpl<bpe>::UpperBound(cds_word value, cds_word ini, cds_word fin) const {
  assert(ini <= fin);
  assert(fin <= length_);
  cds_word count, step;
  count = fin - ini;

  while (count > 0) {
    step = count / 2;
    cds_word pos = ini + step;
    if (!(value < GetField(pos))) {
      ini = pos + 1;
      count -= step + 1;
    } else {
      count = step;
    }
  }
  return ini;
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::UpperBound(cds_word value) const {
  return UpperBound(value, 0, length_);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::BinarySearch(cds_word value, cds_word ini, cds_word fin) const {
  assert(ini <= fin);
  assert(fin <= length_);
  cds_word pos = LowerBound(value, ini, fin);
  if (pos != length_ && !(value < GetField(pos))) {
    return pos;
  } else {
    return fin;
  }
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::BinarySearch(cds_word value) const {
  return BinarySearch(value, 0, length_);
}<commit_msg>Fixed a similar bug in Array::BinarySearch (boundary issues)<commit_after>/********************************************************************************
Copyright (c) 2012, <NAME>.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

    * Neither the name of libcds nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

********************************************************************************/


#include <libcds/array.h>
#include <libcds/libcds.h>

#include <fstream>
#include <algorithm>

namespace cds {
namespace basic {

using std::istream;
using std::ostream;

using cds::basic::Array;
using cds::basic::ArrayTpl;

Array *Array::Create(cds_word *A, cds_word i, cds_word j, cds_word bpe) {
  cds_word max_value = 0;
  for (cds_word k = i; k <= j; k++) {
    max_value = max(max_value, A[k]);
  }
  Array *ret = Create(j - i + 1, max(msb(max_value), bpe));
  for (cds_word k = i; k <= j; k++) {
    assert(A[k] <= max_value);
    ret->SetField(k - i, A[k]);
  }
  return ret;
}

template <cds_word bpe> void ArrayTpl<bpe>::Save(ostream &out) const {
  SaveValue(out, bpe);
  SaveValue(out, length_);
  cds_word uint_length = WordsLength(length_, bpe);
  SaveValue(out, data_, uint_length);
}

template <cds_word bpe> ArrayTpl<bpe>::ArrayTpl(istream &input) {
  length_ = LoadValue<cds_word>(input);
  cds_word uint_length = WordsLength(length_, bpe);
  data_ = LoadValue<cds_word>(input, uint_length);
}

template <cds_word bpe> ArrayTpl<bpe> *ArrayTpl<bpe>::Load(istream &input) {
  cds_word id = LoadValue<cds_word>(input);
  if (id != bpe) {
    return NULL;
  }
  return new ArrayTpl<bpe>(input);
}

template <cds_word bpe> ArrayTpl<bpe>::ArrayTpl(cds_word n) {
  length_ = n;
  InitData();
}

template <cds_word bpe> ArrayTpl<bpe>::ArrayTpl(cds_word *A, cds_word i, cds_word j) {
  length_ = j - i + 1;
  InitData();
  for (cds_word k = i; k <= j; k++) {
    SetField(k - i, A[k]);
  }
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::GetSize() const {
  return sizeof(cds_word) * WordsLength(length_, bpe)
         + sizeof(cds_word) * 4
         + sizeof(cds_word *);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::GetLength() const {
  return length_;
}


template <cds_word bpe> ArrayTpl<bpe>::~ArrayTpl() {
  delete [] data_;
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::GetMax() const {
  cds_word max_value = 0;  // default max
  for (cds_word i = 0; i < length_; i++) {
    max_value = max(max_value, GetField(i));
  }
  return max_value;
}

// Implementation based on the STL lower_bound implementation.
template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBound(cds_word value, cds_word ini, cds_word fin) const {
  assert(ini <= fin);
  assert(fin <= length_);
  cds_word count, step;
  count = fin - ini;

  while (count > 0) {
    step = count / 2;
    cds_word pos = ini + step;
    if (GetField(pos) < value) {
      ini = pos + 1;
      count -= step + 1;
    } else {
      count = step;
    }
  }
  return ini;
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBound(cds_word value) const {
  return LowerBound(value, 0, length_);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBoundExp(cds_word value, cds_word ini, cds_word fin) const {
  cds_word sum = 1;
  while (ini + sum < fin) {
    if (GetField(ini + sum) >= value) {
      return LowerBound(value, ini, ini + sum);
    }
    ini += sum;
    sum *= 2;
  }
  return LowerBound(value, ini, fin);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::LowerBoundExp(cds_word value) const {
  return LowerBoundExp(value, 0, length_);
}

// Also based on the STL version.
template <cds_word bpe> cds_word ArrayTpl<bpe>::UpperBound(cds_word value, cds_word ini, cds_word fin) const {
  assert(ini <= fin);
  assert(fin <= length_);
  cds_word count, step;
  count = fin - ini;

  while (count > 0) {
    step = count / 2;
    cds_word pos = ini + step;
    if (!(value < GetField(pos))) {
      ini = pos + 1;
      count -= step + 1;
    } else {
      count = step;
    }
  }
  return ini;
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::UpperBound(cds_word value) const {
  return UpperBound(value, 0, length_);
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::BinarySearch(cds_word value, cds_word ini, cds_word fin) const {
  assert(ini <= fin);
  assert(fin <= length_);
  cds_word pos = LowerBound(value, ini, fin);
  if (pos != fin && !(value < GetField(pos))) {
    return pos;
  } else {
    return fin;
  }
}

template <cds_word bpe> cds_word ArrayTpl<bpe>::BinarySearch(cds_word value) const {
  return BinarySearch(value, 0, length_);
}
{'fraction_non_alphanumeric': 0.13905652358691034, 'fraction_numerical': 0.004079898002549936, 'mean_word_length': 4.089100346020762, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5789534', 'n_tokens_mistral': 4177, 'n_tokens_neox': 3955, 'n_words': 1257}