<commit_before>/*
    open source routing machine
    Copyright (C) Dennis Luxen, 2010

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
or see http://www.gnu.org/licenses/agpl.txt.
 */

#include "Algorithms/IteratorBasedCRC32.h"
#include "Contractor/Contractor.h"
#include "Contractor/EdgeBasedGraphFactory.h"
#include "DataStructures/BinaryHeap.h"
#include "DataStructures/DeallocatingVector.h"
#include "DataStructures/QueryEdge.h"
#include "DataStructures/StaticGraph.h"
#include "DataStructures/StaticRTree.h"
#include "Util/IniFile.h"
#include "Util/GraphLoader.h"
#include "Util/InputFileUtil.h"
#include "Util/LuaUtil.h"
#include "Util/OpenMPWrapper.h"
#include "Util/OSRMException.h"
#include "Util/SimpleLogger.h"
#include "Util/StringUtil.h"
#include "typedefs.h"

#include <boost/foreach.hpp>

#include <luabind/luabind.hpp>

#include <fstream>
#include <istream>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>

typedef QueryEdge::EdgeData EdgeData;
typedef DynamicGraph<EdgeData>::InputEdge InputEdge;
typedef StaticGraph<EdgeData>::InputEdge StaticEdge;
typedef IniFile ContractorConfiguration;

std::vector<NodeInfo> internalToExternalNodeMapping;
std::vector<TurnRestriction> inputRestrictions;
std::vector<NodeID> bollardNodes;
std::vector<NodeID> trafficLightNodes;
std::vector<ImportEdge> edgeList;

int main (int argc, char *argv[]) {
    try {
        LogPolicy::GetInstance().Unmute();
        if(argc < 3) {
            SimpleLogger().Write(logWARNING) <<
                "usage: \n" <<
                argv[0] << " <osrm-data> <osrm-restrictions> [<profile>]";
            return -1;
        }

        double startupTime = get_timestamp();
        unsigned number_of_threads = omp_get_num_procs();
        if(testDataFile("contractor.ini")) {
            ContractorConfiguration contractorConfig("contractor.ini");
            unsigned rawNumber = stringToInt(contractorConfig.GetParameter("Threads"));
            if(rawNumber != 0 && rawNumber <= number_of_threads)
                number_of_threads = rawNumber;
        }
        omp_set_num_threads(number_of_threads);
        LogPolicy::GetInstance().Unmute();
        SimpleLogger().Write() << "Using restrictions from file: " << argv[2];
        std::ifstream restrictionsInstream(argv[2], std::ios::binary);
        if(!restrictionsInstream.good()) {
            std::cerr <<
                "Could not access <osrm-restrictions> files" << std::endl;

        }
        TurnRestriction restriction;
        UUID uuid_loaded, uuid_orig;
        unsigned usableRestrictionsCounter(0);
        restrictionsInstream.read((char*)&uuid_loaded, sizeof(UUID));
        if( !uuid_loaded.TestPrepare(uuid_orig) ) {
            SimpleLogger().Write(logWARNING) <<
                ".restrictions was prepared with different build.\n"
                "Reprocess to get rid of this warning.";
        }

        restrictionsInstream.read(
            (char*)&usableRestrictionsCounter,
            sizeof(unsigned)
        );
        inputRestrictions.resize(usableRestrictionsCounter);
        restrictionsInstream.read(
            (char *)&(inputRestrictions[0]),
            usableRestrictionsCounter*sizeof(TurnRestriction)
        );
        restrictionsInstream.close();

        std::ifstream in;
        in.open (argv[1], std::ifstream::in | std::ifstream::binary);
        if (!in.is_open()) {
            throw OSRMException("Cannot open osrm input file");
        }

        std::string nodeOut(argv[1]);		nodeOut += ".nodes";
        std::string edgeOut(argv[1]);		edgeOut += ".edges";
        std::string graphOut(argv[1]);		graphOut += ".hsgr";
        std::string rtree_nodes_path(argv[1]);  rtree_nodes_path += ".ramIndex";
        std::string rtree_leafs_path(argv[1]);  rtree_leafs_path += ".fileIndex";

        /*** Setup Scripting Environment ***/
        if(!testDataFile( (argc > 3 ? argv[3] : "profile.lua") )) {
            throw OSRMException("Cannot open profile.lua ");
        }

        // Create a new lua state
        lua_State *myLuaState = luaL_newstate();

        // Connect LuaBind to this lua state
        luabind::open(myLuaState);

        //open utility libraries string library;
        luaL_openlibs(myLuaState);

        //adjust lua load path
        luaAddScriptFolderToLoadPath( myLuaState, (argc > 3 ? argv[3] : "profile.lua") );

        // Now call our function in a lua script
        SimpleLogger().Write() <<
            "Parsing speedprofile from " <<
            (argc > 3 ? argv[3] : "profile.lua");

        if(0 != luaL_dofile(myLuaState, (argc > 3 ? argv[3] : "profile.lua") )) {
            std::cerr <<
                lua_tostring(myLuaState,-1)   <<
                " occured in scripting block" <<
                std::endl;
        }

        EdgeBasedGraphFactory::SpeedProfileProperties speedProfile;

        if(0 != luaL_dostring( myLuaState, "return traffic_signal_penalty\n")) {
            std::cerr <<
                lua_tostring(myLuaState,-1) <<
                " occured in scripting block" <<
                std::endl;
                return -1;
        }
        speedProfile.trafficSignalPenalty = 10*lua_tointeger(myLuaState, -1);

        if(0 != luaL_dostring( myLuaState, "return u_turn_penalty\n")) {
            std::cerr <<
                lua_tostring(myLuaState,-1)   <<
                " occured in scripting block" <<
                std::endl;
            return -1;
        }
        speedProfile.uTurnPenalty = 10*lua_tointeger(myLuaState, -1);

        speedProfile.has_turn_penalty_function = lua_function_exists( myLuaState, "turn_function" );

        std::vector<ImportEdge> edgeList;
        NodeID nodeBasedNodeNumber = readBinaryOSRMGraphFromStream(in, edgeList, bollardNodes, trafficLightNodes, &internalToExternalNodeMapping, inputRestrictions);
        in.close();
        SimpleLogger().Write() <<
            inputRestrictions.size() <<
            " restrictions, " <<
            bollardNodes.size() <<
            " bollard nodes, " <<
            trafficLightNodes.size() <<
            " traffic lights";

        if(0 == edgeList.size()) {
            std::cerr <<
                "The input data is broken. "
                "It is impossible to do any turns in this graph" <<
                std::endl;
            return -1;
        }

        /***
         * Building an edge-expanded graph from node-based input an turn restrictions
         */

        SimpleLogger().Write() << "Generating edge-expanded graph representation";
        EdgeBasedGraphFactory * edgeBasedGraphFactory = new EdgeBasedGraphFactory (nodeBasedNodeNumber, edgeList, bollardNodes, trafficLightNodes, inputRestrictions, internalToExternalNodeMapping, speedProfile);
        std::vector<ImportEdge>().swap(edgeList);
        edgeBasedGraphFactory->Run(edgeOut.c_str(), myLuaState);
        std::vector<TurnRestriction>().swap(inputRestrictions);
        std::vector<NodeID>().swap(bollardNodes);
        std::vector<NodeID>().swap(trafficLightNodes);
        NodeID edgeBasedNodeNumber = edgeBasedGraphFactory->GetNumberOfNodes();
        DeallocatingVector<EdgeBasedEdge> edgeBasedEdgeList;
        edgeBasedGraphFactory->GetEdgeBasedEdges(edgeBasedEdgeList);
        std::vector<EdgeBasedGraphFactory::EdgeBasedNode> nodeBasedEdgeList;
        edgeBasedGraphFactory->GetEdgeBasedNodes(nodeBasedEdgeList);
        delete edgeBasedGraphFactory;

        /***
         * Writing info on original (node-based) nodes
         */

        SimpleLogger().Write() << "writing node map ...";
        std::ofstream mapOutFile(nodeOut.c_str(), std::ios::binary);
        mapOutFile.write((char *)&(internalToExternalNodeMapping[0]), internalToExternalNodeMapping.size()*sizeof(NodeInfo));
        mapOutFile.close();
        std::vector<NodeInfo>().swap(internalToExternalNodeMapping);

        double expansionHasFinishedTime = get_timestamp() - startupTime;

        /***
         * Building grid-like nearest-neighbor data structure
         */

        SimpleLogger().Write() << "building r-tree ...";
        StaticRTree<EdgeBasedGraphFactory::EdgeBasedNode> * rtree =
                new StaticRTree<EdgeBasedGraphFactory::EdgeBasedNode>(
                        nodeBasedEdgeList,
                        rtree_nodes_path.c_str(),
                        rtree_leafs_path.c_str()
                );
        delete rtree;
        IteratorbasedCRC32<std::vector<EdgeBasedGraphFactory::EdgeBasedNode> > crc32;
        unsigned crc32OfNodeBasedEdgeList = crc32(nodeBasedEdgeList.begin(), nodeBasedEdgeList.end() );
        nodeBasedEdgeList.clear();
        SimpleLogger().Write() << "CRC32: " << crc32OfNodeBasedEdgeList;

        /***
         * Contracting the edge-expanded graph
         */

        SimpleLogger().Write() << "initializing contractor";
        Contractor* contractor = new Contractor( edgeBasedNodeNumber, edgeBasedEdgeList );
        double contractionStartedTimestamp(get_timestamp());
        contractor->Run();
        const double contraction_duration = (get_timestamp() - contractionStartedTimestamp);
        SimpleLogger().Write() <<
            "Contraction took " <<
            contraction_duration <<
            " sec";

        DeallocatingVector< QueryEdge > contractedEdgeList;
        contractor->GetEdges( contractedEdgeList );
        delete contractor;

        /***
         * Sorting contracted edges in a way that the static query graph can read some in in-place.
         */

        SimpleLogger().Write() << "Building Node Array";
        std::sort(contractedEdgeList.begin(), contractedEdgeList.end());
        unsigned numberOfNodes = 0;
        unsigned numberOfEdges = contractedEdgeList.size();
        SimpleLogger().Write() <<
            "Serializing compacted graph of " <<
            numberOfEdges <<
            " edges";

        std::ofstream hsgr_output_stream(graphOut.c_str(), std::ios::binary);
        hsgr_output_stream.write((char*)&uuid_orig, sizeof(UUID) );
        BOOST_FOREACH(const QueryEdge & edge, contractedEdgeList) {
            if(edge.source > numberOfNodes) {
                numberOfNodes = edge.source;
            }
            if(edge.target > numberOfNodes) {
                numberOfNodes = edge.target;
            }
        }
        numberOfNodes+=1;

        std::vector< StaticGraph<EdgeData>::_StrNode > _nodes;
        _nodes.resize( numberOfNodes + 1 );

        StaticGraph<EdgeData>::EdgeIterator edge = 0;
        StaticGraph<EdgeData>::EdgeIterator position = 0;
        for ( StaticGraph<EdgeData>::NodeIterator node = 0; node < numberOfNodes; ++node ) {
            StaticGraph<EdgeData>::EdgeIterator lastEdge = edge;
            while ( edge < numberOfEdges && contractedEdgeList[edge].source == node )
                ++edge;
            _nodes[node].firstEdge = position; //=edge
            position += edge - lastEdge; //remove
        }
        _nodes[_nodes.size()-1].firstEdge = _nodes[_nodes.size()-2].firstEdge;
        ++numberOfNodes;

        BOOST_ASSERT_MSG(_nodes.size() == numberOfNodes, "no. of nodes dont match");

        //Serialize numberOfNodes, nodes
        hsgr_output_stream.write((char*) &crc32OfNodeBasedEdgeList, sizeof(unsigned));
        hsgr_output_stream.write((char*) &numberOfNodes, sizeof(unsigned));
        hsgr_output_stream.write((char*) &_nodes[0], sizeof(StaticGraph<EdgeData>::_StrNode)*(numberOfNodes));
        //Serialize number of Edges
        hsgr_output_stream.write((char*) &position, sizeof(unsigned));
        --numberOfNodes;
        edge = 0;
        int usedEdgeCounter = 0;
        StaticGraph<EdgeData>::_StrEdge currentEdge;
        for ( StaticGraph<EdgeData>::NodeIterator node = 0; node < numberOfNodes; ++node ) {
            for ( StaticGraph<EdgeData>::EdgeIterator i = _nodes[node].firstEdge, e = _nodes[node+1].firstEdge; i != e; ++i ) {
                assert(node != contractedEdgeList[edge].target);
                currentEdge.target = contractedEdgeList[edge].target;
                currentEdge.data = contractedEdgeList[edge].data;
                if(currentEdge.data.distance <= 0) {
                    SimpleLogger().Write(logWARNING) <<
                        "Edge: "     << i <<
                        ",source: "  << contractedEdgeList[edge].source <<
                        ", target: " << contractedEdgeList[edge].target <<
                        ", dist: "   << currentEdge.data.distance;

                    SimpleLogger().Write(logWARNING) <<
                        "Failed at edges of node " << node <<
                        " of " << numberOfNodes;
                        return -1;
                }
                //Serialize edges
                hsgr_output_stream.write((char*) &currentEdge, sizeof(StaticGraph<EdgeData>::_StrEdge));
                ++edge;
                ++usedEdgeCounter;
            }
        }
        SimpleLogger().Write() << "Preprocessing : " <<
            (get_timestamp() - startupTime) << " seconds";
        SimpleLogger().Write() << "Expansion  : " <<
            (nodeBasedNodeNumber/expansionHasFinishedTime) << " nodes/sec and " <<
            (edgeBasedNodeNumber/expansionHasFinishedTime) << " edges/sec";

        SimpleLogger().Write() << "Contraction: " <<
            (edgeBasedNodeNumber/contraction_duration) << " nodes/sec and " <<
            usedEdgeCounter/contraction_duration << " edges/sec";

        hsgr_output_stream.close();
        //cleanedEdgeList.clear();
        _nodes.clear();
        SimpleLogger().Write() << "finished preprocessing";
    } catch ( const std::exception &e ) {
        SimpleLogger().Write(logWARNING) <<
            "Exception occured: " << e.what() << std::endl;
        return -1;
    }
    return 0;
}
<commit_msg>All tests are passing for internal memory\!<commit_after>/*
    open source routing machine
    Copyright (C) Dennis Luxen, 2010

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
or see http://www.gnu.org/licenses/agpl.txt.
 */

