コード例 #1
0
ファイル: RPTree.cpp プロジェクト: laperlej/geec_tools
    RPTreeNode* RPTree::readRPTreeNode(std::ifstream* fis, uint64_t fileOffset, bool forceDescend) {

        RPTreeNode* thisNode = NULL;
        typedef int8_t byte;
        try {

            // Read node format into a buffer
            fis->clear();
            fis->seekg(fileOffset);

            // find node type
            byte type;

            fis->read( reinterpret_cast<char*>(&type) , sizeof(byte) );
            type=endian::LittleByte(type);

            bool isLeaf;
            uint32_t itemSize;


            //TODO Check the creator of RPTreeNodes, make sure parent value is called and whatbnot
            if (type == 1) {
                isLeaf = true;
                itemSize = RPTREE_NODE_LEAF_ITEM_SIZE;

                RPTreeLeafNode* tempP= new RPTreeLeafNode();
                thisNode =  tempP ;
            } else {
                isLeaf = false;
                itemSize = RPTREE_NODE_CHILD_ITEM_SIZE;
                thisNode = new RPTreeChildNode();
            }
            //nodeCount++;

            uint16_t itemCount;
            byte empty_b;



            fis->read( reinterpret_cast<char*>(&empty_b) , sizeof(byte) );
            empty_b=endian::LittleByte(empty_b);

            fis->read( reinterpret_cast<char*>(&itemCount) , sizeof(uint16_t) );
            itemCount=endian::LittleShort(itemCount);

          //  uint32_t itemBlockSize = itemCount * itemSize;

          //  buffer = new byte[itemBlockSize];            // allocate buffer for item sisze

            // get the node items - leaves or child nodes
            uint32_t startChromID, endChromID;
            uint32_t startBase, endBase;
            int itemBlockSize = itemCount * itemSize;

            //Readitems into buffer and set to stringstream
            std::vector<char>  buffer(itemBlockSize);
            fis->read(&buffer[0],itemBlockSize);
            std::stringstream itemStream(std::string(buffer.begin(), buffer.end()));
            //Set string stream, does not copy buffer
            //itemStream.rdbuf()->pubsetbuf(&buffer[0],itemBlockSize);

            //TODO make item reading work with buffer
            for (uint32_t item = 0; item < itemCount; ++item) {

                // always extract the bounding rectangle

                //Read from buffer
                itemStream.read( reinterpret_cast<char*>(&startChromID) , sizeof(uint32_t) );
                startChromID=endian::LittleLong(startChromID);

                itemStream.read( reinterpret_cast<char*>(&startBase) , sizeof(uint32_t) );
                startBase=endian::LittleLong(startBase);

                itemStream.read( reinterpret_cast<char*>(&endChromID) , sizeof(uint32_t) );
                endChromID=endian::LittleLong(endChromID);

                itemStream.read( reinterpret_cast<char*>(&endBase) , sizeof(uint32_t) );
                endBase=endian::LittleLong(endBase);

                if (isLeaf) {
                    uint64_t dataOffset;
                    uint64_t dataSize;


                    itemStream.read( reinterpret_cast<char*>(&dataOffset) , sizeof(uint64_t) );
                    dataOffset=endian::LittleDouble(dataOffset);

                    itemStream.read( reinterpret_cast<char*>(&dataSize) , sizeof(uint64_t) );
                    dataSize=endian::LittleDouble(dataSize);
                    //TODO Check creation here, it was incorrect.
                    thisNode->insertItem(new RPTreeLeafNodeItem(startChromID, startBase, endChromID, endBase,
                            dataOffset, dataSize));
                } else {
                    // get the child node pointed to in the node item

                    uint64_t nodeOffset=0;

                    itemStream.read( reinterpret_cast<char*>(&nodeOffset) , sizeof(uint64_t) );
                    nodeOffset=endian::LittleDouble(nodeOffset);


                    // Recursive call to read next child node
                    // The test on chromIds is designed to stop the descent when the tree reaches the level of an
                    // individual chromosome.  These are loaded later on demand.
                    RPTreeNode* childNode;
                    if (startChromID != endChromID || forceDescend) {
                        childNode = readRPTreeNode(fis, nodeOffset, forceDescend);
                    } else {
                        childNode = new RPTreeNodeProxy(*fis, nodeOffset, startChromID);
                    }

                    // insert child node item
                    thisNode->insertItem(new RPTreeChildNodeItem(startChromID, startBase, endChromID,
                            endBase, childNode));

                }
                fileOffset += itemSize;
            }

        } catch (...) {
            throw new std::runtime_error("Error reading R+ tree nodes: \n");
        }

        // return success
        return thisNode;
    }
コード例 #2
0
ファイル: IOHandler.cpp プロジェクト: BasemElbarashy/CDL
void IOHandler::loadDatasetsAndQuestionsFromFile(string FilePath)
{
    int nDatasets,datasetSize,questionsSize;
    string action;
    string question;
    char ch;
    string str;
    int index;
    vector<string> dataset;
    vector<string> questions;
    vector <string> answers;

    ifstream inFile(FilePath.c_str());
    while(1)
    {
        inFile>>ch;
        inFile>>index;
        inFile>>ch;
        //read dataset
        inFile>>datasetSize;

        getline(inFile, action); //to avoid "\n"
        for(int j=0;j<datasetSize;j++){
            getline(inFile, action);
            dataset.push_back(action);
        }

        //read questions and answers
        inFile>>questionsSize;
        if(!getline(inFile, action)) break; //to avoid "\n"

        for(int j=0;j<questionsSize;j++){
            question = "";
            getline(inFile, action);
            istringstream itemStream(action);
            string item;
            while(!itemStream.eof())
            {
                itemStream>>item;
                if(question == "") question  = item;
                else               question  = question +" "+item;
            }
            for(int k=(question.size()-1);;k--)
            {
                if(question[k] == ' ')     {question.erase(k,1); break;}
                question.erase(k,1);
            }
            answers.push_back(item);
            questions.push_back(question);
        }
        //save

        memory->addDatasetQuestionsAnswers(dataset,questions,answers);
        dataset.clear();
        questions.clear();
        answers.clear();
    }
    inFile.close();


}