Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2019
200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Range.h"
  2.  
  3. Range::Range() : rangeFirst(0), rangeLength(0), valueCounts(nullptr) {}
  4.  
  5. void Range::add(T value){
  6.     if(this->empty()){
  7.         resize(value, value);
  8.         add(value);
  9.  
  10.     } else
  11.  
  12.     if((value >= rangeFirst) && (value < (rangeFirst + rangeLength))) {
  13.  
  14.         ++valueCounts[getIndex(value)];
  15.  
  16.     }else
  17.  
  18.     if(value >= (rangeFirst + rangeLength)){
  19.         resize(rangeFirst, value);
  20.         add(value);
  21.     }else
  22.  
  23.     if(value < rangeFirst){
  24.  
  25.         resize(value, rangeFirst + rangeLength - 1);
  26.         add(value);
  27.     }
  28.  
  29. }
  30. size_t Range::getCount(T value) const{
  31.  
  32.     if((getIndex(value) < rangeLength) && (getIndex(value) >= 0)){
  33.  
  34.         return valueCounts[getIndex(value)];
  35.     }else return 0;
  36. }
  37.  
  38. void Range::clear(){
  39.     rangeFirst = 0;
  40.     rangeLength = 0;
  41.     if(valueCounts){
  42.         delete[] valueCounts;
  43.     }
  44. }
  45.  
  46. void Range::resize(T first, T last){
  47.  
  48.     size_t * valuesTemp = new size_t[last - first + 1]();
  49.  
  50.     if(first < rangeFirst){
  51.  
  52.         T offset = rangeFirst - first;
  53.         for(size_t i = 0; i != rangeLength; ++i){
  54.             valuesTemp[i + offset] = valueCounts[i];
  55.         }
  56.     } else
  57.  
  58.     if(last >= rangeFirst + rangeLength){
  59.  
  60.         for(size_t i = 0; i != rangeLength; ++i){
  61.             valuesTemp[i] = valueCounts[i];
  62.         }
  63.     }
  64.  
  65.     clear();
  66.     rangeFirst = first;
  67.     rangeLength = last - first + 1;
  68.     valueCounts = valuesTemp;
  69.  
  70. }
  71.  
  72. bool Range::empty() const{
  73.     return !rangeLength;
  74. }
  75.  
  76. Range::Range(const Range& other) : rangeFirst(other.rangeFirst), rangeLength(other.rangeLength), valueCounts(nullptr) {
  77.  
  78.     if(other.valueCounts){
  79.  
  80.         valueCounts = Range::copyValues(other);
  81.  
  82.     }
  83. }
  84. Range& Range::operator=(const Range& other){
  85.     if(this != &other){
  86.  
  87.         if(other.valueCounts){
  88.             clear();
  89.             rangeFirst = other.rangeFirst;
  90.             rangeLength = other.rangeLength;
  91.             valueCounts = Range::copyValues(other);
  92.         }
  93.  
  94.  
  95.     }
  96.     return *this;
  97. }
  98.  
  99. Range::~Range(){
  100.  
  101.     clear();
  102. }
  103.  
  104. size_t Range::getIndex(T value) const{
  105.     return value - rangeFirst;
  106. }
Advertisement
RAW Paste Data Copied
Advertisement