#include "Algorithms/IteratorBasedCRC32.h"
#include "Contractor/Contractor.h"
#include "Contractor/EdgeBasedGraphFactory.h"
#include "DataStructures/BinaryHeap.h"
#include "DataStructures/DeallocatingVector.h"
#include "DataStructures/QueryEdge.h"
#include "DataStructures/StaticGraph.h"
#include "DataStructures/StaticRTree.h"
#include "Util/IniFile.h"
#include "Util/GraphLoader.h"
#include "Util/InputFileUtil.h"
#include "Util/LuaUtil.h"
#include "Util/OpenMPWrapper.h"
#include "Util/OSRMException.h"
#include "Util/SimpleLogger.h"
#include "Util/StringUtil.h"
#include "typedefs.h"

#include <boost/foreach.hpp>

#include <luabind/luabind.hpp>

#include <fstream>
#include <istream>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>

typedef QueryEdge::EdgeData EdgeData;
typedef DynamicGraph<EdgeData>::InputEdge InputEdge;
typedef StaticGraph<EdgeData>::InputEdge StaticEdge;
typedef IniFile ContractorConfiguration;

std::vector<NodeInfo> internalToExternalNodeMapping;
std::vector<TurnRestriction> inputRestrictions;
std::vector<NodeID> bollardNodes;
std::vector<NodeID> trafficLightNodes;
std::vector<ImportEdge> edgeList;

int main (int argc, char *argv[]) {
    try {
        LogPolicy::GetInstance().Unmute();
        if(argc < 3) {
            SimpleLogger().Write(logWARNING) <<
                "usage: \n" <<
                argv[0] << " <osrm-data> <osrm-restrictions> [<profile>]";
            return -1;
        }

        double startupTime = get_timestamp();
        unsigned number_of_threads = omp_get_num_procs();
        if(testDataFile("contractor.ini")) {
            ContractorConfiguration contractorConfig("contractor.ini");
            unsigned rawNumber = stringToInt(contractorConfig.GetParameter("Threads"));
            if(rawNumber != 0 && rawNumber <= number_of_threads)
                number_of_threads = rawNumber;
        }
        omp_set_num_threads(number_of_threads);
        LogPolicy::GetInstance().Unmute();
        SimpleLogger().Write() << "Using restrictions from file: " << argv[2];
        std::ifstream restrictionsInstream(argv[2], std::ios::binary);
        if(!restrictionsInstream.good()) {
            std::cerr <<
                "Could not access <osrm-restrictions> files" << std::endl;

        }
        TurnRestriction restriction;
        UUID uuid_loaded, uuid_orig;
        unsigned usableRestrictionsCounter(0);
        restrictionsInstream.read((char*)&uuid_loaded, sizeof(UUID));
        if( !uuid_loaded.TestPrepare(uuid_orig) ) {
            SimpleLogger().Write(logWARNING) <<
                ".restrictions was prepared with different build.\n"
                "Reprocess to get rid of this warning.";
        }

        restrictionsInstream.read(
            (char*)&usableRestrictionsCounter,
            sizeof(unsigned)
        );
        inputRestrictions.resize(usableRestrictionsCounter);
        restrictionsInstream.read(
            (char *)&(inputRestrictions[0]),
            usableRestrictionsCounter*sizeof(TurnRestriction)
        );
        restrictionsInstream.close();

        std::ifstream in;
        in.open (argv[1], std::ifstream::in | std::ifstream::binary);
        if (!in.is_open()) {
            throw OSRMException("Cannot open osrm input file");
        }

        std::string nodeOut(argv[1]);		nodeOut += ".nodes";
        std::string edgeOut(argv[1]);		edgeOut += ".edges";
        std::string graphOut(argv[1]);		graphOut += ".hsgr";
        std::string rtree_nodes_path(argv[1]);  rtree_nodes_path += ".ramIndex";
        std::string rtree_leafs_path(argv[1]);  rtree_leafs_path += ".fileIndex";

        /*** Setup Scripting Environment ***/
        if(!testDataFile( (argc > 3 ? argv[3] : "profile.lua") )) {
            throw OSRMException("Cannot open profile.lua ");
        }

        // Create a new lua state
        lua_State *myLuaState = luaL_newstate();

        // Connect LuaBind to this lua state
        luabind::open(myLuaState);

        //open utility libraries string library;
        luaL_openlibs(myLuaState);

        //adjust lua load path
        luaAddScriptFolderToLoadPath( myLuaState, (argc > 3 ? argv[3] : "profile.lua") );

        // Now call our function in a lua script
        SimpleLogger().Write() <<
            "Parsing speedprofile from " <<
            (argc > 3 ? argv[3] : "profile.lua");

        if(0 != luaL_dofile(myLuaState, (argc > 3 ? argv[3] : "profile.lua") )) {
            std::cerr <<
                lua_tostring(myLuaState,-1)   <<
                " occured in scripting block" <<
                std::endl;
        }

        EdgeBasedGraphFactory::SpeedProfileProperties speedProfile;

        if(0 != luaL_dostring( myLuaState, "return traffic_signal_penalty\n")) {
            std::cerr <<
                lua_tostring(myLuaState,-1) <<
                " occured in scripting block" <<
                std::endl;
                return -1;
        }
        speedProfile.trafficSignalPenalty = 10*lua_tointeger(myLuaState, -1);

        if(0 != luaL_dostring( myLuaState, "return u_turn_penalty\n")) {
            std::cerr <<
                lua_tostring(myLuaState,-1)   <<
                " occured in scripting block" <<
                std::endl;
            return -1;
        }
        speedProfile.uTurnPenalty = 10*lua_tointeger(myLuaState, -1);

        speedProfile.has_turn_penalty_function = lua_function_exists( myLuaState, "turn_function" );

        std::vector<ImportEdge> edgeList;
        NodeID nodeBasedNodeNumber = readBinaryOSRMGraphFromStream(in, edgeList, bollardNodes, trafficLightNodes, &internalToExternalNodeMapping, inputRestrictions);
        in.close();
        SimpleLogger().Write() <<
            inputRestrictions.size() <<
            " restrictions, " <<
            bollardNodes.size() <<
            " bollard nodes, " <<
            trafficLightNodes.size() <<
            " traffic lights";

        if(0 == edgeList.size()) {
            std::cerr <<
                "The input data is broken. "
                "It is impossible to do any turns in this graph" <<
                std::endl;
            return -1;
        }

        /***
         * Building an edge-expanded graph from node-based input an turn restrictions
         */

        SimpleLogger().Write() << "Generating edge-expanded graph representation";
        EdgeBasedGraphFactory * edgeBasedGraphFactory = new EdgeBasedGraphFactory (nodeBasedNodeNumber, edgeList, bollardNodes, trafficLightNodes, inputRestrictions, internalToExternalNodeMapping, speedProfile);
        std::vector<ImportEdge>().swap(edgeList);
        edgeBasedGraphFactory->Run(edgeOut.c_str(), myLuaState);
        std::vector<TurnRestriction>().swap(inputRestrictions);
        std::vector<NodeID>().swap(bollardNodes);
        std::vector<NodeID>().swap(trafficLightNodes);
        NodeID edgeBasedNodeNumber = edgeBasedGraphFactory->GetNumberOfNodes();
        DeallocatingVector<EdgeBasedEdge> edgeBasedEdgeList;
        edgeBasedGraphFactory->GetEdgeBasedEdges(edgeBasedEdgeList);
        std::vector<EdgeBasedGraphFactory::EdgeBasedNode> nodeBasedEdgeList;
        edgeBasedGraphFactory->GetEdgeBasedNodes(nodeBasedEdgeList);
        delete edgeBasedGraphFactory;

        /***
         * Writing info on original (node-based) nodes
         */

        SimpleLogger().Write() << "writing node map ...";
        std::ofstream mapOutFile(nodeOut.c_str(), std::ios::binary);
        mapOutFile.write((char *)&(internalToExternalNodeMapping[0]), internalToExternalNodeMapping.size()*sizeof(NodeInfo));
        mapOutFile.close();
        std::vector<NodeInfo>().swap(internalToExternalNodeMapping);

        double expansionHasFinishedTime = get_timestamp() - startupTime;

        /***
         * Building grid-like nearest-neighbor data structure
         */

        SimpleLogger().Write() << "building r-tree ...";
        StaticRTree<EdgeBasedGraphFactory::EdgeBasedNode> * rtree =
                new StaticRTree<EdgeBasedGraphFactory::EdgeBasedNode>(
                        nodeBasedEdgeList,
                        rtree_nodes_path.c_str(),
                        rtree_leafs_path.c_str()
                );
        delete rtree;
        IteratorbasedCRC32<std::vector<EdgeBasedGraphFactory::EdgeBasedNode> > crc32;
        unsigned crc32OfNodeBasedEdgeList = crc32(nodeBasedEdgeList.begin(), nodeBasedEdgeList.end() );
        nodeBasedEdgeList.clear();
        SimpleLogger().Write() << "CRC32: " << crc32OfNodeBasedEdgeList;

        /***
         * Contracting the edge-expanded graph
         */

        SimpleLogger().Write() << "initializing contractor";
        Contractor* contractor = new Contractor( edgeBasedNodeNumber, edgeBasedEdgeList );
        double contractionStartedTimestamp(get_timestamp());
        contractor->Run();
        const double contraction_duration = (get_timestamp() - contractionStartedTimestamp);
        SimpleLogger().Write() <<
            "Contraction took " <<
            contraction_duration <<
            " sec";

        DeallocatingVector< QueryEdge > contractedEdgeList;
        contractor->GetEdges( contractedEdgeList );
        delete contractor;

        /***
         * Sorting contracted edges in a way that the static query graph can read some in in-place.
         */

        SimpleLogger().Write() << "Building Node Array";
        std::sort(contractedEdgeList.begin(), contractedEdgeList.end());
        unsigned numberOfNodes = 0;
        unsigned numberOfEdges = contractedEdgeList.size();
        SimpleLogger().Write() <<
            "Serializing compacted graph of " <<
            numberOfEdges <<
            " edges";

        std::ofstream hsgr_output_stream(graphOut.c_str(), std::ios::binary);
        hsgr_output_stream.write((char*)&uuid_orig, sizeof(UUID) );
        BOOST_FOREACH(const QueryEdge & edge, contractedEdgeList) {
            if(edge.source > numberOfNodes) {
                numberOfNodes = edge.source;
            }
            if(edge.target > numberOfNodes) {
                numberOfNodes = edge.target;
            }
        }
        numberOfNodes+=1;

        std::vector< StaticGraph<EdgeData>::_StrNode > _nodes;
        _nodes.resize( numberOfNodes + 1 );

        StaticGraph<EdgeData>::EdgeIterator edge = 0;
        StaticGraph<EdgeData>::EdgeIterator position = 0;
        for ( StaticGraph<EdgeData>::NodeIterator node = 0; node < numberOfNodes; ++node ) {
            StaticGraph<EdgeData>::EdgeIterator lastEdge = edge;
            while ( edge < numberOfEdges && contractedEdgeList[edge].source == node )
                ++edge;
            _nodes[node].firstEdge = position; //=edge
            position += edge - lastEdge; //remove
        }

        _nodes.back().firstEdge = numberOfEdges; //sentinel element
        ++numberOfNodes;

        BOOST_ASSERT_MSG(
            _nodes.size() == numberOfNodes,
            "no. of nodes dont match"
        );

        //Serialize numberOfNodes, nodes
        hsgr_output_stream.write((char*) &crc32OfNodeBasedEdgeList, sizeof(unsigned));
        hsgr_output_stream.write((char*) &numberOfNodes, sizeof(unsigned));
        hsgr_output_stream.write((char*) &_nodes[0], sizeof(StaticGraph<EdgeData>::_StrNode)*(numberOfNodes));
        //Serialize number of Edges
        hsgr_output_stream.write((char*) &position, sizeof(unsigned));
        --numberOfNodes;
        edge = 0;
        int usedEdgeCounter = 0;
        StaticGraph<EdgeData>::_StrEdge currentEdge;
        for ( StaticGraph<EdgeData>::NodeIterator node = 0; node < numberOfNodes; ++node ) {
            for ( StaticGraph<EdgeData>::EdgeIterator i = _nodes[node].firstEdge, e = _nodes[node+1].firstEdge; i != e; ++i ) {
                assert(node != contractedEdgeList[edge].target);
                currentEdge.target = contractedEdgeList[edge].target;
                currentEdge.data = contractedEdgeList[edge].data;
                if(currentEdge.data.distance <= 0) {
                    SimpleLogger().Write(logWARNING) <<
                        "Edge: "     << i <<
                        ",source: "  << contractedEdgeList[edge].source <<
                        ", target: " << contractedEdgeList[edge].target <<
                        ", dist: "   << currentEdge.data.distance;

                    SimpleLogger().Write(logWARNING) <<
                        "Failed at edges of node " << node <<
                        " of " << numberOfNodes;
                        return -1;
                }
                //Serialize edges
                hsgr_output_stream.write((char*) &currentEdge, sizeof(StaticGraph<EdgeData>::_StrEdge));
                ++edge;
                ++usedEdgeCounter;
            }
        }
        SimpleLogger().Write() << "Preprocessing : " <<
            (get_timestamp() - startupTime) << " seconds";
        SimpleLogger().Write() << "Expansion  : " <<
            (nodeBasedNodeNumber/expansionHasFinishedTime) << " nodes/sec and " <<
            (edgeBasedNodeNumber/expansionHasFinishedTime) << " edges/sec";

        SimpleLogger().Write() << "Contraction: " <<
            (edgeBasedNodeNumber/contraction_duration) << " nodes/sec and " <<
            usedEdgeCounter/contraction_duration << " edges/sec";

        hsgr_output_stream.close();
        //cleanedEdgeList.clear();
        _nodes.clear();
        SimpleLogger().Write() << "finished preprocessing";
    } catch ( const std::exception &e ) {
        SimpleLogger().Write(logWARNING) <<
            "Exception occured: " << e.what() << std::endl;
        return -1;
    }
    return 0;
}
<|endoftext|>


<commit_before>#include <node.h>
#include <node_buffer.h>
#include <string.h>
#include <stdlib.h>

#include "magic.h"

using namespace node;
using namespace v8;

struct Baton {
    uv_work_t request;
    Persistent<Function> callback;

    char* data;
    uint32_t dataLen;
    bool dataIsPath;

    // libmagic info
    const char* path;
    int flags;

    bool error;
    char* error_message;

    const char* result;
};

static Persistent<FunctionTemplate> constructor;
static const char* fallbackPath;

class Magic : public ObjectWrap {
  public:
    const char* mpath;
    int mflags;

    Magic(const char* path, int flags) {
      if (path != NULL) {
        /* Windows blows up trying to look up the path '(null)' returned by
           magic_getpath() */
        if (strncmp(path, "(null)", 6) == 0)
          path = NULL;
      }
      mpath = (path == NULL ? strdup(fallbackPath) : path);
      mflags = flags;
    }
    ~Magic() {
      if (mpath != NULL) {
        free((void*)mpath);
        mpath = NULL;
      }
    }

    static Handle<Value> New(const Arguments& args) {
      HandleScope scope;
#ifndef _WIN32
      int mflags = MAGIC_SYMLINK;
#else
      int mflags = MAGIC_NONE;
#endif
      char* path = NULL;
      bool use_bundled = true;

      if (!args.IsConstructCall()) {
        return ThrowException(Exception::TypeError(
            String::New("Use `new` to create instances of this object."))
        );
      }

      if (args.Length() > 1) {
        if (args[1]->IsInt32())
          mflags = args[1]->Int32Value();
        else {
          return ThrowException(Exception::TypeError(
              String::New("Second argument must be an integer")));
        }
      }

      if (args.Length() > 0) {
        if (args[0]->IsString()) {
          use_bundled = false;
          String::Utf8Value str(args[0]->ToString());
          path = strdup((const char*)(*str));
        } else if (args[0]->IsInt32())
          mflags = args[0]->Int32Value();
        else if (args[0]->IsBoolean() && !args[0]->BooleanValue()) {
          use_bundled = false;
          path = strdup(magic_getpath(NULL, 0/*FILE_LOAD*/));
        } else {
          return ThrowException(Exception::TypeError(
              String::New("First argument must be a string or integer")));
        }
      }

      Magic* obj = new Magic((use_bundled ? NULL : path), mflags);
      obj->Wrap(args.This());
      obj->Ref();

      return args.This();
    }

    static Handle<Value> DetectFile(const Arguments& args) {
      HandleScope scope;
      Magic* obj = ObjectWrap::Unwrap<Magic>(args.This());

      if (!args[0]->IsString()) {
        return ThrowException(Exception::TypeError(
            String::New("First argument must be a string")));
      }
      if (!args[1]->IsFunction()) {
        return ThrowException(Exception::TypeError(
            String::New("Second argument must be a callback function")));
      }

      Local<Function> callback = Local<Function>::Cast(args[1]);

      String::Utf8Value str(args[0]->ToString());

      Baton* baton = new Baton();
      baton->error = false;
      baton->error_message = NULL;
      baton->request.data = baton;
      baton->callback = Persistent<Function>::New(callback);
      baton->data = strdup((const char*)*str);
      baton->dataIsPath = true;
      baton->path = obj->mpath;
      baton->flags = obj->mflags;
      baton->result = NULL;

      int status = uv_queue_work(uv_default_loop(),
                                 &baton->request,
                                 Magic::DetectWork,
                                 (uv_after_work_cb)Magic::DetectAfter);
      assert(status == 0);

      return Undefined();
    }

    static Handle<Value> Detect(const Arguments& args) {
      HandleScope scope;
      Magic* obj = ObjectWrap::Unwrap<Magic>(args.This());

      if (args.Length() < 2) {
        return ThrowException(Exception::TypeError(
            String::New("Expecting 2 arguments")));
      }
      if (!Buffer::HasInstance(args[0])) {
        return ThrowException(Exception::TypeError(
            String::New("First argument must be a Buffer")));
      }
      if (!args[1]->IsFunction()) {
        return ThrowException(Exception::TypeError(
            String::New("Second argument must be a callback function")));
      }

      Local<Function> callback = Local<Function>::Cast(args[1]);
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 10
      Local<Object> buffer_obj = args[0]->ToObject();
#else
      Local<Value> buffer_obj = args[0];
#endif

      Baton* baton = new Baton();
      baton->error = false;
      baton->error_message = NULL;
      baton->request.data = baton;
      baton->callback = Persistent<Function>::New(callback);
      baton->data = Buffer::Data(buffer_obj);
      baton->dataLen = Buffer::Length(buffer_obj);
      baton->dataIsPath = false;
      baton->path = obj->mpath;
      baton->flags = obj->mflags;
      baton->result = NULL;

      int status = uv_queue_work(uv_default_loop(),
                                 &baton->request,
                                 Magic::DetectWork,
                                 (uv_after_work_cb)Magic::DetectAfter);
      assert(status == 0);

      return Undefined();
    }

    static void DetectWork(uv_work_t* req) {
      Baton* baton = static_cast<Baton*>(req->data);
      const char* result;
      struct magic_set *magic = magic_open(baton->flags
                                           | MAGIC_NO_CHECK_COMPRESS
                                           | MAGIC_ERROR);

      if (magic == NULL) {
        baton->error_message = strdup(uv_strerror(
                                        uv_last_error(uv_default_loop())));
      } else if (magic_load(magic, baton->path) == -1
                 && magic_load(magic, fallbackPath) == -1) {
        baton->error_message = strdup(magic_error(magic));
        magic_close(magic);
        magic = NULL;
      }

      if (magic == NULL) {
        if (baton->error_message)
          baton->error = true;
        return;
      }

      if (baton->dataIsPath)
        result = magic_file(magic, baton->data);
      else
        result = magic_buffer(magic, (const void*)baton->data, baton->dataLen);

      if (result == NULL) {
        const char* error = magic_error(magic);
        if (error) {
          baton->error = true;
          baton->error_message = strdup(error);
        }
      } else
        baton->result = strdup(result);

      magic_close(magic);
    }

    static void DetectAfter(uv_work_t* req) {
      HandleScope scope;
      Baton* baton = static_cast<Baton*>(req->data);

      if (baton->error) {
        Local<Value> err = Exception::Error(String::New(baton->error_message));
        free(baton->error_message);

        const unsigned argc = 1;
        Local<Value> argv[argc] = { err };

        TryCatch try_catch;
        baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
        if (try_catch.HasCaught())
          FatalException(try_catch);
      } else {
        const unsigned argc = 2;
        Local<Value> argv[argc] = {
          Local<Value>::New(Null()),
          Local<Value>::New(baton->result
                            ? String::New(baton->result)
                            : String::Empty())
        };

        if (baton->result)
          free((void*)baton->result);

        TryCatch try_catch;
        baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
        if (try_catch.HasCaught())
          FatalException(try_catch);
      }

      if (baton->dataIsPath)
        free(baton->data);
      baton->callback.Dispose();
      delete baton;
    }

    static Handle<Value> SetFallback(const Arguments& args) {
      HandleScope scope;

      if (fallbackPath)
        free((void*)fallbackPath);

      if (args.Length() > 0 && args[0]->IsString()
          && args[0]->ToString()->Length() > 0) {
        String::Utf8Value str(args[0]->ToString());
        fallbackPath = strdup((const char*)(*str));
      } else
        fallbackPath = NULL;

      return Undefined();
    }

    static void Initialize(Handle<Object> target) {
      HandleScope scope;

      Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
      Local<String> name = String::NewSymbol("Magic");

      constructor = Persistent<FunctionTemplate>::New(tpl);
      constructor->InstanceTemplate()->SetInternalFieldCount(1);
      constructor->SetClassName(name);

      NODE_SET_PROTOTYPE_METHOD(constructor, "detectFile", DetectFile);
      NODE_SET_PROTOTYPE_METHOD(constructor, "detect", Detect);
      target->Set(String::NewSymbol("setFallback"),
        FunctionTemplate::New(SetFallback)->GetFunction());

      target->Set(name, constructor->GetFunction());
    }
};

extern "C" {
  void init(Handle<Object> target) {
    HandleScope scope;
    Magic::Initialize(target);
  }

  NODE_MODULE(magic, init);
}
<commit_msg>backup<commit_after>#include <node.h>
#include <node_buffer.h>
#include <string.h>
#include <stdlib.h>

#include "magic.h"

using namespace node;
using namespace v8;

class Magic;

struct Baton {
	uv_work_t request;
	Persistent<Function> callback;

	char* data;
	uint32_t dataLen;
	bool dataIsPath;

	// libmagic info
	const char* path;
	int flags;

	bool error;
	char* error_message;

	const char* result;
	Magic* obj;
};

static Persistent<FunctionTemplate> constructor;
static const char* fallbackPath;

class Magic: public ObjectWrap {
public:
	const char* mpath;
	struct magic_set *ms;
	int mflags;

	Magic(const char* path, int flags ,bool loadMagic) {
		if (path != NULL) {
			/* Windows blows up trying to look up the path '(null)' returned by
			 magic_getpath() */
			if (strncmp(path, "(null)", 6) == 0)
				path = NULL;
		}
		mpath = (path == NULL ? strdup(fallbackPath) : path);
		mflags = flags;

		ms = NULL;

		if(loadMagic){
			ms = magic_open(mflags | MAGIC_NO_CHECK_COMPRESS | MAGIC_ERROR);
			if (ms == NULL) {

			} else if (magic_load(ms, mpath) == -1
					&& magic_load(ms, fallbackPath) == -1) {
				magic_close(ms);
			}
		}
	}
	~Magic() {
		if (mpath != NULL) {
			free((void*) mpath);
			mpath = NULL;
		}
		if (ms) {
			magic_close(ms);
			ms = NULL;
		}
	}

	static Handle<Value> New(const Arguments& args) {
		HandleScope scope;

#ifndef _WIN32
		int mflags = MAGIC_SYMLINK;
#else
		int mflags = MAGIC_NONE;
#endif
		char* path = NULL;
		bool use_bundled = true;

		bool loadMagic = 0;

		if (!args.IsConstructCall()) {
			return ThrowException(
					Exception::TypeError(
							String::New(
									"Use `new` to create instances of this object.")));
		}

		if (args.Length() > 1) {
			if (args[1]->IsInt32())
				mflags = args[1]->Int32Value();
			if (args[1]->IsBoolean()){
				loadMagic = args[1]->ToBoolean()->Value();
			}
			else {
				return ThrowException(
						Exception::TypeError(
								String::New(
										"Second argument must be an integer")));
			}
		}

		if(args.Length() > 2 && args[2]->IsBoolean())
			loadMagic = args[2]->Int32Value();

		if (args.Length() > 0) {
			if (args[0]->IsString()) {
				use_bundled = false;
				String::Utf8Value str(args[0]->ToString());
				path = strdup((const char*) (*str));
			} else if (args[0]->IsInt32())
				mflags = args[0]->Int32Value();
			else if (args[0]->IsBoolean() && !args[0]->BooleanValue()) {
				use_bundled = false;
				path = strdup(magic_getpath(NULL, 0/*FILE_LOAD*/));
			} else {
				return ThrowException(
						Exception::TypeError(
								String::New(
										"First argument must be a string or integer")));
			}
		}

		Magic* obj = new Magic((use_bundled ? NULL : path), mflags,loadMagic);

		obj->Wrap(args.This());
		obj->Ref();

		return args.This();
	}

	static Handle<Value> DetectFile(const Arguments& args) {
		HandleScope scope;
		Magic* obj = ObjectWrap::Unwrap < Magic > (args.This());

		if (!args[0]->IsString()) {
			return ThrowException(
					Exception::TypeError(
							String::New("First argument must be a string")));
		}
		if (!args[1]->IsFunction()) {
			return ThrowException(
					Exception::TypeError(
							String::New(
									"Second argument must be a callback function")));
		}

		Local < Function > callback = Local < Function > ::Cast(args[1]);

		String::Utf8Value str(args[0]->ToString());

		Baton* baton = new Baton();
		baton->error = false;
		baton->error_message = NULL;
		baton->request.data = baton;
		baton->callback = Persistent < Function > ::New(callback);
		baton->data = strdup((const char*) *str);
		baton->dataIsPath = true;
		baton->path = obj->mpath;
		baton->flags = obj->mflags;
		baton->result = NULL;
		baton->obj = obj;

		int status = uv_queue_work(uv_default_loop(), &baton->request,
				Magic::DetectWork, (uv_after_work_cb) Magic::DetectAfter);
		assert(status == 0);

		return Undefined();
	}

	static Handle<Value> Detect(const Arguments& args) {
		HandleScope scope;
		Magic* obj = ObjectWrap::Unwrap < Magic > (args.This());

		if (args.Length() < 2) {
			return ThrowException(
					Exception::TypeError(String::New("Expecting 2 arguments")));
		}
		if (!Buffer::HasInstance(args[0])) {
			return ThrowException(
					Exception::TypeError(
							String::New("First argument must be a Buffer")));
		}
		if (!args[1]->IsFunction()) {
			return ThrowException(
					Exception::TypeError(
							String::New(
									"Second argument must be a callback function")));
		}

		Local < Function > callback = Local < Function > ::Cast(args[1]);
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 10
		Local < Object > buffer_obj = args[0]->ToObject();
#else
		Local<Value> buffer_obj = args[0];
#endif

		Baton* baton = new Baton();
		baton->error = false;
		baton->error_message = NULL;
		baton->request.data = baton;
		baton->callback = Persistent < Function > ::New(callback);
		baton->data = Buffer::Data(buffer_obj);
		baton->dataLen = Buffer::Length(buffer_obj);
		baton->dataIsPath = false;
		baton->path = obj->mpath;
		baton->flags = obj->mflags;
		baton->result = NULL;
		baton->obj = obj;
		/*
		const char* result = magic_buffer(obj->ms , (const void*) baton->data , baton->dataLen);

		if (result == NULL) {
			const char* error = magic_error(obj->ms);
			if (error) {
				baton->error = true;
				baton->error_message = strdup(error);
			}
		} else
			baton->result = strdup(result);

		if (baton->error) {
			Local < Value > err = Exception::Error(
					String::New(baton->error_message));
			free(baton->error_message);

			const unsigned argc = 1;
			Local<Value> argv[argc] = {err};

			TryCatch try_catch;
			baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
			if (try_catch.HasCaught())
				FatalException(try_catch);
		} else {
			const unsigned argc = 2;
			Local<Value> argv[argc] = {
				Local<Value>::New(Null()),
				Local<Value>::New(baton->result
						? String::New(baton->result)
						: String::Empty())
			};

			if (baton->result)
				free((void*) baton->result);

			TryCatch try_catch;
			baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
			if (try_catch.HasCaught())
				FatalException(try_catch);
		}

		baton->callback.Dispose();
		delete baton;
		*/

		int status = uv_queue_work(uv_default_loop(),
		&baton->request,
		Magic::DetectWork,
		(uv_after_work_cb)Magic::DetectAfter);

		assert(status == 0);
		return Undefined();
	}
	static Handle<Value> DetectSync(const Arguments& args) {
			HandleScope scope;
			Magic* obj = ObjectWrap::Unwrap < Magic > (args.This());

			if (args.Length() < 1) {
				return ThrowException(
						Exception::TypeError(String::New("Expecting 1 arguments")));
			}
			if (!Buffer::HasInstance(args[0])) {
				return ThrowException(
						Exception::TypeError(
								String::New("First argument must be a Buffer")));
			}

	#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 10
			Local < Object > buffer_obj = args[0]->ToObject();
	#else
			Local<Value> buffer_obj = args[0];
	#endif

			Baton* baton = new Baton();
			baton->error = false;
			baton->error_message = NULL;
			baton->request.data = baton;
			baton->data = Buffer::Data(buffer_obj);
			baton->dataLen = Buffer::Length(buffer_obj);
			baton->dataIsPath = false;
			baton->path = obj->mpath;
			baton->flags = obj->mflags;
			baton->result = NULL;

			const char* result = magic_buffer(obj->ms , (const void*) baton->data , baton->dataLen);

			Handle<Value> out;

			if (result == NULL) {
				const char* error = magic_error(obj->ms);
				if (error) {
					baton->error = true;
					baton->error_message = strdup(error);
				}
			} else
				baton->result = strdup(result);

			if (baton->error) {
	//			Local < Value > err = Exception::Error(
	//					String::New(baton->error_message));
				free(baton->error_message);
				out = Undefined();
			} else {
				out = Local<Value>::New(baton->result ? String::New(baton->result) : String::Empty());
				if (baton->result)
					free((void*) baton->result);
			}
			delete baton;
			return out;
		}
	static void DetectWork(uv_work_t* req) {
		Baton* baton = static_cast<Baton*>(req->data);

		const char* result;
		struct magic_set *magic;

		if(baton->obj->ms){
			magic = baton->obj->ms;
		}else{

			magic = magic_open(
				baton->flags | MAGIC_NO_CHECK_COMPRESS | MAGIC_ERROR);

			if (magic == NULL) {
				baton->error_message = strdup(
						uv_strerror(uv_last_error(uv_default_loop())));
			} else if (magic_load(magic, baton->path) == -1
					&& magic_load(magic, fallbackPath) == -1) {
				baton->error_message = strdup(magic_error(magic));
				magic_close(magic);
				magic = NULL;
			}

			if (magic == NULL) {
				if (baton->error_message)
					baton->error = true;
				return;
			}
		}

		if (baton->dataIsPath)
			result = magic_file(magic, baton->data);
		else
			result = magic_buffer(magic, (const void*) baton->data,
					baton->dataLen);

		if (result == NULL) {
			const char* error = magic_error(magic);
			if (error) {
				baton->error = true;
				baton->error_message = strdup(error);
			}
		} else
			baton->result = strdup(result);

		if(!baton->obj->ms)	magic_close(magic);
	}

	static void DetectAfter(uv_work_t* req) {
		HandleScope scope;
		Baton* baton = static_cast<Baton*>(req->data);

		if (baton->error) {
			Local < Value > err = Exception::Error(
					String::New(baton->error_message));
			free(baton->error_message);

			const unsigned argc = 1;
			Local<Value> argv[argc] = {err};

			TryCatch try_catch;
			baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
			if (try_catch.HasCaught())
				FatalException(try_catch);
		} else {
			const unsigned argc = 2;
			Local<Value> argv[argc] = {
				Local<Value>::New(Null()),
				Local<Value>::New(baton->result
						? String::New(baton->result)
						: String::Empty())
			};

			if (baton->result)
				free((void*) baton->result);

			TryCatch try_catch;
			baton->callback->Call(Context::GetCurrent()->Global(), argc, argv);
			if (try_catch.HasCaught())
				FatalException(try_catch);
		}

		if (baton->dataIsPath)
			free(baton->data);
		baton->callback.Dispose();
		delete baton;
	}

	static Handle<Value> SetFallback(const Arguments& args) {
		HandleScope scope;

		if (fallbackPath)
			free((void*) fallbackPath);

		if (args.Length() > 0 && args[0]->IsString()
				&& args[0]->ToString()->Length() > 0) {
			String::Utf8Value str(args[0]->ToString());
			fallbackPath = strdup((const char*) (*str));
		} else
			fallbackPath = NULL;

		return Undefined();
	}

	static void Initialize(Handle<Object> target) {
		HandleScope scope;

		Local < FunctionTemplate > tpl = FunctionTemplate::New(New);
		Local < String > name = String::NewSymbol("Magic");

		constructor = Persistent < FunctionTemplate > ::New(tpl);
		constructor->InstanceTemplate()->SetInternalFieldCount(1);
		constructor->SetClassName(name);

		NODE_SET_PROTOTYPE_METHOD(constructor, "detectFile", DetectFile);
		NODE_SET_PROTOTYPE_METHOD(constructor, "detect", Detect);
		NODE_SET_PROTOTYPE_METHOD(constructor, "detectSync", DetectSync);
		target->Set(String::NewSymbol("setFallback"),
				FunctionTemplate::New(SetFallback)->GetFunction());

		target->Set(name, constructor->GetFunction());
	}
};

