Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's another way. Of course this is just for int but the code could easily be altered for other datatypes.</p> <p><strong>AOMatrix.h:</strong></p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface AOMatrix : NSObject { @private int* matrix_; uint columnCount_; uint rowCount_; } - (id)initWithRows:(uint)rowCount Columns:(uint)columnCount; - (uint)rowCount; - (uint)columnCount; - (int)valueAtRow:(uint)rowIndex Column:(uint)columnIndex; - (void)setValue:(int)value atRow:(uint)rowIndex Column:(uint)columnIndex; @end </code></pre> <p><strong>AOMatrix.m</strong></p> <pre><code>#import "AOMatrix.h" #define INITIAL_MATRIX_VALUE 0 #define DEFAULT_ROW_COUNT 4 #define DEFAULT_COLUMN_COUNT 4 /**************************************************************************** * BIG NOTE: * Access values in the matrix_ by matrix_[rowIndex*columnCount+columnIndex] ****************************************************************************/ @implementation AOMatrix - (id)init { return [self initWithRows:DEFAULT_ROW_COUNT Columns:DEFAULT_COLUMN_COUNT]; } - (id)initWithRows:(uint)initRowCount Columns:(uint)initColumnCount { self = [super init]; if(self) { rowCount_ = initRowCount; columnCount_ = initColumnCount; matrix_ = malloc(sizeof(int)*rowCount_*columnCount_); uint i; for(i = 0; i &lt; rowCount_*columnCount_; ++i) { matrix_[i] = INITIAL_MATRIX_VALUE; } // NSLog(@"matrix_ is %ux%u", rowCount_, columnCount_); // NSLog(@"matrix_[0] is at %p", &amp;(matrix_[0])); // NSLog(@"matrix_[%u] is at %p", i-1, &amp;(matrix_[i-1])); } return self; } - (void)dealloc { free(matrix_); [super dealloc]; } - (uint)rowCount { return rowCount_; } - (uint)columnCount { return columnCount_; } - (int)valueAtRow:(uint)rowIndex Column:(uint)columnIndex { // NSLog(@"matrix_[%u](%u,%u) is at %p with value %d", rowIndex*columnCount_+columnIndex, rowIndex, columnIndex, &amp;(matrix_[rowIndex*columnCount_+columnIndex]), matrix_[rowIndex*columnCount+columnIndex]); return matrix_[rowIndex*columnCount_+columnIndex]; } - (void)setValue:(int)value atRow:(uint)rowIndex Column:(uint)columnIndex { matrix_[rowIndex*columnCount_+columnIndex] = value; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload