Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI colls in not really C++. But cleaner than before.
[simgrid.git] / src / smpi / smpi_f2c.cpp
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include <vector>
9
10
11 namespace simgrid{
12 namespace smpi{
13
14 xbt_dict_t F2C::f2c_lookup_=nullptr;
15 int F2C::f2c_id_=0;
16
17 char* F2C::get_key(char* key, int id) {
18   snprintf(key, KEY_SIZE, "%x",id);
19   return key;
20 }
21
22 char* F2C::get_key_id(char* key, int id) {
23   snprintf(key, KEY_SIZE, "%x_%d",id, smpi_process_index());
24   return key;
25 }
26
27 void F2C::delete_lookup(){
28   xbt_dict_free(&f2c_lookup_);
29 }
30
31 xbt_dict_t F2C::lookup(){
32   return f2c_lookup_;
33 }
34
35 void F2C::free_f(int id){
36   char key[KEY_SIZE];
37   xbt_dict_remove(f2c_lookup_, get_key(key, id));
38 }
39
40 int F2C::add_f(){
41   if(f2c_lookup_==nullptr){
42     f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
43   }
44   char key[KEY_SIZE];
45   xbt_dict_set(f2c_lookup_, get_key(key, f2c_id_), this, nullptr);
46   f2c_id_++;
47   return f2c_id_-1;
48 }
49
50 int F2C::c2f(){
51   if(f2c_lookup_==nullptr){
52     f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
53   }
54
55   char* existing_key = xbt_dict_get_key(f2c_lookup_, this);
56   if(existing_key!=nullptr){
57     return atoi(existing_key);}
58   else{
59     return this->add_f();}
60 }
61
62 F2C* F2C::f2c(int id){
63   if(f2c_lookup_==nullptr){
64     f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
65   }
66   char key[KEY_SIZE];
67   if(id >= 0){
68     return static_cast<F2C*>(xbt_dict_get_or_null(f2c_lookup_, get_key(key, id)));
69   }else
70     return NULL;
71 }
72
73 }
74 }