Esempio n. 1
0
int main(int argc, char **argv)
{
  if(argc < 2) return EXIT_FAILURE;
  int stage = atoi(argv[1]);
  char *state = argv[2];
  if(!isValidState(state)) return EXIT_FAILURE;
  
  if(stage == 1){
    //char *state = argv[2];
    char *movelist = argv[3];
    //if(!isValidState(state)) return EXIT_FAILURE;
    if(!isValidMoveList(movelist)) return EXIT_FAILURE;
    //printf("processing...\n");
    processMoveList(state, movelist);
  }
  else if(stage == 2){
    int n_moves = atoi(argv[3]);
    if(n_moves < 0 || n_moves > 9) return EXIT_FAILURE;
    MoveTree *generate = generateAll(state, n_moves);
    MoveTree_print(generate);
    free(generate);
  }
  else if(stage == 3){
    char *solution = solve(state);
    printf("%s\n", solution);
    free(solution);
  }
  return EXIT_SUCCESS;
}
Esempio n. 2
0
File: DFA.cpp Progetto: aliench0/DFA
bool DFA::isFinalState(int state) {
	if (isValidState(state)) {
		for (int i = 0; i < countOfFinalStates; i++)
			if (finalStates[i] == state)
				return true;
	}
	return false;
}
Esempio n. 3
0
bool ContextBase::testCmdArgs(int argc, char **argv) {
	_argc = argc;
	_argv = argv;
	_skipFirstArgs = 1;
	_origProgramName = argv[0];
	setProgram(_programNames[_origProgramName]);
	_argsProcessed.resize(_argc - _skipFirstArgs, false);

	if (!parseCmdArgs(argc, argv, 1) || getShowHelp() || !isValidState()) {
		return false;
	}
	return true;
}
Esempio n. 4
0
bool ContextBase::testCmdArgs(int argc, char **argv) {
	_argc = argc;
	_argv = argv;
	_skipFirstArgs = 1;
	setProgram(_programNames[argv[0]]);
	_argsProcessed.resize(_argc - _skipFirstArgs, false);

	if (!parseCmdArgs(argc, argv, 1) || getShowHelp() || !isValidState()) {
		if (!_errorMsg.empty()) {
			cerr <<_errorMsg << endl;
		}
		return false;
	}
	return true;
}
Esempio n. 5
0
void MonsterMonkBoatState::printSelf() const
{
    std::cout << "==============================" << std::endl;
    if(isValidState())
    {
        //std::cout << "\tLocal Monk:" << m_localMonkCount << std::endl;
        //std::cout << "\tLocal Monster:" << m_localMonsterCount << std::endl;
        //std::cout << "\tRemote Monk:" << m_remoteMonkCount << std::endl;
        //std::cout << "\tRemote Monster:" << m_remoteMonsterCount << std::endl;
        //std::cout << "\tBoat Location:" << s_boatLocation[m_boatLoc] << std::endl;
        std::cout << "(" << m_localMonkCount << ", " <<  m_localMonsterCount << ", ";
		std::cout << m_remoteMonkCount << ", " << m_remoteMonsterCount << ", ";
		std::cout << s_boatLocation[m_boatLoc] << ")" << std::endl;
    }
    std::cout << "==============================" << std::endl;
}
void addSucessorsToStack(std::stack<Node*>& nodes, Node* parentNode, std::vector<Node*>& statesVisited)
{
	for (int numberOfMissinariesToMove = 0; numberOfMissinariesToMove <= 2; ++numberOfMissinariesToMove)
	{
		for (int numberOfCanibalsToMove = 0; numberOfCanibalsToMove <= 2; ++numberOfCanibalsToMove)
		{
			if (numberOfCanibalsToMove + numberOfMissinariesToMove > 2)
			{
				continue;
			}

			Node* newState = (Node*) malloc(sizeof(Node));

			if (parentNode->boat == 1)
			{
				newState->boat = 0;
				newState->missionaries = parentNode->missionaries - numberOfMissinariesToMove;
				newState->canibals = parentNode->canibals - numberOfCanibalsToMove;
			}
			else
			{
				newState->boat = 1;
				newState->missionaries = parentNode->missionaries + numberOfMissinariesToMove;
				newState->canibals = parentNode->canibals + numberOfCanibalsToMove;
			}

			if (isValidState(*newState))
			{
				if (std::find(statesVisited.begin(), statesVisited.end(), newState) == statesVisited.end())
				{
					Node* newSucessor = (Node*) malloc(sizeof(Node));
					newSucessor->parent = parentNode;
					newSucessor->missionaries = newState->missionaries;
					newSucessor->canibals = newState->canibals;
					newSucessor->boat = newState->boat;
					nodes.push(newSucessor);
				}
			}
		}
	}
}
Esempio n. 7
0
File: DFA.cpp Progetto: aliench0/DFA
DFA::DFA(int _startState, int* _finalStates, int _countOfFinalStates,
		std::map<int, std::map<char, int> > _table) {
	int k = 0;
	for (int i = 97; i <= 122; i++)
		alphabet[k++] = (char) i;
	for (int i = 48; i <= 57; i++)
		alphabet[k++] = (char) i;
	alphabet[k] = '\0';
	countOfAlphabet = k;
	table = _table;
	countOfFinalStates = _countOfFinalStates;
	countOfStates = getCountOfStates();
	states = new int[countOfStates];
	states = getStates();
	finalStates = new int[countOfFinalStates];
	std::sort(_finalStates, _finalStates + countOfFinalStates);
	for (int i = 0; i < countOfFinalStates; i++)
		finalStates[i] = _finalStates[i];
	if (isValidState(_startState))
		startState = _startState;
	else
		throw "Error: start state is not valid.";
}
Esempio n. 8
0
int main(int argc, char * * argv)
{
    if (argc < 3)
	{
	    fprintf(stderr, "Invalid arguments, please refer to "
		    "the README file, or read the source for "
		    "pa11.c\n");
	    return EXIT_FAILURE;
	}
    char * state = argv[2];
    if(!isValidState(state))
	{
	    fprintf(stderr, "Invalid state: %s\n", state);
	    return EXIT_FAILURE;
	}
    if (strcmp(argv[1], "1") == 0)
	{
	    if (argc < 4)
		{
		    fprintf(stderr, "Invalid arguments for cmd=1\n");
		    return EXIT_FAILURE;
		}
	    const char * movelist = argv[3];
	    if(!isValidMoveList(movelist))
		{
		    fprintf(stderr, "Invalid movelist: %s\n", 
			    movelist);
		    return EXIT_FAILURE;
		}
	    processMoveList(state, movelist);
	}
    if (strcmp(argv[1], "2") == 0)
	{
	    if (argc < 4)
		{		    
		    fprintf(stderr, "Invalid arguments for cmd=2\n");
		    return EXIT_FAILURE;
		}
	    int nummove = (int) strtol(argv[3], NULL, 10);
	    if ((nummove < 0) || (nummove > 9))
		{
		    fprintf(stderr, "Expected n=[0..9], but got %d\n",
			    nummove);
		    return EXIT_FAILURE;
		}
	    MoveTree * tree = generateAll(state, nummove);
	    MoveTree_print(tree);
	    MoveTree_destroy(tree);
	}
    if (strcmp(argv[1], "3") == 0)
	{
	    char * movelist = solve(state);
	    if(movelist == NULL) {
		printf("No solution within %d steps\n", 
		       MAX_SEARCH_DEPTH);
	    } else {
		printf("%s\n", movelist);
	    }
	    free(movelist);
	}
    return EXIT_SUCCESS;
}
Esempio n. 9
0
void RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
    if (msg->what() == kWhatDisconnect) {
        uint32_t replyID;
        CHECK(msg->senderAwaitsResponse(&replyID));

        mDisconnectReplyID = replyID;
        finishDisconnectIfPossible();
        return;
    } else if (msg->what() == kWhatPerformSeek) {
        int32_t generation;
        CHECK(msg->findInt32("generation", &generation));

        if (generation != mSeekGeneration) {
            // obsolete.
            return;
        }

        int64_t seekTimeUs;
        CHECK(msg->findInt64("timeUs", &seekTimeUs));

        performSeek(seekTimeUs);
        return;
    } else if (msg->what() == kWhatPerformPlay) {
        int64_t playTimeUs;
        CHECK(msg->findInt64("timeUs", &playTimeUs));
        performPlay(playTimeUs);
        return;
    } else if (msg->what() == kWhatPerformPause) {
        performPause();
        return;
    } else if (msg->what() == kWhatPerformResume) {
        performResume();
        return;
    } else if (msg->what() == kWhatPerformSuspend) {
        performSuspend();
        return;
    } else if (msg->what() == kWhatPerformPlaybackEnded) {
        performPlaybackEnded();
        return;
    }

    CHECK_EQ(msg->what(), (uint32_t)kWhatNotify);

    int32_t what;
    int32_t isSeekable = 0;
    CHECK(msg->findInt32("what", &what));

    switch (what) {
        case RtspConnectionHandler::kWhatConnected:
            CHECK(msg->findInt32("isSeekable", &isSeekable));
            onConnected((isSeekable ? true:false));
            break;

        case RtspConnectionHandler::kWhatDisconnected:
            onDisconnected(msg);
            break;

        case RtspConnectionHandler::kWhatSeekDone:
        {
            mState = PLAYING;
            // Even if we have reset mLatestPausedUnit in performSeek(),
            // it's still possible that kWhatPausedDone event may arrive
            // because of previous performPause() command.
            for (size_t i = 0; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                info->mLatestPausedUnit = 0;
            }
            mLatestPausedUnit = 0;
            break;
        }

        case RtspConnectionHandler::kWhatPausedDone:
        {
            // Reject invalid state transition.
            if (mState != PAUSING) {
                return;
            }
            mState = PAUSED;

            for (size_t i = 0; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                info->mLatestPausedUnit = info->mLatestReceivedUnit;
            }

            // The timestamp after a 'Pause' is done is the earliest
            // timestamp among all of the latest received units.
            TrackInfo *info = &mTracks.editItemAt(0);
            mLatestPausedUnit = info->mLatestReceivedUnit;
            for (size_t i = 1; i < mTracks.size(); ++i) {
                TrackInfo *info = &mTracks.editItemAt(i);
                if (mLatestPausedUnit > info->mLatestReceivedUnit) {
                    mLatestPausedUnit = info->mLatestReceivedUnit;
                }
            }

            if (mPlayPending) {
                mPlayPending = false;
                performPlay(mLatestPausedUnit);
            }
            break;
        }

        case RtspConnectionHandler::kWhatAccessUnitComplete:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping access unit.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            sp<RefBase> obj;
            CHECK(msg->findObject("accessUnit", &obj));

            sp<ABuffer> accessUnit = static_cast<ABuffer *>(obj.get());

            int32_t damaged;
            if (accessUnit->meta()->findInt32("damaged", &damaged)
                    && damaged) {
                LOGI("dropping damaged access unit.");
                break;
            }

            TrackInfo *info = &mTracks.editItemAt(trackIndex);

            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                uint32_t rtpTime;
                CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));

                if (!info->mNPTMappingValid) {
                    // This is a live stream, we didn't receive any normal
                    // playtime mapping. Assume the first packets correspond
                    // to time 0.

                    LOGV("This is a live stream, assuming time = 0");

                    info->mRTPTime = rtpTime;
                    info->mNormalPlaytimeUs = 0ll;
                    info->mNPTMappingValid = true;
                }

                int64_t nptUs =
                    ((double)rtpTime - (double)info->mRTPTime)
                        / info->mTimeScale
                        * 1000000ll
                        + info->mNormalPlaytimeUs;

                accessUnit->meta()->setInt64("timeUs", nptUs);
                info->mLatestReceivedUnit = nptUs;
                // Drop the frames that are older than the frames in the queue.
                if (info->mLatestPausedUnit && (int64_t)info->mLatestPausedUnit > nptUs) {
                    break;
                }
                source->queueAccessUnit(accessUnit);
            }

            onTrackDataAvailable(trackIndex);
            break;
        }

        case RtspConnectionHandler::kWhatEOS:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping end-of-stream message.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            int32_t finalResult;
            CHECK(msg->findInt32("finalResult", &finalResult));
            CHECK_NE(finalResult, (status_t)OK);

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
                source->signalEOS(finalResult);
            }

            onTrackEndOfStream(trackIndex);
            break;
        }

        case RtspConnectionHandler::kWhatSeekDiscontinuity:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping seek discontinuity message.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            sp<AnotherPacketSource> source = info->mSource;
            if (source != NULL) {
#if ANDROID_VERSION >= 21
                source->queueDiscontinuity(ATSParser::DISCONTINUITY_TIME, NULL,
                                           true /* discard */);
#else
                source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL);