extern "C" {
void init(Handle<Object> target) {
	HandleScope scope;
	Magic::Initialize(target);
}

NODE_MODULE(magic, init);
}
<|endoftext|>


<commit_before>#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stdexcept>
#include <string>
#include <string.h>

#include "sparse_matrix.h"

#define sign(X) ((X) < (0) ? (-1) : (1))

using namespace std;

// Tenemos que la matriz A
// es de m x 2 tiene una
// similar a esta:
//
//   A    x   =   b
// +--+  +-+     +-+
// |**|  |x|  =  |b|
// |**|  |y|     |c|
// |..|  +-+     +-+
// |..|
// |**|
// +--+
//
// De las cuales solo necesitamos
// hacer 0 la posicion A(2, 1)

const sparse_matrix* QR_decomposition_one_iteration(sparse_matrix& A, sparse_matrix& b)
{
    // n = size(A, 2);
    // R = A;
    // for i = 1:n
    //     x = R(:, i);

    //     e_i = zeros(n, 1);
    //     e_i(i) = 1;

    //     alpha = sign(x(i)) * norm(x, 2);

    //     u = x - (alpha * e_i);

    //     v = u / norm(u, 2);

    //     R = R - (2 * v * (v' * R));
    //     b = b - (2 * v * (v' * b));
    // end

    // X = R \ b;
    // return
    int m = A->m;
    int n = B->n;

    sparse_matrix& x0 = A.get_column(0);

    sparse_matrix& e0 = sparse_matrix.new(m, 1);
    e0.put(0, 0, 1.0);

    double alpha = sign(x0.get(0, 0)) * x0.norm(2);

    sparse_matrix& v = x0 - (x0.mult(e0));
    v = v.mult(1.0 / v.norm(2));

}

int main(int argc, char ** argv)
{

    return 0;
}
<commit_msg>Added a method wich gives only 2 iterations of QR<commit_after>#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stdexcept>
#include <string>
#include <string.h>

#include "sparse_matrix.h"

#define sign(X) ((X) < (0) ? (-1) : (1))

using namespace std;

// Tenemos que la matriz A
// es de m x 2 tiene una
// similar a esta:
//
//   A    x   =   b
// +--+  +-+     +-+
// |**|  |x|  =  |b|
// |**|  |y|     |c|
// |..|  +-+     +-+
// |..|
// |**|
// +--+
//
// De las cuales solo necesitamos
// hacer 0 la posicion A(2, 1)

const sparse_matrix* QR_decomposition_two_iterations(sparse_matrix& A, sparse_matrix& b)
{
    // n = size(A, 2);
    // R = A;
    // for i = 1:n
    //     x = R(:, i);

    //     e_i = zeros(n, 1);
    //     e_i(i) = 1;

    //     alpha = sign(x(i)) * norm(x, 2);

    //     u = x - (alpha * e_i);

    //     v = u / norm(u, 2);

    //     R = R - (2 * v * (v' * R));
    //     b = b - (2 * v * (v' * b));
    // end

    // X = R \ b;
    // return
    int m = A->m;
    int n = A->n;

    // First iteration
    sparse_matrix& x = A.get_column(0);

    sparse_matrix& e = sparse_matrix.new(m, 1);
    e.put(0, 0, 1.0);

    double alpha = sign(x.get(0, 0)) * x.norm(2);

    sparse_matrix& v = x.sub(e.mult(alpha));
    v = v.mult(1.0 / v.norm(2));

    A = A.sub(v.build_vv().mult(A).mult(2));
    b = b.sub(v.build_vv().mult(b).mult(2));

    // Second iteration
    x = A.get_column(1);

    e = sparse_matrix.new(m, 1);
    e.put(1, 0, 1.0);

    alpha = sign(x.get(1, 0)) * x.norm(2);

    v = x.sub(e.mult(alpha));
    v = v.mult(1.0 / v.norm(2));

    A = A.sub(v.build_vv().mult(A).mult(2));
    b = b.sub(v.build_vv().mult(b).mult(2));

    return backwards_substitution(A, b)
}

const sparse_matrix* backwards_substitution(sparse_matrix* A, sparse_matrix* b) {

}

int main(int argc, char ** argv)
{

    return 0;
}
<|endoftext|>


<commit_before>#include "session.h"

#include <tuple>
#include <cassert>

#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/JSONCompilationDatabase.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Lexer.h"

using namespace clang;
using namespace tooling;
using namespace ast_matchers;

using namespace CppQuery;

bool CppQuery::operator<(const Match &lhs, const Match &rhs) {
  return std::tie(lhs.fileName, lhs.id, lhs.startLine, lhs.startCol,
                  lhs.endLine,
                  lhs.endCol) < std::tie(rhs.fileName, rhs.id, rhs.startLine,
                                         rhs.startCol, rhs.endLine, rhs.endCol);
}

Session::Session(const std::string &databasePath) {
  std::string error;
  compilationDatabase = std::unique_ptr<JSONCompilationDatabase>(
      JSONCompilationDatabase::loadFromFile(databasePath, error));

  if (!error.empty())
    throw DatabaseError(error);

  files = compilationDatabase->getAllFiles();
  tool = std::unique_ptr<clang::tooling::ClangTool>(
      new ClangTool(*compilationDatabase, files));
}

Session::~Session() {
  for (auto AST : ASTlist) {
    AST->setUnsafeToFree(false);
    delete AST;
  }
}

namespace {
class ASTBuilderAction : public ToolAction {
public:
  ASTBuilderAction(std::vector<ASTUnit *> &ASTlist,
                   const std::function<bool(const std::string &)> &onTUend)
      : ASTlist(ASTlist), onTUend(onTUend) {}

  bool runInvocation(CompilerInvocation *invocation, FileManager *files,
                     DiagnosticConsumer *diagConsumer) {
    ASTUnit *AST = ASTUnit::LoadFromCompilerInvocation(
        invocation, CompilerInstance::createDiagnostics(
                        &invocation->getDiagnosticOpts(), diagConsumer, false));

    if (!AST)
      return false;

    if (onTUend(AST->getMainFileName().str()))
      ASTlist.push_back(AST);

    return true;
  }

private:
  std::vector<ASTUnit *> &ASTlist;
  const std::function<bool(const std::string &)> &onTUend;
};
}

void
Session::parseFiles(const std::function<bool(const std::string &)> &onTUend) {
  ASTBuilderAction action(ASTlist, onTUend);
  int ret = tool->run(&action);
  assert(!ret && "TODO: handle this error"); // Occurs when a file can not be
                                             // parsed e.g. compilation error.
}

namespace {

struct CollectBoundNodes : MatchFinder::MatchCallback {
  std::vector<BoundNodes> &bindings;

  CollectBoundNodes(std::vector<BoundNodes> &bindings) : bindings(bindings) {}

  void run(const MatchFinder::MatchResult &result) {
    bindings.push_back(result.Nodes);
  }
};
}

void Session::runQuery(const std::string &query) {
  dynamic::Diagnostics diag;
  llvm::Optional<dynamic::DynTypedMatcher> matcher =
      dynamic::Parser::parseMatcherExpression(query, &diag);

  if (!matcher)
    throw QueryError(diag.toString());

  foundMatches.clear();
  Match m;

  for (auto ast : ASTlist) {
    SourceManager &srcMgr = ast->getSourceManager();

    MatchFinder finder;
    std::vector<BoundNodes> boundNodes;
    CollectBoundNodes collector(boundNodes);

    if (!finder.addDynamicMatcher(*matcher, &collector))
      assert(false && "Unimplemented"); // Fails if the top matcher is not valid

    finder.matchAST(ast->getASTContext());

    for (auto nodes : boundNodes) {
      for (auto idToNode : nodes.getMap()) {
        SourceRange range = idToNode.second.getSourceRange();

        if (!range.isValid())
          continue;

        SourceLocation start = range.getBegin();

        if (srcMgr.isInSystemHeader(start) ||
            srcMgr.isInExternCSystemHeader(start))
          continue;

        // Get the last character of the last token
        SourceLocation end = Lexer::getLocForEndOfToken(range.getEnd(), 1,
                                                        srcMgr, LangOptions());

        llvm::StringRef fileName = srcMgr.getFilename(start);

        if (fileName.empty())
          continue;

        m.fileName = fileName.str();
        m.id = idToNode.first;
        m.startCol = srcMgr.getSpellingColumnNumber(start);
        m.startLine = srcMgr.getSpellingLineNumber(start);
        m.endCol = srcMgr.getSpellingColumnNumber(end);
        m.endLine = srcMgr.getSpellingLineNumber(end);

        foundMatches.insert(std::move(m));
      }
    }
  }
}

const std::set<Match> &Session::getMatches() const { return foundMatches; }
<commit_msg>Handled onvalid top level matcher error.<commit_after>#include "session.h"

#include <tuple>
#include <cassert>

#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/Dynamic/Parser.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/JSONCompilationDatabase.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Lexer.h"

using namespace clang;
using namespace tooling;
using namespace ast_matchers;

using namespace CppQuery;

bool CppQuery::operator<(const Match &lhs, const Match &rhs) {
  return std::tie(lhs.fileName, lhs.id, lhs.startLine, lhs.startCol,
                  lhs.endLine,
                  lhs.endCol) < std::tie(rhs.fileName, rhs.id, rhs.startLine,
                                         rhs.startCol, rhs.endLine, rhs.endCol);
}

Session::Session(const std::string &databasePath) {
  std::string error;
  compilationDatabase = std::unique_ptr<JSONCompilationDatabase>(
      JSONCompilationDatabase::loadFromFile(databasePath, error));

  if (!error.empty())
    throw DatabaseError(error);

  files = compilationDatabase->getAllFiles();
  tool = std::unique_ptr<clang::tooling::ClangTool>(
      new ClangTool(*compilationDatabase, files));
}

Session::~Session() {
  for (auto AST : ASTlist) {
    AST->setUnsafeToFree(false);
    delete AST;
  }
}

namespace {
class ASTBuilderAction : public ToolAction {
public:
  ASTBuilderAction(std::vector<ASTUnit *> &ASTlist,
                   const std::function<bool(const std::string &)> &onTUend)
      : ASTlist(ASTlist), onTUend(onTUend) {}

  bool runInvocation(CompilerInvocation *invocation, FileManager *files,
                     DiagnosticConsumer *diagConsumer) {
    ASTUnit *AST = ASTUnit::LoadFromCompilerInvocation(
        invocation, CompilerInstance::createDiagnostics(
                        &invocation->getDiagnosticOpts(), diagConsumer, false));

    if (!AST)
      return false;

    if (onTUend(AST->getMainFileName().str()))
      ASTlist.push_back(AST);

    return true;
  }

private:
  std::vector<ASTUnit *> &ASTlist;
  const std::function<bool(const std::string &)> &onTUend;
};
}

void
Session::parseFiles(const std::function<bool(const std::string &)> &onTUend) {
  ASTBuilderAction action(ASTlist, onTUend);
  int ret = tool->run(&action);
  assert(!ret && "TODO: handle this error"); // Occurs when a file can not be
                                             // parsed e.g. compilation error.
}

namespace {

struct CollectBoundNodes : MatchFinder::MatchCallback {
  std::vector<BoundNodes> &bindings;

  CollectBoundNodes(std::vector<BoundNodes> &bindings) : bindings(bindings) {}

  void run(const MatchFinder::MatchResult &result) {
    bindings.push_back(result.Nodes);
  }
};
}

