What is the Matrix? - Part 3, Streaming

Environment: Visual Studio, All Windows platforms

The Matrix, in its simplest form, simplifies working with arrays. This is the third of several articles that explains how to use the different features of the Matrix. In the previous article, I described how to use the Matrix to simplify COM marshalling with VARIANTs and SAFEARRAYS. In this article, I will explain how to use stream opeators to insert data into a Matrix.

The Matrix can be downloaded from CodeGuru, along with a brief overview of what it does. The first article, "Basic Features," can be dowloaded from here. The second article, "Working with COM," can be dowloaded from here.

Streaming

The following code shows how to create a basic two-dimensional array

CMatrix<int> matrix ;
matrix.cell(0,0)=100 ;
matrix.cell(1,0)=200;
matrix.cell(0,1)=300;
matrix.cell(1,1)=400;

This code looks a little verbose. I'm sure you will agree that the following code is a much preferable way of inserting data...

CMatrix<int> matrix ;
matrix << move ( 0,0 ) << 100
       << move (1,0 ) << 200
       << move (0,1 ) << 300
       << move (1,1 ) << 400 ;

Here, the 'move' is an internal stream operator. It simply moves the internal cursor of the matrix so that the next item streamed in will be placed at this location. The 'move (0,0)' moves the cursor to 0,0 in the Matrix and the value '100' is inserted in the cell.

Similar to 'move' is 'offset'. Here is an example of its use...

CMatrix<int> matrix ;
matrix << move ( 5,5 ) << 100
       << move (1,0 )  << 200
       << move (0,1 )  << 300
       << move (1,1 )  << 400 ;

matrix << move(5,5 )    << "cell: 5,5"
       << offset(-1,-1) << "cell: 4,4"
       << offset(2,2)   << "cell: 6,6" ;

Offset simply offsets the internal cursor by the amount specific (in x,y format).

Bookmarks

Bookmarks allow you to set an anchor within the Matrix. All operations performed after setting a bookmark are relative to that bookmark. A bookmark's properties are its name and its X & Y co-ordinates. Imagine you had a 10 by 10 Matrix and wanted the centre of the Matrix to be the point from which all references are made...

matrix.addMarker(L"middle", 5,5 ) ;
matrix << bookmark(L"middle") << "middle"
<< move (1,1) << "cell: 6,6" ;

The move to position 1x and 1y in the above code will actually move to position 6x and 6y. All operations are relative to the last bookmark.

One important thing to remember when using bookmarks is to clear the bookmark when you've finished with it. If you were to share your Matrix or pass it to a function to read data, the read operations would be performed relative to the last bookmark. To clear the bookmark, simply stream in an empty bookmar like so...

matrix << bookmark() ;

Extensibility

It is possible to extend the basic stream operators found in the Matrix. For example, you could 'plug-in' an operator that performs a calculation on each number you stream in, for example...

matrix << mulitplier(2) << 4 ;    // would store 8
matrix << multiplier(5) << 2 ;    // would store 10

An article describing how to extend the operators and the Matrix itself could be the topic of a future article. I'll base this decision on the feedback and comments that are left.

Conclusion

Hopefully, this article has shown you how to use stream operators with the Matrix class.

Downloads

Download demo project - 81 Kb
Download source - 13 Kb


Comments

  • Nice, bur how about [ ][ ]

    Posted by Legacy on 04/28/2003 12:00am

    Originally posted by: Per N.

    Cool, thing...and here's a tip for ya:

    Let the CMatrix have a [] operator returning a TempClass that itself also has a [] operator returning a reference of the element type. Then you could do stuff like

    myMatrix[3][4] = 7;
    and
    int foo = myMatrix[2][7];

    even though there is no [][] operator defined in the C++ standard....

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds