dimanche 1 mai 2016

Creating A Random Iterator (Permutation)

Last big hurdle for this project, and I hope you you guys can help me out because I am terribly stuck again. What I'm working on is a dynamically allocated templated container, with all the code written from scratch with a constant iterator, and a random iterator that generates a permutation in the form of an array of indices. Thus far I've gotten it to generate the indices properly, however I am unsure as to how to get the random iterator to move through them using that array of indices. I think it's a problem with my ++ operator for the random iterator, but I'm totally unsure of what to do make it iterate properly. Here are some snippets:

// Implementation of the rand_iterator method when provided a seed by the user.
// rand_iterator takes a pointer to a container object, and the seed as parameters.
template <class T>
typename sorted<T>::rand_iterator sorted<T>::rndbegin(unsigned seed){
  return rand_iterator(this, seed);
}

// Implementation of the const iterator pre-incrementer.
template <class T>
typename sorted<T>::const_iterator sorted<T>::const_iterator::operator++(){ ++m_current; return *this; }

// Implementation of the const iterator post-incrementer.
template <class T>
typename sorted<T>::const_iterator sorted<T>::const_iterator::operator++(int){ const_iterator result(*this); ++(*this); return result; }

// snip which creates the permutation (functional)

  srand(seed);
  m_random = new int[srtdPtr->m_size];
  srtdPtr->m_crandom = new int[srtdPtr->m_size];

  for (int i = 0; i < srtdPtr->m_size; i++)
    m_random[i] = i;
  for (int i = 0; i < srtdPtr->m_size; i++){

    T temp;

    j = rand() % (srtdPtr->m_size);
    temp = m_random[i];
    srtdPtr->m_crandom[i] = srtdPtr->m_crandom[j];
    srtdPtr->m_crandom[j] = temp;
    m_random[i] = m_random[j];
    m_random[j] = temp;
  }




Aucun commentaire:

Enregistrer un commentaire