void Session::runQuery(const std::string &query) {
  dynamic::Diagnostics diag;
  llvm::Optional<dynamic::DynTypedMatcher> matcher =
      dynamic::Parser::parseMatcherExpression(query, &diag);

  if (!matcher)
    throw QueryError(diag.toString());

  foundMatches.clear();
  Match m;

  for (auto ast : ASTlist) {
    SourceManager &srcMgr = ast->getSourceManager();

    MatchFinder finder;
    std::vector<BoundNodes> boundNodes;
    CollectBoundNodes collector(boundNodes);

    if (!finder.addDynamicMatcher(*matcher, &collector))
      throw QueryError("Invalid top level matcher.");

    finder.matchAST(ast->getASTContext());

    for (auto nodes : boundNodes) {
      for (auto idToNode : nodes.getMap()) {
        SourceRange range = idToNode.second.getSourceRange();

        if (!range.isValid())
          continue;

        SourceLocation start = range.getBegin();

        if (srcMgr.isInSystemHeader(start) ||
            srcMgr.isInExternCSystemHeader(start))
          continue;

        // Get the last character of the last token
        SourceLocation end = Lexer::getLocForEndOfToken(range.getEnd(), 1,
                                                        srcMgr, LangOptions());

        llvm::StringRef fileName = srcMgr.getFilename(start);

        if (fileName.empty())
          continue;

        m.fileName = fileName.str();
        m.id = idToNode.first;
        m.startCol = srcMgr.getSpellingColumnNumber(start);
        m.startLine = srcMgr.getSpellingLineNumber(start);
        m.endCol = srcMgr.getSpellingColumnNumber(end);
        m.endLine = srcMgr.getSpellingLineNumber(end);

        foundMatches.insert(std::move(m));
      }
    }
  }
}

const std::set<Match> &Session::getMatches() const { return foundMatches; }
<|endoftext|>


<commit_before>#include "main.h"

#include <math.h>
#include <stdio.h>

#include "drivers/Driver.h"
#include "drivers/TeleopDriver.h"
#include "drivers/BaselockDriver.h"
#include "subsystems/Drive.h"
#include "subsystems/Intake.h"
#include "subsystems/Shooter.h"
#include "subsystems/ShooterController.h"
#include "subsystems/OperatorControl.h"
#include "subsystems/Pid.h"
#include "subsystems/PidCommander.h"
#include "util/Functions.h"
#include "util/Logger.h"
#include "util/PidTuner.h"
#include "vision/BackboardFinder.h"
#include "drivers/AutoTurnDriver.h"

Joystick* xbox = new Joystick(3);

MainRobot::MainRobot() {
  // Constants
  constants_ = Constants::GetInstance();

  // Motors
  leftDriveMotorA_ = new Victor((int)constants_->leftDrivePwmA);
  leftDriveMotorB_ = new Victor((int)constants_->leftDrivePwmB);
  rightDriveMotorA_ = new Victor((int)constants_->rightDrivePwmA);
  rightDriveMotorB_ = new Victor((int)constants_->rightDrivePwmB);
  intakeMotor_ = new Victor((int)constants_->intakePwm);
  conveyorMotor_ = new Victor((int)constants_->conveyorPwm);
  leftShooterMotor_ = new Victor((int)constants_->leftShooterPwm);
  rightShooterMotor_ = new Victor((int)constants_->rightShooterPwm);
  jumbleMotor_ = new Victor((int)constants_->jumblePwm);

  // Sensors
  leftEncoder_ = new Encoder((int)constants_->leftEncoderPortA, (int)constants_->leftEncoderPortB);
  leftEncoder_->Start();
  rightEncoder_ = new Encoder((int)constants_->rightEncoderPortA, (int)constants_->rightEncoderPortB);
  rightEncoder_->Start();
  shooterEncoder_ = new Encoder((int)constants_->shooterEncoderPortA, (int)constants_->shooterEncoderPortB,
                                false, CounterBase::k1X);
  shooterEncoder_->Start();
  conveyorEncoder_ = new Encoder((int)constants_->conveyorEncoderPortA,
                                 (int)constants_->conveyorEncoderPortB, true);
  conveyorEncoder_->Start();
  gyro_ = new Gyro((int)constants_->gyroPort);
  gyro_->SetSensitivity(1.0);
  poofMeter_ = new AnalogChannel((int)constants_->poofMeterPort);
  ballRanger_ = new AnalogChannel((int)constants_->ballRangerPort);

  double accelerometerSensitivity = 1.0;
  //accelerometerX_ = new Accelerometer((int)constants_->accelerometerXPort);
  //accelerometerY_ = new Accelerometer((int)constants_->accelerometerYPort);
  //accelerometerZ_ = new Accelerometer((int)constants_->accelerometerZPort);
  //accelerometerX_->SetSensitivity(accelerometerSensitivity);
  //accelerometerY_->SetSensitivity(accelerometerSensitivity);
  //accelerometerZ_->SetSensitivity(accelerometerSensitivity);
  bumpSensor_ = new DigitalInput((int)constants_->bumpSensorPort);
  conveyorBallSensor_ = new AnalogChannel((int)constants_->conveyorBallSensorPort);

  // Pneumatics
  compressor_ = new Compressor((int)constants_->compressorPressureSwitchPort,(int)constants_->compressorRelayPort);
  compressor_->Start();
  shiftSolenoid_ = new Solenoid((int)constants_->shiftSolenoidPort);
  hoodSolenoid_ = new Solenoid((int)constants_->hoodSolenoidPort);
  pizzaWheelSolenoid_ = new DoubleSolenoid((int)constants_->pizzaWheelSolenoidDownPort, (int)constants_->pizzaWheelSolenoidUpPort);
  intakeSolenoid_ = new DoubleSolenoid((int)constants_->intakeSolenoidUpPort, (int)constants_->intakeSolenoidDownPort);
  brakeSolenoid_ = new DoubleSolenoid((int)constants_->brakeSolenoidOnPort, (int)constants_->brakeSolenoidOffPort);

  // Subsystems
  drivebase_ = new Drive(leftDriveMotorA_, leftDriveMotorB_, rightDriveMotorA_, rightDriveMotorB_,
                         shiftSolenoid_, pizzaWheelSolenoid_, brakeSolenoid_,  leftEncoder_,
                         rightEncoder_, gyro_, accelerometerX_, accelerometerY_,
                         accelerometerZ_, bumpSensor_);
  intake_ = new Intake(intakeMotor_, intakeSolenoid_);
  shooter_ = new Shooter(conveyorMotor_, leftShooterMotor_, rightShooterMotor_, shooterEncoder_,
                         hoodSolenoid_, conveyorEncoder_, conveyorBallSensor_, poofMeter_, ballRanger_);
  sc_ = new ShooterController(shooter_, intake_);

  // Control Board
  leftJoystick_ = new Joystick((int)constants_->leftJoystickPort);
  rightJoystick_ = new Joystick((int)constants_->rightJoystickPort);
  operatorControl_ = new OperatorControl((int)constants_->operatorControlPort);

  // Vision Tasks
  target_ = new BackboardFinder();
  target_->Start();
  ledRingSwitch_ = new DigitalOutput((int)constants_->ledRingSwitchPort);

  // Drivers
  teleopDriver_ = new TeleopDriver(drivebase_, leftJoystick_, rightJoystick_, operatorControl_);
  baselockDriver_ = new BaselockDriver(drivebase_, leftJoystick_);
  autoAlignDriver_ = new AutoTurnDriver(drivebase_, target_);

  // Set the current Driver to teleop, though this will change later
  currDriver_ = teleopDriver_;

  testPid_ = new Pid(&constants_->driveKP, &constants_->driveKI, &constants_->driveKD);
  testTimer_ = new Timer();
  testLogger_ = new Logger("/test.log", 1);

  // Watchdog
  GetWatchdog().SetExpiration(100);

  // Get a local instance of the Driver Station LCD
  lcd_ = DriverStationLCD::GetInstance();
  lcd_->PrintfLine(DriverStationLCD::kUser_Line1,"***Teleop Ready!***");

  shooterTargetVelocity_ = 0;
  oldShooterUpSwitch_ = false;
  oldShooterDownSwitch_ = false;
  oldBallQueueSwitch_ = false;
}

void MainRobot::DisabledInit() {
}

void MainRobot::AutonomousInit() {
  constants_->LoadFile();
  delete testPid_;
  testPid_ = new Pid(&constants_->driveKP, &constants_->driveKI, &constants_->driveKD);
  drivebase_->ResetGyro();
  drivebase_->ResetEncoders();
  testPid_->ResetError();
  testTimer_->Reset();
  testTimer_->Start();
  conveyorEncoder_->Reset();
  GetWatchdog().SetEnabled(false);
  power_ = 0;
}

void MainRobot::TeleopInit() {
  constants_->LoadFile();
  drivebase_->ResetGyro();
  drivebase_->ResetEncoders();
  testTimer_->Start();
  shooter_->ResetQueueState();

  // Start off with the TeleopDriver
  currDriver_ = teleopDriver_;
  currDriver_->Reset();
  GetWatchdog().SetEnabled(true);
}

void MainRobot::DisabledPeriodic() {
  lcd_->PrintfLine(DriverStationLCD::kUser_Line1, "Convey: %d", conveyorEncoder_->Get());
  lcd_->PrintfLine(DriverStationLCD::kUser_Line2, "Ball: %d", conveyorBallSensor_->GetValue());
  lcd_->PrintfLine(DriverStationLCD::kUser_Line3, "Poof: %d", poofMeter_->GetValue());
  lcd_->UpdateLCD();
}

void MainRobot::AutonomousPeriodic() {
/*
  double time = testTimer_->Get();
  double rate = conveyorEncoder_->GetRate();

  if (time > 0.5) {
    power_ += 0.01;
    testTimer_->Reset();
    testLogger_->Log("%f,%f\n", power_, rate);
  }
  lcd_->PrintfLine(DriverStationLCD::kUser_Line5,"Pow: %.0f%%", power_ * 100);
  lcd_->PrintfLine(DriverStationLCD::kUser_Line6,"Rate: %.0f%%", rate);
  lcd_->UpdateLCD();
  shooter_->SetLinearConveyorPower(power_);
*/
  int target = (int)Functions::SquareWave(testTimer_->Get(), 10, 500) + 500;
  shooter_->SetConveyorTarget(target);
  shooter_->ConveyorPIDUpdate();
  //  PidTuner::PushData(target, conveyorEncoder_->Get(), 0);
}

void MainRobot::TeleopPeriodic() {
  GetWatchdog().Feed();
  drivebase_->SetBrakeOn(false);

  // Ghetto shooter control for testing
  if (xbox->GetRawButton(10)) {
    shooterTargetVelocity_ = 0;
  } else if (xbox->GetRawButton(9) && !oldShooterDownSwitch_) {
    shooterTargetVelocity_ -= 2;
  } else if (xbox->GetRawButton(8) && !oldShooterUpSwitch_) {
    shooterTargetVelocity_ += 2;
  } else if (xbox->GetRawButton(7)) {
    shooterTargetVelocity_ = 38;
  }
  oldShooterUpSwitch_ = xbox->GetRawButton(8);
  oldShooterDownSwitch_ = xbox->GetRawButton(9);
  shooter_->SetTargetVelocity(shooterTargetVelocity_);
  shooter_->PIDUpdate();

  // Automatic ball queueing control.
  if (xbox->GetTwist() < -0.75) {
    //    if (shooter_->QueueBall() && !oldBallQueueSwitch_) {
    //      shooter_->ShootBall();
    //    }
    intake_->SetIntakePower(1.0);
  } else {
    shooter_->SetLinearConveyorPower(0);
    intake_->SetIntakePower(0);
  }
  oldBallQueueSwitch_ = (xbox->GetTwist() < -0.75);

  if (xbox->GetRawButton(12)) {
    intake_->SetIntakePower(1.0);
    shooter_->SetLinearConveyorPower(-1.0);
  } else if (xbox->GetRawButton(11)) {
    intake_->SetIntakePower(1.0);
    shooter_->SetLinearConveyorPower(1.0);
  } else if (xbox->GetTwist() < -0.75) {
    intake_->SetIntakePower(1.0);
  } else if (xbox->GetZ() < -0.75) {
    intake_->SetIntakePower(-1.0);
    shooter_->SetLinearConveyorPower(-1.0);
  } else {
    intake_->SetIntakePower(0);
  }

  if (xbox->GetRawButton(6)) {
    intake_->SetIntakePosition(Intake::INTAKE_UP);
  } else if (xbox->GetRawButton(4)) {
    intake_->SetIntakePosition(Intake::INTAKE_DOWN);
  } else if (xbox->GetRawButton(5)) {
    intake_->SetIntakePosition(Intake::INTAKE_FLOATING);
  }

  if (shooterTargetVelocity_ > 50) {
    shooter_->SetHoodUp(true);
  } else {
    shooter_->SetHoodUp(false);
  }

  ledRingSwitch_->Set(shooterTargetVelocity_ > 0);

  // Only have Teleop and Baselock Drivers right now
  if (leftJoystick_->GetRawButton(4) && !oldAutoAlignButton_) {
    currDriver_ = autoAlignDriver_;
    currDriver_->Reset();
  } else if (!leftJoystick_->GetRawButton(4) && !operatorControl_->GetBaseLockSwitch() ) {
      // If the baselock switch has been flipped off, switch back to teleop
      currDriver_ = teleopDriver_;
      currDriver_->Reset();
  }

  // Update the driver and the baselock switch status
  currDriver_->UpdateDriver();
  oldAutoAlignButton_ = leftJoystick_->GetRawButton(4);
  //oldBaseLockSwitch_ = operatorControl_->GetBaseLockSwitch();

  double velocity = shooter_->GetVelocity();
  lcd_->PrintfLine(DriverStationLCD::kUser_Line3,"Vel: %f", velocity);
  lcd_->PrintfLine(DriverStationLCD::kUser_Line4,"Shoot: %.0f%%", shooterTargetVelocity_);
  lcd_->PrintfLine(DriverStationLCD::kUser_Line5, "Ranger: %d", ballRanger_->GetValue());
  lcd_->UpdateLCD();
  //
}
<commit_msg>Fixed compiler errors<commit_after>#include "main.h"

