File: spbita2d.cpp

package info (click to toggle)
netgen 6.2.2601%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,076 kB
  • sloc: cpp: 166,627; tcl: 6,310; python: 2,868; sh: 528; makefile: 90
file content (172 lines) | stat: -rw-r--r-- 2,937 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**************************************************************************/
/* File:   spbita2d.cpp                                                   */
/* Author: Joachim Schoeberl                                              */
/* Date:   01. Jun. 95                                                    */
/**************************************************************************/

/* 
   Implementation of sparse 2 dimensional bitarray
*/


#include <mystdlib.h>
#include <myadt.hpp>

namespace netgen
{
  //using namespace netgen;

  SPARSE_BIT_Array_2D :: SPARSE_BIT_Array_2D (int ah, int aw)
  {
    lines = NULL;
    SetSize (ah, aw);
  }

  SPARSE_BIT_Array_2D :: ~SPARSE_BIT_Array_2D ()
  {
    DeleteElements ();
    delete lines;
  }


  void SPARSE_BIT_Array_2D :: SetSize (int ah, int aw)
  {
    DeleteElements();
    if (lines)
      {
	delete lines;
	lines = NULL;
      }

    if (!aw) aw = ah;

    height = ah;
    width = aw;

    if (!ah) return;
    lines = new linestruct[ah];

    if (lines)
      {
	for (int i = 0; i < ah; i++)
	  {
	    lines[i].size = 0;
	    lines[i].maxsize = 0;
	    lines[i].col = NULL;
	  }
      }
    else
      {
	height = width = 0;
	MyError ("SPARSE_Array::SetSize: Out of memory");
      }
  }



  void SPARSE_BIT_Array_2D :: DeleteElements ()
  {
    if (lines)
      {
	for (int i = 0; i < height; i++)
	  {
	    if (lines[i].col)
	      {
		delete [] lines[i].col;
		lines[i].col = NULL;
		lines[i].size = 0;
		lines[i].maxsize = 0;
	      }
	  }
      }
  }


  int SPARSE_BIT_Array_2D :: Test (int i, int j) const
  {
    int k, max, *col;

    if (!lines) return 0;
    if (i < 1 || i > height) return 0;

    col = lines[i-1].col;
    max = lines[i-1].size;

    for (k = 0; k < max; k++, col++)
      if (*col == j) return 1;

    return 0;
  }



  void SPARSE_BIT_Array_2D :: Set(int i, int j)
  {
    int k, max, *col;

    i--;
    col = lines[i].col;
    max = lines[i].size;

    for (k = 0; k < max; k++, col++)
      if (*col == j)
	return;

    if (lines[i].size)
      {
	if (lines[i].size == lines[i].maxsize)
	  {
	    col = new int[lines[i].maxsize+2];
	    if (col)
	      {
		lines[i].maxsize += 2;
		memcpy (col, lines[i].col, sizeof (int) * lines[i].size);
		delete [] lines[i].col;
		lines[i].col = col;
	      }
	    else
	      {
		MyError ("SPARSE_BIT_Array::Set: Out of mem 1");
		return;
	      }
	  }
	else
	  col = lines[i].col;

	if (col)
	  {
	    k = lines[i].size-1;
	    while (k >= 0 && col[k] > j)
	      {
		col[k+1] = col[k];
		k--;
	      }

	    k++;
	    lines[i].size++;
	    col[k] = j;
	    return;
	  }
	else
	  {
	    MyError ("SPARSE_Array::Set: Out of memory 2");
	  }
      }
    else
      {
	lines[i].col = new int[4];
	if (lines[i].col)
	  {
	    lines[i].maxsize = 4;
	    lines[i].size = 1;
	    lines[i].col[0] = j;
	    return;
	  }
	else
	  {
	    MyError ("SparseMatrix::Elem: Out of memory 3");
	  }
      }
  }

}