/usr/include/shogun/lib/Map.h is in libshogun-dev 3.2.0-7.5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Evgeniy Andreev (gsomix)
*
* Copyright (C) 2012 Evgeniy Andreev (gsomix)
*/
#ifndef _MAP_H_
#define _MAP_H_
#include <shogun/base/SGObject.h>
#include <shogun/lib/common.h>
#include <shogun/lib/Hash.h>
#include <shogun/base/DynArray.h>
#include <cstdio>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Lock.h>
namespace shogun
{
#define IGNORE_IN_CLASSLIST
#define MapNode CMapNode<K, T>
#ifndef DOXYGEN_SHOULD_SKIP_THIS
/** hashset node */
IGNORE_IN_CLASSLIST template<class K, class T> struct CMapNode
{
/** index in hashtable */
int32_t index;
/** is free? */
bool free;
/** key of node */
K key;
/** data of node */
T data;
/** pointer to left sibling */
CMapNode *left;
/** pointer to right sibling */
CMapNode *right;
};
#endif
/** @brief the class CMap, a map based on the hash-table.
* w: http://en.wikipedia.org/wiki/Hash_table
*/
IGNORE_IN_CLASSLIST template<class K, class T> class CMap: public CSGObject
{
public:
/** Custom constructor */
CMap(int32_t size=41, int32_t reserved=128, bool tracable=true)
{
hash_size=size;
free_index=0;
num_elements=0;
use_sg_mallocs=tracable;
if (use_sg_mallocs)
hash_array=SG_CALLOC(MapNode*, size);
else
hash_array=(CMapNode<K, T>**) calloc(size, sizeof(CMapNode<K, T>*));
for (int32_t i=0; i<size; i++)
{
hash_array[i]=NULL;
}
array=new DynArray<CMapNode<K, T>*>(reserved, tracable);
}
/** Default destructor */
virtual ~CMap()
{
destroy_map();
}
/** @return object name */
virtual const char* get_name() const { return "Map"; }
/** Add an element to the map
*
* @param key key to be added
* @param data data to be added
* @return index of added element
*/
int32_t add(const K& key, const T& data)
{
int32_t index=hash(key);
if (chain_search(index, key)==NULL)
{
lock.lock();
int32_t added_index=insert_key(index, key, data);
num_elements++;
lock.unlock();
return added_index;
}
return -1;
}
/** Check an element in the map
*
* @param key key to be looked for
* @return true if element contains in the map
*/
bool contains(const K& key)
{
int32_t index=hash(key);
if (chain_search(index, key)!=NULL)
return true;
return false;
}
/** Remove an element from the set
*
* @param key key to be removed
*/
void remove(const K& key)
{
int32_t index=hash(key);
CMapNode<K, T>* result=chain_search(index, key);
if (result!=NULL)
{
lock.lock();
delete_key(index, result);
num_elements--;
lock.unlock();
}
}
/** Index of element in the set
*
* @param key key to be looked for
* @return index of the element or -1 if not found
*/
int32_t index_of(const K& key)
{
int32_t index=hash(key);
CMapNode<K ,T>* result=chain_search(index, key);
if (result!=NULL)
return result->index;
return -1;
}
/** Get element by key
*
* @param key key to be looked for
* @return exist element or new element
* (if key doesn't consist in map)
*/
T get_element(const K& key)
{
int32_t index=hash(key);
CMapNode<K, T>* result=chain_search(index, key);
if (result!=NULL)
return result->data;
else
{
int32_t added_index=add(key, T());
result=get_node_ptr(added_index);
return result->data;
}
}
/** Set element by key
*
* @param key key of element
* @param data new data for element
*/
void set_element(const K& key, const T& data)
{
int32_t index=hash(key);
CMapNode<K, T>* result=chain_search(index, key);
lock.lock();
if (result!=NULL)
result->data=data;
else
add(key, data);
lock.unlock();
}
/** Get number of elements
*
* @return number of elements
*/
int32_t get_num_elements() const
{
return num_elements;
}
/** Get size of auxilary array
*
* @return array size
*/
int32_t get_array_size() const
{
return array->get_num_elements();
}
/** get element at index as reference
*
* (does NOT do bounds checking)
*
* @param index index
* @return array element at index
*/
T* get_element_ptr(int32_t index)
{
CMapNode<K, T>* result=array->get_element(index);
if (result!=NULL && !is_free(result))
return &(array->get_element(index)->data);
return NULL;
}
/** get node at index as reference
*
* (does NOT do bounds checking)
*
* @param index index
* @return node at index
*/
CMapNode<K, T>* get_node_ptr(int32_t index)
{
return array->get_element(index);
}
/** @return underlying array of nodes in memory */
CMapNode<K, T>** get_array()
{
return array->get_array();
}
/** assignment operator that copies map */
CMap& operator =(const CMap& orig)
{
destroy_map();
use_sg_mallocs = orig.use_sg_mallocs;
hash_size = orig.hash_size;
if (use_sg_mallocs)
hash_array = SG_CALLOC(MapNode*, hash_size);
else
hash_array = (CMapNode<K, T>**) calloc(hash_size,
sizeof(CMapNode<K, T>*));
for (int32_t i = 0; i<hash_size; i++)
{
hash_array[i] = NULL;
}
array = new DynArray<CMapNode<K, T>*>(128, use_sg_mallocs);
for (int i = 0; i < orig.num_elements; i++)
{
CMapNode<K, T>* node = orig.array->get_element(i);
add(node->key, node->data);
}
return *this;
}
private:
/** Returns hash of key
* MurmurHash used
*/
int32_t hash(const K& key)
{
return CHash::MurmurHash3((uint8_t*)(&key), sizeof(key), 0xDEADBEEF) % hash_size;
}
/** is free? */
bool is_free(CMapNode<K, T>* node)
{
if (node->free==true)
return true;
return false;
}
/** Searchs key in list(chain) */
CMapNode<K, T>* chain_search(int32_t index, const K& key)
{
if (hash_array[index]==NULL)
{
return NULL;
}
else
{
CMapNode<K, T>* current=hash_array[index];
do // iterating all items in the list
{
if (current->key==key)
return current; // it's a search key
current=current->right;
} while (current!=NULL);
return NULL;
}
}
/** Inserts nodes with certain key and data in set */
int32_t insert_key(int32_t index, const K& key, const T& data)
{
int32_t new_index;
CMapNode<K, T>* new_node;
if ((free_index>=array->get_num_elements()) || (array->get_element(free_index)==NULL))
{
// init new node
if (use_sg_mallocs)
new_node=SG_CALLOC(MapNode, 1);
else
new_node=(CMapNode<K, T>*) calloc(1, sizeof(CMapNode<K, T>));
new (&new_node->key) K();
new (&new_node->data) T();
array->append_element(new_node);
new_index=free_index;
free_index++;
}
else
{
new_node=array->get_element(free_index);
ASSERT(is_free(new_node))
new_index=free_index;
free_index=new_node->index;
}
new_node->index=new_index;
new_node->free=false;
new_node->key=key;
new_node->data=data;
new_node->left=NULL;
new_node->right=NULL;
// add new node in start of list
if (hash_array[index]==NULL)
{
hash_array[index]=new_node;
}
else
{
hash_array[index]->left=new_node;
new_node->right=hash_array[index];
hash_array[index]=new_node;
}
return new_index;
}
/** Deletes key from set */
void delete_key(int32_t index, CMapNode<K, T>* node)
{
int32_t temp=0;
if (node==NULL)
return;
if (node->right!=NULL)
node->right->left = node->left;
if (node->left!=NULL)
node->left->right = node->right;
else
hash_array[index] = node->right;
temp=node->index;
node->index=free_index;
node->free=true;
node->left=NULL;
node->right=NULL;
free_index=temp;
}
/*cleans up map*/
void destroy_map()
{
if (array!=NULL)
{
for(int32_t i=0; i<array->get_num_elements(); i++)
{
CMapNode<K, T>* element = array->get_element(i);
if (element!=NULL)
{
element->key.~K();
element->data.~T();
if (use_sg_mallocs)
SG_FREE(element);
else
free(element);
}
}
delete array;
}
if (hash_array!=NULL)
{
if (use_sg_mallocs)
SG_FREE(hash_array);
else
free(hash_array);
}
}
protected:
/** whether SG_MALLOC or just malloc etc shall be used */
bool use_sg_mallocs;
/** hashtable size */
int32_t hash_size;
/** next free index for new element */
int32_t free_index;
/** number of elements */
int32_t num_elements;
/** array of lists (chains) */
CMapNode<K, T>** hash_array;
/** array for index permission */
DynArray<CMapNode<K, T>*>* array;
/** concurrency lock */
CLock lock;
};
}
#endif /* _MAP_H_ */
|