SGString.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) 2012 Fernando José Iglesias García
00008  * Written (W) 2010,2012 Soeren Sonnenburg
00009  * Written (W) 2012 Jacob Walker
00010  * Copyright (C) 2010 Berlin Institute of Technology
00011  * Copyright (C) 2012 Soeren Sonnenburg
00012  */
00013 #ifndef __SGSTRING_H__
00014 #define __SGSTRING_H__
00015 
00016 #include <shogun/lib/config.h>
00017 #include <shogun/lib/DataType.h>
00018 #include <shogun/lib/SGVector.h>
00019 
00020 namespace shogun
00021 {
00022 
00024 template<class T> class SGString
00025 {
00026 public:
00028     SGString() : string(NULL), slen(0), do_free(false) { }
00029 
00031     SGString(T* s, index_t l, bool free_s=false)
00032         : string(s), slen(l), do_free(free_s) { }
00033 
00035     SGString(SGVector<T> v)
00036         : string(v.vector), slen(v.vlen), do_free(v.do_free) { }
00037 
00039     SGString(index_t len, bool free_s=false) :
00040         slen(len), do_free(free_s)
00041     {
00042         string=SG_CALLOC(T, len);
00043     }
00044 
00046     SGString(const SGString &orig)
00047         : string(orig.string), slen(orig.slen), do_free(orig.do_free) { }
00048 
00050     bool operator==(const SGString & other) const
00051     {
00052         if (other.slen != slen)
00053             return false;
00054 
00055         for (int i = 0; i < slen; i++)
00056         {
00057             if (other.string[i] != string[i])
00058                 return false;
00059         }
00060 
00061         return true;
00062     }
00063 
00065     void free_string()
00066     {
00067         if (do_free)
00068             SG_FREE(string);
00069 
00070         string=NULL;
00071         do_free=false;
00072         slen=0;
00073     }
00074 
00076     void destroy_string()
00077     {
00078         do_free=true;
00079         free_string();
00080     }
00081 
00082 public:
00084     T* string;
00086     index_t slen;
00088     bool do_free;
00089 };
00090 }
00091 #endif // __SGSTRING_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation