通过创建循环来简化某些重复动作的代码

时间:2019-02-20 22:04:35

标签: c++

以下是我的代码,用于对一系列整数类型的端口执行数据重置。

public void getOutputsForTheseInputsUsingThisNet(String netFilePath,String inputFileDir) throws Exception {

    //open the network file
    File locationToSave = new File(netFilePath);
    MultiLayerNetwork nNet = null;
    logger.info("Trying to open the model");
    try {
        nNet = ModelSerializer.restoreMultiLayerNetwork(locationToSave);
        logger.info("Success: Model opened");
    } catch (IOException e) {
        throw new Exception(String.format("Unable to open model from %s because of error %s", locationToSave.getAbsolutePath(),e.getMessage()));
    }

    logger.info("Loading test data");
    SequenceRecordReader testFeatures = new CSVSequenceRecordReader(0, ",");  //skip no lines at the top - i.e. no header
    try {
        testFeatures.initialize(new NumberedFileInputSplit(inputFileDir + "/features/s_4180%d.csv", 0, 4));
    } catch (InterruptedException e) {
        testFeatures.close();
        throw new Exception(String.format("IO error %s. during testFeatures", e.getMessage()));
    }
    logger.info("Loading label data");
    SequenceRecordReader testLabels = new CSVSequenceRecordReader();
    try {
        testLabels.initialize(new NumberedFileInputSplit(inputFileDir + "/labels/s_4180%d.csv", 0,4));
    } catch (InterruptedException e) {
        testLabels.close();
        testFeatures.close();
        throw new IOException(String.format("Interrupted exception error %s. during testLabels initialise", e.getMessage()));
    }


    //DataSetIterator inputData = new Seque
    logger.info("creating iterator");

    DataSetIterator testData =  new SequenceRecordReaderDataSetIterator(testFeatures, testLabels,
            this._modelDefinition.getBATCH_SIZE(),this._modelDefinition.getNUM_LABEL_CLASSES(), false, SequenceRecordReaderDataSetIterator.AlignmentMode.ALIGN_END);


    //now use it to classify some data
    logger.info("classifying examples");

    INDArray output = nNet.output(testData);
    logger.info("outputing the classifications");
    if(output==null||output.isEmpty())
        throw new Exception("There is no output");
    System.out.println(output);

    //sample output

在上面的代码中,有不同类型的端口(分组在一起),它们由} 处理。是否可以通过创建一些循环来简化这些冗长的代码列表,而无需更改 //note function: void resetData(int pin); resetData(p00); resetData(p01); resetData(p02); resetData(p03); resetData(p04); resetData(p05); resetData(p06); resetData(p07); resetData(p10); resetData(p11); resetData(p12); resetData(p13); resetData(p14); resetData(p15); resetData(p16); resetData(p17); resetData(p50); resetData(p51); resetData(p80); resetData(p81); resetData(p82); resetData(p83); resetData(p84); resetData(p85); resetData(p86); resetData(p87); resetData(p110); resetData(p111); resetData(p112); resetData(p113); resetData(p114); resetData(p115); resetData(p116); resetData(p117); 的定义?谢谢!

2 个答案:

答案 0 :(得分:2)

您可以创建一个适用于引脚组的函数:

void myResetData(const std::vector<int> &pins)
{
    for (auto pin : pins)
    {
        resetData(pin);
    }
}

然后使用它:

void f()
{
    std::vector<int> group1{ p00, p01, p02, p03, p04, p05, p06, p07 },
                     group2{ p10, p11, p12, p13, p14, p15, p16, p17 };
    myResetData(group1);
    myResetData(group2);
}

或者只是:

void f()
{
    myResetData(std::vector<int>{ p00, p01, p02, p03, p04, p05, p06, p07 });
    myResetData(std::vector<int>{ p10, p11, p12, p13, p14, p15, p16, p17 });
}

您甚至可以摆脱:

void f()
{
    myResetData({ p00, p01, p02, p03, p04, p05, p06, p07 });
    myResetData({ p10, p11, p12, p13, p14, p15, p16, p17 });
}

答案 1 :(得分:0)

int ports[] = {p00,p01,p02,p03,p04,p05,p06,p07,
               p10,p11,p12,p13,p14,p15,p16,p17,
               p50,p51,
               p80,p81,p82,p83,p84,p85,p86,p87,
               p110,p111,p112,p113,p114,p115,p116,p117};
int portsNum = sizeof(ports)/sizeof(*ports);
for(int i=0;i<portsNum;i++)
{
    resetData(ports[i]);
}