string_db.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*************************************************************************/
  2. /* string_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "string_db.h"
  30. #include "print_string.h"
  31. StaticCString StaticCString::create(const char *p_ptr) {
  32. StaticCString scs; scs.ptr=p_ptr; return scs;
  33. }
  34. StringName::_Data *StringName::_table[STRING_TABLE_LEN];
  35. StringName _scs_create(const char *p_chr) {
  36. return (p_chr[0]?StringName(StaticCString::create(p_chr)):StringName());
  37. }
  38. bool StringName::configured=false;
  39. void StringName::setup() {
  40. ERR_FAIL_COND(configured);
  41. for(int i=0;i<STRING_TABLE_LEN;i++) {
  42. _table[i]=NULL;
  43. }
  44. configured=true;
  45. }
  46. void StringName::cleanup() {
  47. _global_lock();
  48. for(int i=0;i<STRING_TABLE_LEN;i++) {
  49. while(_table[i]) {
  50. _Data*d=_table[i];
  51. _table[i]=_table[i]->next;
  52. memdelete(d);
  53. }
  54. }
  55. _global_unlock();
  56. }
  57. void StringName::unref() {
  58. ERR_FAIL_COND(!configured);
  59. if (_data && _data->refcount.unref()) {
  60. _global_lock();
  61. if (_data->prev) {
  62. _data->prev->next=_data->next;
  63. } else {
  64. if (_table[_data->idx]!=_data) {
  65. ERR_PRINT("BUG!");
  66. }
  67. _table[_data->idx]=_data->next;
  68. }
  69. if (_data->next) {
  70. _data->next->prev=_data->prev;
  71. }
  72. memdelete(_data);
  73. _global_unlock();
  74. }
  75. _data=NULL;
  76. }
  77. bool StringName::operator==(const String& p_name) const {
  78. if (!_data) {
  79. return (p_name.length()==0);
  80. }
  81. return (_data->get_name()==p_name);
  82. }
  83. bool StringName::operator==(const char* p_name) const {
  84. if (!_data) {
  85. return (p_name[0]==0);
  86. }
  87. return (_data->get_name()==p_name);
  88. }
  89. bool StringName::operator!=(const String& p_name) const {
  90. return !(operator==(p_name));
  91. }
  92. bool StringName::operator!=(const StringName& p_name) const {
  93. // the real magic of all this mess happens here.
  94. // this is why path comparisons are very fast
  95. return _data!=p_name._data;
  96. }
  97. void StringName::operator=(const StringName& p_name) {
  98. if (this==&p_name)
  99. return;
  100. unref();
  101. if (p_name._data && p_name._data->refcount.ref()) {
  102. _data = p_name._data;
  103. }
  104. }
  105. StringName::operator String() const {
  106. if (_data)
  107. return _data->get_name();
  108. return "";
  109. }
  110. StringName::StringName(const StringName& p_name) {
  111. ERR_FAIL_COND(!configured);
  112. _data=NULL;
  113. if (p_name._data && p_name._data->refcount.ref()) {
  114. _data = p_name._data;
  115. }
  116. }
  117. StringName::StringName(const char *p_name) {
  118. _data=NULL;
  119. ERR_FAIL_COND(!configured);
  120. ERR_FAIL_COND( !p_name || !p_name[0]);
  121. _global_lock();
  122. uint32_t hash = String::hash(p_name);
  123. uint32_t idx=hash&STRING_TABLE_MASK;
  124. _data=_table[idx];
  125. while(_data) {
  126. // compare hash first
  127. if (_data->hash==hash && _data->get_name()==p_name)
  128. break;
  129. _data=_data->next;
  130. }
  131. if (_data) {
  132. if (_data->refcount.ref()) {
  133. // exists
  134. _global_unlock();
  135. return;
  136. } else {
  137. }
  138. }
  139. _data = memnew( _Data );
  140. _data->name=p_name;
  141. _data->refcount.init();
  142. _data->hash=hash;
  143. _data->idx=idx;
  144. _data->cname=NULL;
  145. _data->next=_table[idx];
  146. _data->prev=NULL;
  147. if (_table[idx])
  148. _table[idx]->prev=_data;
  149. _table[idx]=_data;
  150. _global_unlock();
  151. }
  152. StringName::StringName(const StaticCString& p_static_string) {
  153. _data=NULL;
  154. ERR_FAIL_COND(!configured);
  155. ERR_FAIL_COND( !p_static_string.ptr || !p_static_string.ptr[0]);
  156. _global_lock();
  157. uint32_t hash = String::hash(p_static_string.ptr);
  158. uint32_t idx=hash&STRING_TABLE_MASK;
  159. _data=_table[idx];
  160. while(_data) {
  161. // compare hash first
  162. if (_data->hash==hash && _data->get_name()==p_static_string.ptr)
  163. break;
  164. _data=_data->next;
  165. }
  166. if (_data) {
  167. if (_data->refcount.ref()) {
  168. // exists
  169. _global_unlock();
  170. return;
  171. } else {
  172. }
  173. }
  174. _data = memnew( _Data );
  175. _data->refcount.init();
  176. _data->hash=hash;
  177. _data->idx=idx;
  178. _data->cname=p_static_string.ptr;
  179. _data->next=_table[idx];
  180. _data->prev=NULL;
  181. if (_table[idx])
  182. _table[idx]->prev=_data;
  183. _table[idx]=_data;
  184. _global_unlock();
  185. }
  186. StringName::StringName(const String& p_name) {
  187. _data=NULL;
  188. ERR_FAIL_COND(!configured);
  189. _global_lock();
  190. uint32_t hash = p_name.hash();
  191. uint32_t idx=hash&STRING_TABLE_MASK;
  192. _data=_table[idx];
  193. while(_data) {
  194. if (_data->hash==hash && _data->get_name()==p_name)
  195. break;
  196. _data=_data->next;
  197. }
  198. if (_data) {
  199. if (_data->refcount.ref()) {
  200. // exists
  201. _global_unlock();
  202. return;
  203. } else {
  204. }
  205. }
  206. _data = memnew( _Data );
  207. _data->name=p_name;
  208. _data->refcount.init();
  209. _data->hash=hash;
  210. _data->idx=idx;
  211. _data->cname=NULL;
  212. _data->next=_table[idx];
  213. _data->prev=NULL;
  214. if (_table[idx])
  215. _table[idx]->prev=_data;
  216. _table[idx]=_data;
  217. _global_unlock();
  218. }
  219. StringName StringName::search(const char *p_name) {
  220. ERR_FAIL_COND_V(!configured,StringName());
  221. ERR_FAIL_COND_V( !p_name, StringName() );
  222. if (!p_name[0])
  223. return StringName();
  224. _global_lock();
  225. uint32_t hash = String::hash(p_name);
  226. uint32_t idx=hash&STRING_TABLE_MASK;
  227. _Data *_data=_table[idx];
  228. while(_data) {
  229. // compare hash first
  230. if (_data->hash==hash && _data->get_name()==p_name)
  231. break;
  232. _data=_data->next;
  233. }
  234. if (_data && _data->refcount.ref()) {
  235. _global_unlock();
  236. return StringName(_data);
  237. }
  238. _global_unlock();
  239. return StringName(); //does not exist
  240. }
  241. StringName StringName::search(const CharType *p_name) {
  242. ERR_FAIL_COND_V(!configured,StringName());
  243. ERR_FAIL_COND_V( !p_name, StringName() );
  244. if (!p_name[0])
  245. return StringName();
  246. _global_lock();
  247. uint32_t hash = String::hash(p_name);
  248. uint32_t idx=hash&STRING_TABLE_MASK;
  249. _Data *_data=_table[idx];
  250. while(_data) {
  251. // compare hash first
  252. if (_data->hash==hash && _data->get_name()==p_name)
  253. break;
  254. _data=_data->next;
  255. }
  256. if (_data && _data->refcount.ref()) {
  257. _global_unlock();
  258. return StringName(_data);
  259. }
  260. _global_unlock();
  261. return StringName(); //does not exist
  262. }
  263. StringName StringName::search(const String &p_name) {
  264. ERR_FAIL_COND_V( p_name=="", StringName() );
  265. _global_lock();
  266. uint32_t hash = p_name.hash();
  267. uint32_t idx=hash&STRING_TABLE_MASK;
  268. _Data *_data=_table[idx];
  269. while(_data) {
  270. // compare hash first
  271. if (_data->hash==hash && p_name==_data->get_name())
  272. break;
  273. _data=_data->next;
  274. }
  275. if (_data && _data->refcount.ref()) {
  276. _global_unlock();
  277. return StringName(_data);
  278. }
  279. _global_unlock();
  280. return StringName(); //does not exist
  281. }
  282. StringName::StringName() {
  283. _data=NULL;
  284. }
  285. StringName::~StringName() {
  286. unref();
  287. }