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 <shogun/lib/common.h>
00015 #include <shogun/mathematics/Math.h>
00016 #include <shogun/base/DynArray.h>
00017 #include <shogun/base/SGObject.h>
00018 
00019 namespace shogun
00020 {
00026 template <class T> class CSet : public CSGObject
00027 {
00028     public:
00030         CSet(bool traceable=true)
00031         {
00032             array = new DynArray<T>(1024, traceable);
00033         }
00034 
00036         ~CSet()
00037         {
00038             delete array;
00039         }
00040 
00045         inline void add(T e)
00046         {
00047             if (!contains(e))
00048                 array->append_element(e);
00049         }
00050 
00055         inline void remove(T e)
00056         {
00057             int32_t idx=array->find_element(e);
00058             if (idx>=0)
00059                 array->delete_element(idx);
00060         }
00061 
00066         inline bool contains(T e)
00067         {
00068             int32_t idx=array->find_element(e);
00069             return (idx!=-1);
00070         }
00071 
00077         inline int32_t index_of(T e)
00078         {
00079             return array->find_element(e);
00080         }
00081 
00086         inline int32_t get_num_elements() const
00087         {
00088             return array->get_num_elements();
00089         }
00090 
00098         inline T get_element(int32_t index) const
00099         {
00100             return array->get_element(index);
00101         }
00102 
00110         inline T* get_element_ptr(int32_t index)
00111         {
00112             return array->get_element_ptr(index);
00113         }
00114 
00123         inline T operator[](int32_t index) const
00124         {
00125             return array->get_element(index);
00126         }
00127 
00129         inline T* get_array()
00130         {
00131             return array->get_array();
00132         }
00133 
00135         inline virtual const char* get_name() const { return "Set"; }
00136 
00137     protected:
00139         DynArray<T>* array;
00140 };
00141 }
00142 #endif //_SET_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation