/* * svec.cpp * This file is part of qps -- Qt-based visual process status monitor * * Copyright 1997-1999 Mattias EngdegÄrd * * 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., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ // implement a stretchy vector class: // An Svec grows automatically (doubles when full), so that adding // elements to the end has an amortized cost of O(1). // For now, only use this for types not requiring a con/destructor. #ifndef SVEC_C #define SVEC_C #include "svec.h" ////#define fatal(str) { printf("fatal error: %s\n",str); exit(0);} #define fatal printf template Svec::Svec(const Svec &s) { int n = s.size(); if (n < 8) n = 8; vect = (T *)malloc(n * sizeof(T)); alloced = n; used = s.size(); for (int i = 0; i < used; i++) vect[i] = s[i]; } template Svec &Svec::operator=(const Svec &s) { if (this != &s) { if (alloced < s.size()) { alloced = s.size(); vect = (T *)realloc(vect, alloced * sizeof(T)); } for (int i = 0; i < s.size(); i++) { vect[i] = s.vect[i]; } used = s.size(); } return *this; } /* template void Svec::indexerr(int index) const { fatal("Svec: index out of range (%d, valid is 0..%d)", index, used - 1); } */ template void Svec::setSize(int newsize) { while (newsize > alloced) grow(); used = newsize; } template void Svec::setextend(int index, T value) { #ifdef CHECK_INDICES if (index < 0) fatal("const Svec: negative index"); #endif while (index >= alloced) grow(); if (index >= used) used = index + 1; vect[index] = value; } template void Svec::insert(int index, T value) { #ifdef CHECK_INDICES if (index < 0 || index > used) fatal("Svec: index out of range"); #endif if ((used + 1) > alloced) grow(); /*int i; T v; v=vect[i+1]; for(i = index; j < =used ; i++) vect[i+1]=v; vect[i+1]=vect[i]; //vect[index+1]=vect[index]; */ // old for (int j = used - 1; j >= index; j--) vect[j + 1] = vect[j]; vect[index] = value; used++; } // for int,float type , no delete template void Svec::remove(int index) { #ifdef CHECK_INDICES if (index < 0 || index >= used) fatal("Svec: index out of range"); #endif for (int j = index; j < used - 1; j++) vect[j] = vect[j + 1]; used--; } // for class type template void Svec::Delete(int index) { #ifdef CHECK_INDICES if (index < 0 || index >= used) fatal("Svec: index out of range"); #endif delete vect[index]; for (int j = index; j < used - 1; j++) vect[j] = vect[j + 1]; used--; } // Assuming T is "pointer to U", delete all contents. // Warning: duplicated objects will be deleted twice --- doubleplusungood template void Svec::purge() { for (int i = 0; i < used; i++) delete vect[i]; used = 0; } extern "C" { typedef int (*compare_func)(const void *, const void *); } template void Svec::sort(int (*compare)(const T *a, const T *b)) { qsort(vect, used, sizeof(T), (compare_func)compare); } #endif // SVEC_C