string Relation::solvedQueryToString(vector<Token>& inputTokens)
{
    string out;
    vector<Token> noStrings = removeStrings(inputTokens);
    vector<std::pair<Token, vector<int> > > myMap;
    for(int i = 0; i < noStrings.size(); i++)
    {
        bool inserted = false;
        for(int j = 0; j < myMap.size(); j++)
        {
            if(myMap[j].first.getTokensValue() == noStrings[i].getTokensValue())
            {
                myMap[j].second.push_back(i);
                inserted = true;
            }
        }
        if(!inserted && noStrings[i].getTokenType() == ID)
        { 
            vector<int> newVec;
            newVec.push_back(i);
            myMap.push_back(pair<Token, vector<int> >(noStrings[i], newVec) );
        }
    }

    for(set<Tuple>::iterator it = tuples->begin(); it != tuples->end(); it++)
    {
        int firstInMapVector = 0;
        Tuple thisTuple = (*it);
        if(thisTuple.toString() != "")
        {
            out += "\n  ";
        }
        for(int i = 0; i < myMap.size(); i++)
        {
            if(myMap[i].first.getTokenType() == ID)
            {
                out += myMap[i].first.getTokensValue() + "=";
                out += thisTuple.getTokenFromPairAt(myMap[i].second[firstInMapVector]).getTokensValue();
                if(i + 1 != myMap.size())
                {
                    out += ", ";
                }
            }
        }
    }
    return out;
}
Relation Relation::select(vector<Token>& inputTokens)
{
    Relation newRelation = (*this);
    set<Tuple>* newTuples = new set<Tuple>();
    map<Token, vector<int> > myMap;
    
    for(int i = 0; i < inputTokens.size(); i++)
    {
        if(inputTokens[i].getTokenType() == ID)
        {
            bool inserted = false;
            for(map<Token, vector<int> >::iterator it = myMap.begin(); it != myMap.end(); it++)
            {
                if(it->first.getTokensValue() == inputTokens[i].getTokensValue())
                {
                    it->second.push_back(i);
                    inserted = true;
                }
            }
            if(!inserted)
            { 
                vector<int> newVec;
                newVec.push_back(i);
                myMap.insert(pair<Token, vector<int> >(inputTokens[i], newVec) );
            }
        }
    }

    //Goes through all Tuples in THIS relation, checks if all query strings match, and adds matching tuples to the returned relation
    for(set<Tuple>::iterator it = tuples->begin(); it != tuples->end(); it++) //for all tuples in THIS
    {
        bool isTrue = true;
        Tuple tempTuple = (*it);
        Tuple thisTuple = tempTuple;
        for(int j = 0; j < thisTuple.getPairVectorSize(); j++) //for all pairs in this tuple
        {
            for(int i = 0; i < inputTokens.size(); i++) //for all parameters in the query
            {
                //CHECKS FOR TUPLES WITH CORRECT STRING COMBINATIONS
                if(inputTokens[i].getTokenType() == STRING && j == i) //if this parameter is the placeholder string 
                {
                    if(inputTokens[i].getTokensValue() != thisTuple.getTokenFromPairAt(j).getTokensValue()) //if the values don't match
                    {
                        isTrue = false; //do not add to the new relation
                    }
                }
                //CHECK FOR TUPLES WITH DUPLICATE VALUES FOR DUPLICATE IDs IN QUERY
                else if(inputTokens[i].getTokenType() == ID)
                {
                    for(map<Token, vector<int> >::iterator mit = myMap.begin(); mit != myMap.end(); mit++)
                    {
                        for(int k = 0; k < thisTuple.getPairVectorSize(); k++)
                        {
                            for(int h = 0; h < mit->second.size(); h++)
                            {
                                if(k == mit->second[h])
                                {
                                    for(int n = 0; n < mit->second.size(); n++)
                                    {
                                        if(thisTuple.getPairs()[k].second.getTokensValue() != thisTuple.getPairs()[mit->second[n]].second.getTokensValue())
                                        {
                                            isTrue = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if(isTrue) //otherwise add the tuple to the new relation
        {
            newTuples->insert(thisTuple);
        }
    }
    set<Tuple>* deleteSet = newRelation.getTuples();
    newRelation.setTuples(newTuples);
    delete deleteSet;
    return newRelation;
}