Path: blob/devel/ElmerGUI/netgen/libsrc/general/spbita2d.cpp
3206 views
/**************************************************************************/1/* File: spbita2d.cpp */2/* Author: Joachim Schoeberl */3/* Date: 01. Jun. 95 */4/**************************************************************************/56/*7Implementation of sparse 2 dimensional bitarray8*/91011#include <mystdlib.h>12#include <myadt.hpp>1314namespace netgen15{16//using namespace netgen;1718SPARSE_BIT_ARRAY_2D :: SPARSE_BIT_ARRAY_2D (int ah, int aw)19{20lines = NULL;21SetSize (ah, aw);22}2324SPARSE_BIT_ARRAY_2D :: ~SPARSE_BIT_ARRAY_2D ()25{26DeleteElements ();27delete lines;28}293031void SPARSE_BIT_ARRAY_2D :: SetSize (int ah, int aw)32{33DeleteElements();34if (lines)35{36delete lines;37lines = NULL;38}3940if (!aw) aw = ah;4142height = ah;43width = aw;4445if (!ah) return;46lines = new linestruct[ah];4748if (lines)49{50for (int i = 0; i < ah; i++)51{52lines[i].size = 0;53lines[i].maxsize = 0;54lines[i].col = NULL;55}56}57else58{59height = width = 0;60MyError ("SPARSE_ARRAY::SetSize: Out of memory");61}62}63646566void SPARSE_BIT_ARRAY_2D :: DeleteElements ()67{68if (lines)69{70for (int i = 0; i < height; i++)71{72if (lines[i].col)73{74delete [] lines[i].col;75lines[i].col = NULL;76lines[i].size = 0;77lines[i].maxsize = 0;78}79}80}81}828384int SPARSE_BIT_ARRAY_2D :: Test (int i, int j) const85{86int k, max, *col;8788if (!lines) return 0;89if (i < 1 || i > height) return 0;9091col = lines[i-1].col;92max = lines[i-1].size;9394for (k = 0; k < max; k++, col++)95if (*col == j) return 1;9697return 0;98}99100101102void SPARSE_BIT_ARRAY_2D :: Set(int i, int j)103{104int k, max, *col;105106i--;107col = lines[i].col;108max = lines[i].size;109110for (k = 0; k < max; k++, col++)111if (*col == j)112return;113114if (lines[i].size)115{116if (lines[i].size == lines[i].maxsize)117{118col = new int[lines[i].maxsize+2];119if (col)120{121lines[i].maxsize += 2;122memcpy (col, lines[i].col, sizeof (int) * lines[i].size);123delete [] lines[i].col;124lines[i].col = col;125}126else127{128MyError ("SPARSE_BIT_ARRAY::Set: Out of mem 1");129return;130}131}132else133col = lines[i].col;134135if (col)136{137k = lines[i].size-1;138while (k >= 0 && col[k] > j)139{140col[k+1] = col[k];141k--;142}143144k++;145lines[i].size++;146col[k] = j;147return;148}149else150{151MyError ("SPARSE_ARRAY::Set: Out of memory 2");152}153}154else155{156lines[i].col = new int[4];157if (lines[i].col)158{159lines[i].maxsize = 4;160lines[i].size = 1;161lines[i].col[0] = j;162return;163}164else165{166MyError ("SparseMatrix::Elem: Out of memory 3");167}168}169}170171}172173174