Mac binaries
[jabaws.git] / website / archive / binaries / mac / src / clustalw / src / general / Array2D.h
1 /**
2  * Author: Mark Larkin
3  * 
4  * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.  
5  */
6 #ifndef ARRAY2D_H
7 #define ARRAY2D_H
8
9 #include<iostream>
10 #include <vector>
11 namespace clustalw
12 {
13
14 using namespace std;
15
16 template <class T>
17 class Array2D
18 {
19 public:
20    Array2D():m_dimRow(0), m_dimCol(0){;}
21    Array2D(int nRow, int nCol) 
22    {
23        m_dimRow = nRow;
24        m_dimCol = nCol;
25        for (int i=0; i < nRow; i++)
26        {
27            vector<T> x(nCol);
28            int y = x.size();
29            m_2DVector.push_back(x);
30        }
31    }
32    
33    int getRowSize()
34    {
35        return m_dimRow;
36    }
37    
38    int getColSize()
39    {
40        return m_dimCol;
41    }
42    
43    void SetAt(int nRow, int nCol, const T& value)
44    {
45        m_2DVector[nRow][nCol] = value;
46    }
47    T GetAt(int nRow, int nCol) 
48    {
49        return m_2DVector[nRow][nCol];
50    }
51    void GrowRow(int newSize) 
52    {
53        if (newSize <= (int)m_dimRow)
54            return;
55        m_dimRow = newSize;
56        for(int i = 0 ; i < newSize - (int)m_dimCol; i++)   
57        {
58            vector<T> x(m_dimRow);
59            m_2DVector.push_back(x);
60        }
61    }
62    void GrowCol(int newSize) 
63    {
64        if(newSize <= (int)m_dimCol)
65            return;
66        m_dimCol = newSize;
67        for (int i=0; i < (int)m_dimRow; i++)
68            m_2DVector[i].resize(newSize);
69    }
70    
71    void ResizeRect(int row, int col)
72    {
73        GrowRow(row);
74        GrowCol(col);
75    }
76
77    /* This is to get everything initialised to a value */
78    void GrowRow(int newSize, const T& value) 
79    {
80        if (newSize <= m_dimRow)
81            return;
82        m_dimRow = newSize;
83        for(int i = 0 ; i < newSize - m_dimCol; i++)   
84        {
85            vector<T> x(m_dimRow, value);
86            m_2DVector.push_back(x);
87        }
88    }
89    void GrowCol(int newSize, const T& value) 
90    {
91        if(newSize <= m_dimCol)
92            return;
93        m_dimCol = newSize;
94        for (int i=0; i <m_dimRow; i++)
95            m_2DVector[i].resize(newSize, value);
96    }
97    
98    void ResizeRect(int row, int col, const T& value)
99    {
100        GrowRow(row, value);
101        GrowCol(col, value);
102    }   
103       
104    void printArray()
105    {
106        for(int row=0; row < m_dimRow; row++)
107        {
108            for(int col=0; col < m_dimCol; col++)
109            {
110                cout <<" "<< m_2DVector[row][col];  
111            }
112            cout<<"\n";
113        }
114    }
115    
116    void clearArray()
117    {
118        int size = m_2DVector.size();
119        for(int i = 0; i < size; i++)
120        {
121            m_2DVector[i].clear();
122        }
123        m_2DVector.clear();
124        m_dimRow = 0;
125        m_dimCol = 0;
126        
127    }
128    
129    vector<T>& operator[](int x)    
130    {
131        return m_2DVector[x];
132    }
133 private:
134    vector< vector <T> > m_2DVector;
135    unsigned int m_dimRow;
136    unsigned int m_dimCol;
137 };
138
139 }
140 #endif
141