SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGString.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Fernando José Iglesias García
8  * Written (W) 2010,2012 Soeren Sonnenburg
9  * Written (W) 2012 Jacob Walker
10  * Copyright (C) 2010 Berlin Institute of Technology
11  * Copyright (C) 2012 Soeren Sonnenburg
12  */
13 #ifndef __SGSTRING_H__
14 #define __SGSTRING_H__
15 
16 #include <shogun/lib/config.h>
17 #include <shogun/lib/DataType.h>
18 #include <shogun/lib/SGVector.h>
19 
20 namespace shogun
21 {
22 
24 template<class T> class SGString
25 {
26 public:
28  SGString() : string(NULL), slen(0), do_free(false) { }
29 
31  SGString(T* s, index_t l, bool free_s=false)
32  : string(s), slen(l), do_free(free_s) { }
33 
36  : string(v.vector), slen(v.vlen), do_free(v.do_free) { }
37 
39  SGString(index_t len, bool free_s=false) :
40  slen(len), do_free(free_s)
41  {
42  string=SG_MALLOC(T, len);
43  }
44 
46  SGString(const SGString &orig)
47  : string(orig.string), slen(orig.slen), do_free(orig.do_free) { }
48 
50  bool operator==(const SGString & other) const
51  {
52  if (other.slen != slen)
53  return false;
54 
55  for (int i = 0; i < slen; i++)
56  {
57  if (other.string[i] != string[i])
58  return false;
59  }
60 
61  return true;
62  }
63 
65  void free_string()
66  {
67  if (do_free)
68  SG_FREE(string);
69 
70  string=NULL;
71  do_free=false;
72  slen=0;
73  }
74 
77  {
78  do_free=true;
79  free_string();
80  }
81 
82 public:
84  T* string;
88  bool do_free;
89 };
90 }
91 #endif // __SGSTRING_H__

SHOGUN Machine Learning Toolbox - Documentation