Recovered from microbel
This commit is contained in:
113
common/lib/utils/intarray.C
Normal file
113
common/lib/utils/intarray.C
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* PVVMUD a 3D MUD
|
||||
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
#include "pvvmud.H"
|
||||
#include <stdlib.h>
|
||||
#include "intarray.H"
|
||||
#include "exception.H"
|
||||
|
||||
CIntArray::CIntArray(){
|
||||
m_size = 0;
|
||||
m_num = 0;
|
||||
m_array = NULL;
|
||||
}
|
||||
|
||||
CIntArray::CIntArray(int size){
|
||||
if (size > 0){
|
||||
m_size = size;
|
||||
m_num = 0;
|
||||
m_array = (int*)malloc(m_size*sizeof(int));
|
||||
} else {
|
||||
m_size = 0;
|
||||
m_num = 0;
|
||||
m_array = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
CIntArray::CIntArray(const CIntArray & array){
|
||||
int ii;
|
||||
m_size = array.m_num;
|
||||
m_num = array.m_num;
|
||||
m_array = (int*)malloc(m_size*sizeof(int));
|
||||
for (ii = 0; ii < m_num; ii++) m_array[ii] = array.m_array[ii];
|
||||
}
|
||||
|
||||
CIntArray::~CIntArray() {
|
||||
if (m_array != NULL) free(m_array);
|
||||
}
|
||||
|
||||
int CIntArray::add(int num){
|
||||
if (m_num >= m_size || m_array == NULL) {
|
||||
if (m_size <= 0) m_size = 16;
|
||||
else m_size = m_size*2;
|
||||
m_array = (int*)realloc(m_array,m_size*sizeof(int));
|
||||
}
|
||||
if (m_array != NULL) m_array[m_num] = num;
|
||||
return m_num++;
|
||||
}
|
||||
|
||||
int CIntArray::set(int index, int num){
|
||||
if ((index < m_size) && (index >= 0)){
|
||||
m_array[index] = num;
|
||||
if (m_num <= index) m_num = index+1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int CIntArray::get( int index ) const{
|
||||
if (index < 0 || index >= m_num || m_array == NULL)
|
||||
throw new CException("OutOfBound exception");
|
||||
return m_array[index];
|
||||
}
|
||||
|
||||
int CIntArray::getNumElements() const{
|
||||
return m_num;
|
||||
}
|
||||
|
||||
int CIntArray::find(int value) const{
|
||||
int index,num = getNumElements();
|
||||
for (index = 0; index < num; index++){
|
||||
if (get(index) == value) return index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
CIntArray CIntArray::substracSet( const CIntArray & array ) const {
|
||||
CIntArray result;
|
||||
int index,num = getNumElements();
|
||||
for (index = 0; index < num; index++){
|
||||
int value = get(index);
|
||||
if (array.find(value) == -1) result.add(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
CIntArray CIntArray::unionSet( const CIntArray & array ) const {
|
||||
CIntArray result;
|
||||
int index,num = getNumElements();
|
||||
for (index = 0; index < num; index++){
|
||||
int value = get(index);
|
||||
if (array.find(value) != -1) result.add(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user