c++ - Double-Ended Queue Index -


so, have been banging head 3 days trying figure how work.

my assignment write double-ended queue. have no issue part.

the issue have run fact must have bracket operators work when given 1 index.

so, if put in 6, want [2][2] , such. but, have no idea equation work.

i have tried everything, have googled it, have asked people have been in class, has never been done before in class no there. no 1 discusses this, , seems use two-dimensions rather messing 1 index.

i know equation simple, due date friday morning, , still have debug , run unit tests.

here function used in:

template<typename generic> generic& deque<generic>::operator[](unsigned int p) {   return m_data[dq_index]->operator[](block_index); } 

class:

#include<stdexcept> using std::out_of_range; #include "block.h"  template<typename generic> class deque {  public:   deque();   deque(unsigned int n);   deque(deque& d);   ~deque();   void push_front(generic x);   void pop_front();   void push_back(generic x);   void pop_back();   void clear();   deque& operator=(const deque& d);   generic& operator[](unsigned int p);   const generic& operator[](unsigned int p) const;   unsigned int size() const;   unsigned int block_size() const;   bool empty() const;   private:   block<generic>** m_data;   unsigned int m_size;   unsigned int m_blocks;   unsigned int m_block_size; }; 

the assignment: http://web.mst.edu/~buechler/datastructures/dequeclass.htm

the formula you're looking should this:

template<typename generic> generic& deque<generic>::operator[](size_t p) {   size_t dq_index = p / m_block_size;   size_t block_index = p % m_block_size;    return m_data[dq_index]->operator[](block_index);   // return m_data[dq_index][block_index];  should equivalent above } 

commentary: i'm little annoyed pedagogy here. instructor wants code deque using particular implementation technique (block-structured resizable arrays), fine , all, didn't bother explain parts of assignment "implement deque" , "implement block-structured resizable array". come problem that's block-structured part, call deque problem because that's know, confused.

nitpick 1: operator[] takes argument of type size_t. not unsigned int. every instance of unsigned int in block.h , deque.h should size_t, , may tell instructor said so. (you may need add #include <cstddef> above #include <stdexcept> in both files.)

nitpick 2: never put using directives @ top level of header file; stomp on includer's namespace when that. goes both .h interface files , .hpp implementation files, since both of them wind getting #included. can either put using directives @ beginning of each function needs them, or can use qualified names (std::whatever) -- choose whichever less typing, on per-function basis. again, error made instructor, , may tell them said so.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -