ArrayFiller.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * ArrayFiller.cpp
  3. *
  4. * Created on: Oct 1, 2016
  5. * Author: Sina M.Baharlou
  6. * The class for filling 1-D boolean array
  7. */
  8. #include "ArrayFiller.hpp"
  9. ArrayFiller::ArrayFiller(bool* bArray,unsigned int arrSize):
  10. arrSize(arrSize),
  11. stepSize(DEFAULT_STEP_SIZE),
  12. currStep(DEFAULT_CURR_STEP),
  13. tRate(0),
  14. pArray(NULL)
  15. {
  16. // -- Initialize boolean array if it has not been initialized
  17. if (bArray==NULL)
  18. this->bArray=new bool[arrSize];
  19. else
  20. this->bArray=bArray;
  21. // -- Initializing sampling distribution --
  22. this->rndReal=std::uniform_real_distribution<float>(0.0,1.0);
  23. // -- Init random engine --
  24. struct timespec ts;
  25. unsigned theTick = 0U;
  26. clock_gettime( CLOCK_REALTIME, &ts );
  27. theTick = ts.tv_nsec / 1000000;
  28. theTick += ts.tv_sec * 1000;
  29. this->rndEngine.seed(theTick);
  30. }
  31. bool* ArrayFiller::getArray()
  32. {
  33. return this->bArray;
  34. }
  35. void ArrayFiller::setPortionArray(float* pArray)
  36. {
  37. this->pArray=pArray;
  38. }
  39. void ArrayFiller::setPortionRate(float rate)
  40. {
  41. this->tRate=rate;
  42. }
  43. bool* ArrayFiller::fillArray(FillMethod method)
  44. {
  45. switch (method)
  46. {
  47. case ZEROS_FILL: // -- fills with zeros (false)
  48. memset(this->bArray,0,this->arrSize*sizeof(bool));
  49. break;
  50. case ONES_FILL: // -- fills with ones (true)
  51. memset(this->bArray,1,this->arrSize*sizeof(bool));
  52. break;
  53. case UNIFORM_FILL: // -- uniformly fills with zeros and ones
  54. for (unsigned int i=0;i<this->arrSize;i++)
  55. {
  56. float rnd=this->rndReal(this->rndEngine);
  57. this->bArray[i]=(rnd<UNIFORM_RATIO);
  58. }
  59. break;
  60. case STEP_FILL: // -- fills with step method
  61. for (unsigned int i=0;i<this->arrSize;i++)
  62. {
  63. float rnd=this->rndReal(this->rndEngine);
  64. this->bArray[i]=(rnd < this->currStep);
  65. }
  66. this->currStep+=this->stepSize; // -- increase the step size ( probability of having true value)
  67. if (this->currStep>1) this->currStep=0;
  68. break;
  69. case PORTION_FILL: // -- fills proportionally
  70. for (unsigned int i=0;i<this->arrSize;i++)
  71. {
  72. float rnd=this->rndReal(this->rndEngine);
  73. this->bArray[i]=(rnd < this->tRate);
  74. }
  75. break;
  76. case PORTION_EL_FILL: // -- fills each element proportionally
  77. if (this->pArray==NULL)
  78. break;
  79. for (unsigned int i=0;i<this->arrSize;i++)
  80. {
  81. float rnd=this->rndReal(this->rndEngine);
  82. this->bArray[i]=(rnd < this->pArray[i]);
  83. }
  84. break;
  85. }
  86. return this->bArray;
  87. }