#include <math.h>
#include <stdio.h>

#include "drivers/Driver.h"
#include "drivers/TeleopDriver.h"
#include "drivers/BaselockDriver.h"
#include "subsystems/Drive.h"
#include "subsystems/Intake.h"
#include "subsystems/Shooter.h"
#include "subsystems/ShooterController.h"
#include "subsystems/OperatorControl.h"
#include "subsystems/Pid.h"
#include "subsystems/PidCommander.h"
#include "util/Functions.h"
#include "util/Logger.h"
#include "util/PidTuner.h"
#include "vision/BackboardFinder.h"
#include "drivers/AutoTurnDriver.h"

Joystick* xbox = new Joystick(3);

MainRobot::MainRobot() {
  // Constants
  constants_ = Constants::GetInstance();

  // Motors
  leftDriveMotorA_ = new Victor((int)constants_->leftDrivePwmA);
  leftDriveMotorB_ = new Victor((int)constants_->leftDrivePwmB);
  rightDriveMotorA_ = new Victor((int)constants_->rightDrivePwmA);
  rightDriveMotorB_ = new Victor((int)constants_->rightDrivePwmB);
  intakeMotor_ = new Victor((int)constants_->intakePwm);
  conveyorMotor_ = new Victor((int)constants_->conveyorPwm);
  leftShooterMotor_ = new Victor((int)constants_->leftShooterPwm);
  rightShooterMotor_ = new Victor((int)constants_->rightShooterPwm);
  jumbleMotor_ = new Victor((int)constants_->jumblePwm);

  // Sensors
  leftEncoder_ = new Encoder((int)constants_->leftEncoderPortA, (int)constants_->leftEncoderPortB);
  leftEncoder_->Start();
  rightEncoder_ = new Encoder((int)constants_->rightEncoderPortA, (int)constants_->rightEncoderPortB);
  rightEncoder_->Start();
  shooterEncoder_ = new Encoder((int)constants_->shooterEncoderPortA, (int)constants_->shooterEncoderPortB,
                                false, CounterBase::k1X);
  shooterEncoder_->Start();
  conveyorEncoder_ = new Encoder((int)constants_->conveyorEncoderPortA,
                                 (int)constants_->conveyorEncoderPortB, true);
  conveyorEncoder_->Start();
  gyro_ = new Gyro((int)constants_->gyroPort);
  gyro_->SetSensitivity(1.0);
  poofMeter_ = new AnalogChannel((int)constants_->poofMeterPort);
  ballRanger_ = new AnalogChannel((int)constants_->ballRangerPort);

  double accelerometerSensitivity = 1.0;
  //accelerometerX_ = new Accelerometer((int)constants_->accelerometerXPort);
  //accelerometerY_ = new Accelerometer((int)constants_->accelerometerYPort);
  //accelerometerZ_ = new Accelerometer((int)constants_->accelerometerZPort);
  //accelerometerX_->SetSensitivity(accelerometerSensitivity);
  //accelerometerY_->SetSensitivity(accelerometerSensitivity);
  //accelerometerZ_->SetSensitivity(accelerometerSensitivity);
  bumpSensor_ = new DigitalInput((int)constants_->bumpSensorPort);
  conveyorBallSensor_ = new AnalogChannel((int)constants_->conveyorBallSensorPort);

  // Pneumatics
  compressor_ = new Compressor((int)constants_->compressorPressureSwitchPort,(int)constants_->compressorRelayPort);
  compressor_->Start();
  shiftSolenoid_ = new Solenoid((int)constants_->shiftSolenoidPort);
  hoodSolenoid_ = new Solenoid((int)constants_->hoodSolenoidPort);
  pizzaWheelSolenoid_ = new DoubleSolenoid((int)constants_->pizzaWheelSolenoidDownPort, (int)constants_->pizzaWheelSolenoidUpPort);
  intakeSolenoid_ = new DoubleSolenoid((int)constants_->intakeSolenoidUpPort, (int)constants_->intakeSolenoidDownPort);
  brakeSolenoid_ = new DoubleSolenoid((int)constants_->brakeSolenoidOnPort, (int)constants_->brakeSolenoidOffPort);

  // Subsystems
  drivebase_ = new Drive(leftDriveMotorA_, leftDriveMotorB_, rightDriveMotorA_, rightDriveMotorB_,
                         shiftSolenoid_, pizzaWheelSolenoid_, brakeSolenoid_,  leftEncoder_,
                         rightEncoder_, gyro_, accelerometerX_, accelerometerY_,
                         accelerometerZ_, bumpSensor_);
  intake_ = new Intake(intakeMotor_, intakeSolenoid_);
  shooter_ = new Shooter(conveyorMotor_, leftShooterMotor_, rightShooterMotor_, shooterEncoder_,
                         hoodSolenoid_, conveyorEncoder_, conveyorBallSensor_, poofMeter_, ballRanger_);
  sc_ = new ShooterController(shooter_, intake_);

  // Control Board
  leftJoystick_ = new Joystick((int)constants_->leftJoystickPort);
  rightJoystick_ = new Joystick((int)constants_->rightJoystickPort);
  operatorControl_ = new OperatorControl((int)constants_->operatorControlPort);

  // Vision Tasks
  target_ = new BackboardFinder();
  target_->Start();
  ledRingSwitch_ = new DigitalOutput((int)constants_->ledRingSwitchPort);

  // Drivers
  teleopDriver_ = new TeleopDriver(drivebase_, leftJoystick_, rightJoystick_, operatorControl_);
  baselockDriver_ = new BaselockDriver(drivebase_, leftJoystick_);
  autoAlignDriver_ = new AutoTurnDriver(drivebase_, target_);

  // Set the current Driver to teleop, though this will change later
  currDriver_ = teleopDriver_;

  testPid_ = new Pid(&constants_->driveKP, &constants_->driveKI, &constants_->driveKD);
  testTimer_ = new Timer();
  testLogger_ = new Logger("/test.log", 1);

  // Watchdog
  GetWatchdog().SetExpiration(100);

  // Get a local instance of the Driver Station LCD
  lcd_ = DriverStationLCD::GetInstance();
  lcd_->PrintfLine(DriverStationLCD::kUser_Line1,"***Teleop Ready!***");

  shooterTargetVelocity_ = 0;
  oldShooterUpSwitch_ = false;
  oldShooterDownSwitch_ = false;
  oldBallQueueSwitch_ = false;
}

void MainRobot::DisabledInit() {
}

void MainRobot::AutonomousInit() {
  constants_->LoadFile();
  delete testPid_;
  testPid_ = new Pid(&constants_->driveKP, &constants_->driveKI, &constants_->driveKD);
  drivebase_->ResetGyro();
  drivebase_->ResetEncoders();
  testPid_->ResetError();
  testTimer_->Reset();
  testTimer_->Start();
  conveyorEncoder_->Reset();
  GetWatchdog().SetEnabled(false);
  power_ = 0;
}

void MainRobot::TeleopInit() {
  constants_->LoadFile();
  drivebase_->ResetGyro();
  drivebase_->ResetEncoders();
  testTimer_->Start();
  shooter_->ResetQueueState();

  // Start off with the TeleopDriver
  currDriver_ = teleopDriver_;
  currDriver_->Reset();
  GetWatchdog().SetEnabled(true);
}

void MainRobot::DisabledPeriodic() {
  lcd_->PrintfLine(DriverStationLCD::kUser_Line1, "Convey: %d", conveyorEncoder_->Get());
  lcd_->PrintfLine(DriverStationLCD::kUser_Line2, "Ball: %d", conveyorBallSensor_->GetValue());
  lcd_->PrintfLine(DriverStationLCD::kUser_Line3, "Poof: %d", poofMeter_->GetValue());
  lcd_->UpdateLCD();
}

void MainRobot::AutonomousPeriodic() {
/*
  double time = testTimer_->Get();
  double rate = conveyorEncoder_->GetRate();

  if (time > 0.5) {
    power_ += 0.01;
    testTimer_->Reset();
    testLogger_->Log("%f,%f\n", power_, rate);
  }
  lcd_->PrintfLine(DriverStationLCD::kUser_Line5,"Pow: %.0f%%", power_ * 100);
  lcd_->PrintfLine(DriverStationLCD::kUser_Line6,"Rate: %.0f%%", rate);
  lcd_->UpdateLCD();
  shooter_->SetLinearConveyorPower(power_);
*/
  int target = (int)Functions::SquareWave(testTimer_->Get(), 10, 500) + 500;
  shooter_->SetConveyorTarget(target);
  shooter_->ConveyorPIDUpdate();
  //  PidTuner::PushData(target, conveyorEncoder_->Get(), 0);
}

void MainRobot::TeleopPeriodic() {
  GetWatchdog().Feed();
  drivebase_->SetBrakeOn(false);

  // Ghetto shooter control for testing
  if (xbox->GetRawButton(10)) {
    shooterTargetVelocity_ = 0;
  } else if (xbox->GetRawButton(9) && !oldShooterDownSwitch_) {
    shooterTargetVelocity_ -= 2;
  } else if (xbox->GetRawButton(8) && !oldShooterUpSwitch_) {
    shooterTargetVelocity_ += 2;
  } else if (xbox->GetRawButton(7)) {
    shooterTargetVelocity_ = 38;
  }
  oldShooterUpSwitch_ = xbox->GetRawButton(8);
  oldShooterDownSwitch_ = xbox->GetRawButton(9);
  shooter_->SetTargetVelocity(shooterTargetVelocity_);
  shooter_->PIDUpdate();

  // Automatic ball queueing control.
  if (xbox->GetTwist() < -0.75) {
    //    if (shooter_->QueueBall() && !oldBallQueueSwitch_) {
    //      shooter_->ShootBall();
    //    }
    intake_->SetIntakePower(1.0);
  } else {
    shooter_->SetLinearConveyorPower(0);
    intake_->SetIntakePower(0);
  }
  oldBallQueueSwitch_ = (xbox->GetTwist() < -0.75);

  if (xbox->GetRawButton(12)) {
    intake_->SetIntakePower(1.0);
    shooter_->SetLinearConveyorPower(-1.0);
  } else if (xbox->GetRawButton(11)) {
    intake_->SetIntakePower(1.0);
    shooter_->SetLinearConveyorPower(1.0);
  } else if (xbox->GetTwist() < -0.75) {
    intake_->SetIntakePower(1.0);
  } else if (xbox->GetZ() < -0.75) {
    intake_->SetIntakePower(-1.0);
    shooter_->SetLinearConveyorPower(-1.0);
  } else {
    intake_->SetIntakePower(0);
  }

  if (xbox->GetRawButton(6)) {
    intake_->SetIntakePosition(Intake::INTAKE_UP);
  } else if (xbox->GetRawButton(4)) {
    intake_->SetIntakePosition(Intake::INTAKE_DOWN);
  } else if (xbox->GetRawButton(5)) {
    intake_->SetIntakePosition(Intake::INTAKE_FLOATING);
  }

  if (shooterTargetVelocity_ > 50) {
    shooter_->SetHoodUp(true);
  } else {
    shooter_->SetHoodUp(false);
  }

  ledRingSwitch_->Set(shooterTargetVelocity_ > 0);

  // Only have Teleop and Baselock Drivers right now
  if (leftJoystick_->GetRawButton(4) && !oldAutoAlignButton_) {
    currDriver_ = autoAlignDriver_;
    currDriver_->Reset();
  } else if (!leftJoystick_->GetRawButton(4)) {
      // If the baselock switch has been flipped off, switch back to teleop
      currDriver_ = teleopDriver_;
      currDriver_->Reset();
  }

  // Update the driver and the baselock switch status
  currDriver_->UpdateDriver();
  oldAutoAlignButton_ = leftJoystick_->GetRawButton(4);
  //oldBaseLockSwitch_ = operatorControl_->GetBaseLockSwitch();

  double velocity = shooter_->GetVelocity();
  lcd_->PrintfLine(DriverStationLCD::kUser_Line3,"Vel: %f", velocity);
  lcd_->PrintfLine(DriverStationLCD::kUser_Line4,"Shoot: %.0f%%", shooterTargetVelocity_);
  lcd_->PrintfLine(DriverStationLCD::kUser_Line5, "Ranger: %d", ballRanger_->GetValue());
  lcd_->UpdateLCD();
  //
}
<|endoftext|>


