Herramientas de usuario

Herramientas del sitio


rf_map

RF_Map

La clase RF_Map es la clase que controla la lógica de almacenamiento de las instancias de RF_Process.

Implementación

struct Node
{
    string key;
    RF_Process* data;
 
    Node *son;
    Node *firstChild;
 
    void addChild(string key, RF_Process *dat)
    {
        Node *n = new Node();
        n->key = key;
        n->data = dat;
 
        n->son = firstChild;
        firstChild = n;
    }
};
 
class RF_Map
{
    protected:
        Node *root = new Node();
        unordered_map<string, RF_Process*> taskMap;
        multimap<string, RF_Process*> typeMap;
 
        void Add(string key, RF_Process *dat)
        {
            vector<string> v;
            split(key,':',v);
 
            Node *it = root;
            Node *aux;
            for (int i = 1; i < v.size(); i++)
            {
                aux = it->firstChild;
                while (aux != NULL && aux->key != v[i])
                {
                    aux = aux->son;
                }
 
                if (aux != NULL)
                {
                    it = aux;
                }
                else
                {
                    it->addChild(v[v.size()-1], dat);
                    taskMap[key] = dat;
                    typeMap.insert(pair<string, RF_Process*>(dat->type, dat));
                    return;
                }
            }
        }
 
        bool isInList(string key)
        {
            return (Get(key) != nullptr);
        }
 
        RF_Process* Get(string key)
        {
            return taskMap[key];
        }
 
        void Remove(string key)
        {
          pair<multimap<string, RF_Process*>::iterator,multimap<string, RF_Process*>::iterator> par = typeMap.equal_range(taskMap[key]->type);
          for(auto it = par.first; it != par.second; it++)
          {
            if(it->second != nullptr && it->second->id == key)
            {
                typeMap.erase(it);
                break;
            }
          }
 
          delete(taskMap[key]);
          taskMap.erase(key);
        }
 
        void split(const string& s, char delim,vector<string>& v){
            int i = 0;
            int pos = s.find(delim);
            while (pos != string::npos)
            {
                v.push_back(s.substr(i, pos-i));
                i = ++pos;
                pos = s.find(delim, pos);
 
                if (pos == string::npos)
                {
                    v.push_back(s.substr(i, s.length()));
                }
            }
        }
};
Categorías: Clases
rf_map.txt · Última modificación: 2018/01/19 16:51 por Yawin