#endif
            }

            break;
        }

        case RtspConnectionHandler::kWhatNormalPlayTimeMapping:
        {
            if (!isValidState()) {
                LOGI("We're disconnected, dropping normal play time mapping "
                     "message.");
                break;
            }
            size_t trackIndex;
            CHECK(msg->findSize("trackIndex", &trackIndex));
            CHECK_LT(trackIndex, mTracks.size());

            uint32_t rtpTime;
            CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));

            int64_t nptUs;
            CHECK(msg->findInt64("nptUs", &nptUs));

            TrackInfo *info = &mTracks.editItemAt(trackIndex);
            info->mRTPTime = rtpTime;
            info->mNormalPlaytimeUs = nptUs;
            info->mNPTMappingValid = true;
            break;
        }

        case RtspConnectionHandler::kWhatTryTCPInterleaving:
        {
            // By default, we will request to deliver RTP over UDP. If the play
            // request timed out and we didn't receive any RTP packet, we will
            // fail back to use RTP interleaved in the existing RTSP/TCP
            // connection. And in this case, we have to explicitly perform
            // another play event to request the server to start streaming
            // again.
            int64_t playTimeUs;
            if (!msg->findInt64("timeUs", &playTimeUs)) {
                playTimeUs = 0;
            }
            performPlay(playTimeUs);
            break;
        }

        default:
            TRESPASS();
    }
}