<commit_before>#ifndef INCLUDED_COLDB_COLUMN_HPP
#define INCLUDED_COLDB_COLUMN_HPP

#include <algorithm>
#include "types.hpp"

namespace coldb{

// abstract interface of all column objects
// IFType: type of interface
// ACType: type of actual storage
template <typename IFType>
class Column
{
public:
  Column() {}
  virtual ~Column() {}
  virtual I32 get_size() = 0;
  virtual IFType get(I32 rowid) = 0;  // get the item in rowid row
};

template <typename IFType, class Impl>
class ColumnImpl : public Column<IFType>
{
private:
  Impl impl_;
public:
  ColumnImpl(void* data_ptr, I32 data_size)
    : Column(),
      impl_(data_ptr, data_size)
  {}

  I32 get_size() {return impl_.data_size_;}

  IFType get(I32 rowid) {return impl_.get(rowid);}
};

// abstract interface of sorted column for findings
template <typename IFType>
class SortedColumn : public virtual Column<IFType, ACType> {
public:
  virtual I32 find(IFType var) = 0;  // find the row of target value
};

template <typename IFType, class Impl>
class SortedColumnImpl
  : public SortedColumn<IFType>, public ColumnImpl<IFType, Impl>
{
public:
  SortedColumnImpl(void* data_ptr, I32 data_size)
    : SortedColumn(),
      ColumnImpl(data_ptr, data_size)
  {}

  I32 find(IFType var){return impl_.find(var);}
};

template <typename IFType>
class FKeyColumn : public virtual Column<IFType>
{
public:
  // virtual I32 find_by_tgt_row(I32 tgt_row) = 0;
  virtual I32 get_tgt_row(I32 rowid) = 0;
};

template <typename IFType, class Impl, class TgtImpl>
class FKeyColumnImpl : public FKeyColumn<IFType>
{
private:
  Impl impl_;
  TgtImpl tgt_impl_;
public:
  FKeyColumnImpl(void* data_ptr, I32 data_size, TgtImpl& tgt_impl)
    : impl_(data_ptr, data_size), tgt_impl_(tgt_impl)
  {}

  I32 get_tgt_row(I32 rowid) {return impl_.get(rowid);}

  IFType get(I32 rowid) {return tgt_impl_.get(get_tgt_row(rowid));}
};

template <typename IFType>
class SortedFKeyColumn
  : public virtual SortedColumn<IFType>, public virtual FKeyColumn<IFType>
{
public:
  virtual I32 find_dest_row(I32 tgt_row) = 0;
};

template <typename IFType, class Impl, class TgtImpl>
class SortedFKeyColumnImpl
  : public SortedFKeyColumn<IFType>,
    public FKeyColumnImpl<IFType, Impl, TgtImpl>
{
public:
  SortedFKeyColumnImpl(void* data_ptr, I32 data_size, TgtImpl& tgt_impl)
    : SortedFKeyColumn(),
      FKeyColumnImpl(data_ptr, data_size, tgt_impl)
  {}

  I32 find_dest_row(I32 tgt_row) {return impl_.find(tgt_row);}

  I32 find(IFType var) {return impl_.find(tgt_impl_.find(var));}
};

template <typename DT>
struct PlainImpl
{
  DT* data_ptr_;
  I32 data_size_;

  PlainImpl(DT* data_ptr, I32 data_size)
    : data_ptr_(data_ptr), data_size_(data_size)
  {}

  DT get(I32 rowid) {return data_ptr_[i];}

  I32 find(DT var)
  {
    I32 i = std::lower_bound(data_ptr_, data_ptr_ + data_size_, var);
    if(data_ptr_[i] == var)
    {
      return i;
    }
    return -1;
  }
};

template <typename DT, typename PT>
struct Run0Impl{
};

}
#endif
<commit_msg>basic implementations for interger arrs all done, yeah!<commit_after>#ifndef INCLUDED_COLDB_COLUMN_HPP
#define INCLUDED_COLDB_COLUMN_HPP

#include <algorithm>
#include "types.hpp"

namespace coldb{

template <typename T>
inline void* aligned(void* ptr)
{
  U32 align_mask = sizeof(T)-1;
  U32 u_ptr = (U32)ptr;
  U32 tail = u_ptr & align_mask;
  if(tail)
  {
    return (void*) (u_ptr + sizeof(T) - tail);
  }
  return ptr;
}

template <>
inline void* aligned<U8>(void* ptr) {return ptr;}

template <>
inline void* aligned<I8>(void* ptr) {return ptr;}

// abstract interface of all column objects
// IFType: type of interface
// ACType: type of actual storage
template <typename IFType>
class Column
{
public:
  Column() {}
  virtual ~Column() {}
  virtual I32 get_size() = 0;
  virtual IFType get(I32 rowid) = 0;  // get the item in rowid row
};

template <typename IFType, class Impl>
class ColumnImpl : public Column<IFType>
{
private:
  Impl impl_;
public:
  ColumnImpl(void* data_ptr, I32 data_size)
    : Column(),
      impl_(data_ptr, data_size)
  {}

  I32 get_size() {return impl_.data_size_;}

  IFType get(I32 rowid) {return impl_.get(rowid);}
};

// abstract interface of sorted column for findings
template <typename IFType>
class SortedColumn : public virtual Column<IFType, ACType> {
public:
  virtual I32 find(IFType var) = 0;  // find the row of target value
};

template <typename IFType, class Impl>
class SortedColumnImpl
  : public SortedColumn<IFType>, public ColumnImpl<IFType, Impl>
{
public:
  SortedColumnImpl(void* data_ptr, I32 data_size)
    : SortedColumn(),
      ColumnImpl(data_ptr, data_size)
  {}

  I32 find(IFType var){return impl_.find(var);}
};

template <typename IFType>
class FKeyColumn : public virtual Column<IFType>
{
public:
  // virtual I32 find_by_tgt_row(I32 tgt_row) = 0;
  virtual I32 get_tgt_row(I32 rowid) = 0;
};

template <typename IFType, class Impl, class TgtImpl>
class FKeyColumnImpl : public FKeyColumn<IFType>
{
private:
  Impl impl_;
  TgtImpl tgt_impl_;
public:
  FKeyColumnImpl(void* data_ptr, I32 data_size, TgtImpl& tgt_impl)
    : impl_(data_ptr, data_size), tgt_impl_(tgt_impl)
  {}

  I32 get_tgt_row(I32 rowid) {return impl_.get(rowid);}

  IFType get(I32 rowid) {return tgt_impl_.get(get_tgt_row(rowid));}
};

template <typename IFType>
class SortedFKeyColumn
  : public virtual SortedColumn<IFType>, public virtual FKeyColumn<IFType>
{
public:
  virtual I32 find_dest_row(I32 tgt_row) = 0;
};

template <typename IFType, class Impl, class TgtImpl>
class SortedFKeyColumnImpl
  : public SortedFKeyColumn<IFType>,
    public FKeyColumnImpl<IFType, Impl, TgtImpl>
{
public:
  SortedFKeyColumnImpl(void* data_ptr, I32 data_size, TgtImpl& tgt_impl)
    : SortedFKeyColumn(),
      FKeyColumnImpl(data_ptr, data_size, tgt_impl)
  {}

  I32 find_dest_row(I32 tgt_row) {return impl_.find(tgt_row);}

  I32 find(IFType var) {return impl_.find(tgt_impl_.find(var));}
};

template <typename DT>
struct PlainImpl
{
  DT* data_ptr_;
  I32 data_size_;

  PlainImpl(DT* data_ptr, I32 data_size)
    : data_ptr_(data_ptr), data_size_(data_size)
  {}

  DT get(I32 rowid) {return data_ptr_[i];}

  I32 find(DT var)
  {
    I32 i = std::lower_bound(data_ptr_, data_ptr_ + data_size_, var);
    if(data_ptr_[i] == var)
    {
      return i;
    }
    return -1;
  }
};

template <typename DT, typename PT>
struct Run0Impl
{
  I32 data_size_;
  I32 run_cnt_;
  PT* run_ptr_;
  DT* data_ptr_;

  Run0Impl(void* data_ptr, I32 data_size) : data_size_(data_size)
  {
    run_cnt_ = *((PT*)data_ptr);
    run_ptr_ = ((PT*)data_ptr) + 1;
    data_ptr_ = aligned<DT>(run_ptr_ + run_cnt_);
  }

  DT get(I32 rowid)
  {
    // find rowid in run_ptr
    I32 runid = std::lower_bound(run_ptr_, run_ptr_ + run_cnt_, rowid);

    if(run_ptr_[runid] != rowid)
    {
      --runid;
    }
    // rtn data from data_ptr
    return data_ptr_[runid];
  }

  I32 find(DT var)
  {
    I32 runid = std::lower_bound(data_ptr_, data_ptr_ + run_cnt_, var);
    if(data_ptr_[runid] == var)
    {
      return run_ptr_[runid];
    }
    return -1;
  }
};

template <typename DT, typename PT>
struct Run1Impl : public Run0Impl<DT, PT>
{
  DT get(I32 rowid)
  {
    // find rowid in run_ptr
    I32 runid = std::lower_bound(run_ptr_, run_ptr_ + run_cnt_, rowid);

    if(run_ptr_[runid] != rowid)
    {
      --runid;
    }
    return data_ptr_[runid] + (rowid - run_ptr_[runid]);
  }

  I32 find(DT var)
  {
    I32 runid = std::lower_bound(data_ptr_, data_ptr_ + run_cnt_, var);
    DT* this_run = run_ptr_ + runid;
    I32 runlen;
    I32 diff = var - data_ptr_[runid];
    // get length of this run
    if(runid == (run_cnt_ - 1))
    {
      runlen = data_size_ - *this_run;
    }
    else
    {
      runlen = *(this_run + 1) - *this_run;
    }

    if(diff < runlen)
    {
      return *this_run + diff;
    }
    return -1;
  }
};

template <typename DT, typename ET>
struct EnumImpl
{
  I32 data_size_;
  I32 enum_cnt_;
  ET* new_val_ptr_;
  DT* enum_ptr_;
  EnumImpl(void* data_ptr, I32 data_size) : data_size_(data_size)
  {
    enum_cnt_ = *((ET*)data_ptr);
    new_val_ptr_ = ((ET*)data_ptr) + 1;
    enum_ptr_ = aligned<DT>(new_val_ptr_ + data_size_);
  }

  DT get(I32 rowid)
  {
    return enum_ptr_[new_val_ptr_[rowid]];
  }

  // find is oddly used, whatever
  I32 find(DT var)
  {
    enum_i = std::lower_bound(enum_ptr_, enum_ptr_ + enum_cnt_, var);
    if(enum_ptr_[enum_i] != var)
    {
      return -1;
    }

    return std::lower_bound(new_val_ptr_, new_val_ptr_ + data_size_, enum_i);
  }
}

}
#endif
<|endoftext|>