container_tpl.h 4.15 KB
/**
 * \file container_tpl.h
 * 
 * \brief ein Template das als Behälter für alles mögliche dient.
 *
 * \author Georg Steffers <georg@steffers.org> [gs]
 *
 * \date 16.12.2003
 *
 * \version ..2002 [gs]: erste Implementation
 * \version 16.12.2003 [gs]: <ul><li>
 *                                       Beginn der Dokumentaion mit Doxygen
 *                                       </li><li>
 *                                       Einige &Auml;nderungen, so das
 *                                       content in einem leeren container
 *                                       NULL ist und dann entsprechen auch
 *                                       nicht alle Aktionen mit diesem
 *                                       container m&ouml;glich sind.
 *                                       </li></ul>
 */

/* 
 * Copyright (C)2003 Georg Steffers
 *
 * 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
 */

using namespace std;

#include <cstring>
#include "container.h"

template <class C>
container<C>::container() : content(NULL), count(0) {
    //content=new C[1];
}

template <class C>
container<C>::container(const C& c) : count(1) {
    content=new C[1];
    content[0]=c;
}

template <class C>
container<C>::container(const C* c, unsigned co) : count(co) {
   if(count > 0)
       content=new C[count];
   else
      content=NULL;

    for(unsigned i=0; i<count; i++)
        content[i]=c[i];
}

template <class C>
container<C>::container(const container<C>& c) : count(c.count) {
    if(count > 0)
       content=new C[count];
    else
       content=NULL;
            
    for(unsigned i=0; i<count; i++)
        content[i]=c.content[i];
}

template <class C>
container<C>::~container() {
    if(content) {
        delete [] content;
        content=NULL;
    }
     count=0;
}

template <class C>
const container<C>& container<C>::operator=(const container<C>& c) {
    if(this==&c)
        return *this;

    if(count != c.count) {
        delete [] content;
        if(c.count > 0)
           content=new C[c.count];
        else
           content=NULL;
    }

    count=c.count;

    for(unsigned i=0; i<count; i++)
        content[i]=c.content[i];

    return *this;
}

template <class C>
const C& container<C>::operator[](unsigned index) const {
    if(index+1 > count)
        return content[0];

    return content[index];
}

template <class C>
C& container<C>::operator[](unsigned index) {
    if(index+1 > count) {
        C* content_tmp=new C[index+1];

        for(unsigned i=0; i<count; i++)
            content_tmp[i]=content[i];
//        memcpy(content_tmp, content, count*sizeof(C));

        delete [] content;
        content=content_tmp;
        count=index+1;
    }

    return content[index];
}

// WARNUNG!!! Diese funktion verkleiner auch ohne weitern check. d.h. ist
// size < count, dann verliert man alle Einträge >size.
template <class C>
void container<C>::resize(unsigned size) {
    C* content_tmp;

    if(size>0) {
       content_tmp=new C[size];

       for(unsigned i=0; i<size<count?size:count; i++)
           content_tmp[i]=content[i];
    }
    else
       content_tmp=NULL;

    delete [] content;
    content=content_tmp;
    count=size;
}

template <class C>
void container<C>::insert(unsigned idx, const C& value) {
   C* content_tmp=new C[count+1];

   memcpy(content_tmp, content, idx*sizeof(C));
   content_tmp[idx]=value;

   for(unsigned i=0; i<count; i++)
      content_tmp[i]=content[i];
//   memcpy(&content_tmp[idx+1], &content[idx], (count-idx)*sizeof(C));

   delete [] content;
   content=content_tmp;
   count++;
}