Set.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2009 Soeren Sonnenburg
00008  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef _SET_H_
00012 #define _SET_H_
00013 
00014 #include "lib/common.h"
00015 #include "lib/Mathematics.h"
00016 #include "lib/DynamicArray.h"
00017 #include "base/SGObject.h"
00018 
00019 namespace shogun
00020 {
00026 template <class T> class CSet : public CSGObject
00027 {
00028     public:
00030         CSet()
00031         {
00032             array = new CDynamicArray<T>();
00033             SG_REF(array);
00034         }
00035 
00037         ~CSet()
00038         {
00039             SG_UNREF(array);
00040         }
00041 
00046         inline void add(T e)
00047         {
00048             if (!contains(e))
00049                 array->append_element(e);
00050         }
00051 
00056         inline void remove(T e)
00057         {
00058             int32_t idx=array->find_element(e);
00059             if (idx>=0)
00060                 array->delete_element(idx);
00061         }
00062 
00067         inline bool contains(T e)
00068         {
00069             int32_t idx=array->find_element(e);
00070             return (idx!=-1);
00071         }
00072 
00077         inline int32_t get_num_elements() const
00078         {
00079             return array->get_num_elements();
00080         }
00081 
00089         inline T get_element(int32_t index) const
00090         {
00091             return array->get_element(index);
00092         }
00093 
00102         inline T operator[](int32_t index) const
00103         {
00104             return array->get_element(index);
00105         }
00106 
00108         inline virtual const char* get_name() const { return "Set"; }
00109 
00110     protected:
00112         CDynamicArray<T>* array;
00113 };
00114 }
00115 #endif //_SET_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation