Is the following possible without storing pointers?

This is a discussion on Is the following possible without storing pointers? within the C++ Programming forums, part of the General Programming Boards category; <Not that I have something against pointers, but they'd make the class awkward to use> The problem is about a ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Is the following possible without storing pointers?

    <Not that I have something against pointers, but they'd make the class awkward to use>

    The problem is about a matrix class.
    I'm trying to make a trade-off...which I think is justified.

    The idea is to store both the matrix and its transpose as member vector<vector<T>> `s. (Not exactly T...a pointer..reference..or something else)
    That way... transpose would just be a std::swap and grabbing or interchanging rows and/or columns would just be accessing or swapping a vector<>.

    Forgetting about templates for now, consider I have an integer matrix;
    How exactly should I design it, such that, when an element from either of the representations get modified, the other one gets changed to, automatically ?
    (This felt like a simple thing to do at first.....and here I am...after some blunders!)
    Last edited by manasij7479; 11-15-2011 at 05:43 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Well since you can only efficiently swap either rows or columns, you need to make a choice.

    Or you could do something like this.
    Code:
    vector<vector<T>> matrix;
    vector<vector<pair<int,int> > > transpose;
    If matrix[row][col] is an element,
    it's transposition is at matrix[transpose[row][col].first][transpose[row][col].second]

    By suitably initialising all the pairs, you can achieve any arbitrary matrix transformation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    I feel that you're trying to go about it the wrong way. You don't have to store it both ways around, and I don't see why you'd want to store vectors internally. All you'd need to do is to make your interface capable of allowing the accessing of the members to be in row or column major order.
    I.e. try out the following code which for this example just stores ints and the matrix is 4x4 in size.
    Code:
    class constProxyAccessor {
    	const int *base, step;
    public:
    	constProxyAccessor(const int *base, int step)
    		: base(base), step(step) {}
    
    	int operator[](int x) const {
    		return base[x * step];
    	}
    };
    
    class proxyAccessor {
    	int *base, step;
    public:
    	proxyAccessor(int *base, int step)
    		: base(base), step(step) {}
    
    	int& operator[](int x) {
    		return base[x * step];
    	}
    };
    
    class transposableMatrix {
    	int m[16];
    	bool inverted;
    public:
    	transposableMatrix() : inverted(false) {}
    	void invert() {
    		inverted = !inverted;
    	}
    	constProxyAccessor operator[](int x) const {
    		return inverted ? constProxyAccessor(&m[x*4], 1) : constProxyAccessor(&m[x], 4);
    	}
    	proxyAccessor operator[](int x) {
    		return inverted ? proxyAccessor(&m[x*4], 1) : proxyAccessor(&m[x], 4);
    	}
    };
    
    int main()
    {
    	transposableMatrix mtx;
    	mtx[3][1] = 123;
    	mtx.invert();
    	int foo = mtx[1][3]; // foo is now 123!
    	mtx[2][0] = 456;
    	mtx.invert();
    	foo = mtx[0][2]; // foo is now 456!
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing function pointers
    By lautarox in forum C++ Programming
    Replies: 11
    Last Post: 09-22-2009, 05:58 PM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. storing pointers to a templated class in a vector
    By rt454 in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2009, 02:04 AM
  4. Storing # from .dat file in Matrix using Pointers
    By dakarn in forum C Programming
    Replies: 5
    Last Post: 11-29-2008, 05:46 PM
  5. storing dynamically allocated pointers
    By breaka in forum C Programming
    Replies: 19
    Last Post: 07-22-2006, 06:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21