Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/general/spbita2d.cpp
3206 views
1
/**************************************************************************/
2
/* File: spbita2d.cpp */
3
/* Author: Joachim Schoeberl */
4
/* Date: 01. Jun. 95 */
5
/**************************************************************************/
6
7
/*
8
Implementation of sparse 2 dimensional bitarray
9
*/
10
11
12
#include <mystdlib.h>
13
#include <myadt.hpp>
14
15
namespace netgen
16
{
17
//using namespace netgen;
18
19
SPARSE_BIT_ARRAY_2D :: SPARSE_BIT_ARRAY_2D (int ah, int aw)
20
{
21
lines = NULL;
22
SetSize (ah, aw);
23
}
24
25
SPARSE_BIT_ARRAY_2D :: ~SPARSE_BIT_ARRAY_2D ()
26
{
27
DeleteElements ();
28
delete lines;
29
}
30
31
32
void SPARSE_BIT_ARRAY_2D :: SetSize (int ah, int aw)
33
{
34
DeleteElements();
35
if (lines)
36
{
37
delete lines;
38
lines = NULL;
39
}
40
41
if (!aw) aw = ah;
42
43
height = ah;
44
width = aw;
45
46
if (!ah) return;
47
lines = new linestruct[ah];
48
49
if (lines)
50
{
51
for (int i = 0; i < ah; i++)
52
{
53
lines[i].size = 0;
54
lines[i].maxsize = 0;
55
lines[i].col = NULL;
56
}
57
}
58
else
59
{
60
height = width = 0;
61
MyError ("SPARSE_ARRAY::SetSize: Out of memory");
62
}
63
}
64
65
66
67
void SPARSE_BIT_ARRAY_2D :: DeleteElements ()
68
{
69
if (lines)
70
{
71
for (int i = 0; i < height; i++)
72
{
73
if (lines[i].col)
74
{
75
delete [] lines[i].col;
76
lines[i].col = NULL;
77
lines[i].size = 0;
78
lines[i].maxsize = 0;
79
}
80
}
81
}
82
}
83
84
85
int SPARSE_BIT_ARRAY_2D :: Test (int i, int j) const
86
{
87
int k, max, *col;
88
89
if (!lines) return 0;
90
if (i < 1 || i > height) return 0;
91
92
col = lines[i-1].col;
93
max = lines[i-1].size;
94
95
for (k = 0; k < max; k++, col++)
96
if (*col == j) return 1;
97
98
return 0;
99
}
100
101
102
103
void SPARSE_BIT_ARRAY_2D :: Set(int i, int j)
104
{
105
int k, max, *col;
106
107
i--;
108
col = lines[i].col;
109
max = lines[i].size;
110
111
for (k = 0; k < max; k++, col++)
112
if (*col == j)
113
return;
114
115
if (lines[i].size)
116
{
117
if (lines[i].size == lines[i].maxsize)
118
{
119
col = new int[lines[i].maxsize+2];
120
if (col)
121
{
122
lines[i].maxsize += 2;
123
memcpy (col, lines[i].col, sizeof (int) * lines[i].size);
124
delete [] lines[i].col;
125
lines[i].col = col;
126
}
127
else
128
{
129
MyError ("SPARSE_BIT_ARRAY::Set: Out of mem 1");
130
return;
131
}
132
}
133
else
134
col = lines[i].col;
135
136
if (col)
137
{
138
k = lines[i].size-1;
139
while (k >= 0 && col[k] > j)
140
{
141
col[k+1] = col[k];
142
k--;
143
}
144
145
k++;
146
lines[i].size++;
147
col[k] = j;
148
return;
149
}
150
else
151
{
152
MyError ("SPARSE_ARRAY::Set: Out of memory 2");
153
}
154
}
155
else
156
{
157
lines[i].col = new int[4];
158
if (lines[i].col)
159
{
160
lines[i].maxsize = 4;
161
lines[i].size = 1;
162
lines[i].col[0] = j;
163
return;
164
}
165
else
166
{
167
MyError ("SparseMatrix::Elem: Out of memory 3");
168
}
169
}
170
}
171
172
}
173
174