1 /* -*-C-*- 2 ******************************************************************************** 3 * 4 * File: matrix.h (Formerly matrix.h) 5 * Description: Ratings matrix code. (Used by associator) 6 * Author: Mark Seaman, OCR Technology 7 * Created: Wed May 16 13:22:06 1990 8 * Modified: Tue Mar 19 16:00:20 1991 (Mark Seaman) marks@hpgrlt 9 * Language: C 10 * Package: N/A 11 * Status: Experimental (Do Not Distribute) 12 * 13 * (c) Copyright 1990, Hewlett-Packard Company. 14 ** Licensed under the Apache License, Version 2.0 (the "License"); 15 ** you may not use this file except in compliance with the License. 16 ** You may obtain a copy of the License at 17 ** http://www.apache.org/licenses/LICENSE-2.0 18 ** Unless required by applicable law or agreed to in writing, software 19 ** distributed under the License is distributed on an "AS IS" BASIS, 20 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 ** See the License for the specific language governing permissions and 22 ** limitations under the License. 23 * 24 *********************************************************************************/ 25 #ifndef MATRIX_H 26 #define MATRIX_H 27 28 #include "ratngs.h" 29 #include "unicharset.h" 30 31 static const int NOT_CLASSIFIED = 0; 32 33 // A generic class to store a matrix with entries of type T. 34 template <class T> 35 class GENERIC_MATRIX { 36 public: 37 // Allocate a piece of memory to hold a matrix of the given dimension. 38 // Initialize all the elements of the matrix to 0/NULL. GENERIC_MATRIX(int dimension)39 GENERIC_MATRIX(int dimension) { 40 matrix_ = new T[dimension * dimension]; 41 dimension_ = dimension; 42 for (int x = 0; x < dimension; x++) 43 for (int y = 0; y < dimension; y++) 44 this->put(x, y, NOT_CLASSIFIED); 45 } ~GENERIC_MATRIX()46 ~GENERIC_MATRIX() { delete[] matrix_; } 47 48 // Provide the dimension of this square matrix. dimension()49 long dimension() const { return dimension_; } 50 51 // Expression to select a specific location in the matrix. index(int column,int row)52 int index(int column, int row) const { 53 return (row * this->dimension() + column); 54 } 55 56 // Put a list element into the matrix at a specific location. put(int column,int row,T thing)57 void put(int column, int row, T thing) { 58 matrix_[this->index(column, row)] = thing; 59 } 60 61 // Get the item at a specified location from the matrix. get(int column,int row)62 T get(int column, int row) const { 63 return matrix_[this->index(column, row)]; 64 } 65 66 // Delete objects pointed to by matrix_[i]. delete_matrix_pointers()67 void delete_matrix_pointers() { 68 for (int x = 0; x < this->dimension(); x++) { 69 for (int y = 0; y < this->dimension(); y++) { 70 T matrix_cell = this->get(x, y); 71 if (matrix_cell != NOT_CLASSIFIED) 72 delete matrix_cell; 73 } 74 } 75 } 76 77 private: 78 T *matrix_; 79 int dimension_; 80 }; 81 82 class MATRIX : public GENERIC_MATRIX<BLOB_CHOICE_LIST *> { 83 public: MATRIX(int dimension)84 MATRIX(int dimension) : GENERIC_MATRIX<BLOB_CHOICE_LIST *>(dimension) {} 85 // Print a shortened version of the contents of the matrix. 86 void print(const UNICHARSET ¤t_unicharset); 87 }; 88 89 #endif 90