0

我需要编写一个图形文件解析器(该图形由 TGFF 作为基于标识符/变量的文本文件生成)

@TASK_GRAPH 0 {
PERIOD 1100

TASK t0_0   TYPE 13 
TASK t0_1   TYPE 3 
TASK t0_2   TYPE 10 
    .
    .

ARC a0_0    FROM t0_0  TO  t0_1 TYPE 9
ARC a0_1    FROM t0_1  TO  t0_2 TYPE 0
ARC a0_2    FROM t0_2  TO  t0_3 TYPE 37
    .
    .
}

#------------------------------------------------------------------------------
# type    exec_time
  0      71.659
  1     59.3856
  2     64.7101

这是迄今为止 iv 得到的(不要介意零散的代码......这只是 iv 到目前为止的一个例子)

void read(char* graph){
     //open task graph description
     string name;
     string TaskList[300][300];
     ifstream gfile;
     gfile.open (graph);
     if ( !gfile.is_open() ) cout<<"Could not open graph description file\n";
     //start parsing
     while(getline(gfile,inptext)){
        istringstream sstream(inptext);
        int i=0;
        sstream >> name;
        if(name.compare("TASK")==0){
            sstream >> name;i
            //wrte node name to hash index i++
            sstream >> name;
            if (name.compare("TYPE")==0){
                sstream >> name;
                //retrieve node index from hash
                //write node weight to matrix
            }
        }
        if(name.compare("ARC")==0){
           sstream >> name;
           //write edge name to hash index i++
           sstream >> name;
           if (name.compare("FROM")==0){
              sstream >> name;
              //retrieve node index a from hash
           }
            sstream >> name;
            if (name.compare("TO")==0){
              sstream >> name;
              //retrieve node index b from hash
              if (name.compare("TYPE")==0){
                sstream >> name;
                //write edge weight to matrix index a b
              }
            }
         }
        i++;
     }
     //end parsing
     gfile.close();
 }

因为在我没有费心阅读它们之前,我没有使用过令牌。现在我遇到的问题是TYPE从文件底部读取值的周期,因为它们的标识符是常规数字,你不能让它们成为要搜索的标识符。我猜最好的方法是寻找“# type”,但由于我使用的是字符串流,所以处理起来有点困难。第二个问题是节点的数量事先是未知的,所以我无法初始化数组矩阵以适应图形的大小......

我是不是该:

  • a) 两次读取文件 - 一次用于计算节点,另一次用于实际读取数据
  • b)使用向量/矩阵类型(我以前没有使用过,我认为我必须构建一个矩阵类)
  • c) 使用其他东西

我正在考虑的矩阵应该是 matrix[x][x] 其中 n 是节点权重, e 是边权重(未镜像,因为边只走一种方式)到目前为止,我计划用类型号填充矩阵并读取稍后来自哈希或向量的类型并用正确的值替换它们

  A B C D E
A n 0 0 0 0
B e n 0 0 0
C e e n 0 0
D e e e n 0
E e e e e n

如果有人知道如何更轻松地读取和解析此文件,那就太好了。

4

1 回答 1

1

为了解决将任务名称映射到标识符的问题,例如映射t0_0到 0、 t0_1到 1 以及处理节点名称的其他格式,我建议使用任务名称作为键和矩阵索引作为值的哈希表。

下面的函数将返回给定任务名称的索引。如果第一次遇到任务名称,它将创建一个新索引,将其分配给任务名称并返回。

#include <unordered_map>
int tasks = 0;
std::unordered_map<std::string, int> hashNameToIndex;

int nameToIndex(string name){    
  if (hashNameToIndex.find(name) == hashNameToIndex.end()){
     hashNameToIndex[name] = tasks;
     tasks++;
     return tasks-1;
  }else{
     return hashNameToIndex[name];
  }
}
于 2013-05-02T07:30:22.677 回答