FoundPoint percolate (const int size, ThreadPool& pool) {

	std::vector<std::future<FoundPoint>> points;
	FoundPoint max = FoundPoint();
	int numThreads = pool.getSize();
	int numOpThreadM = size/ numThreads;
	int numOpThreadR = size % numThreads;
	bool end = false;  

	for (int i = 0; i < numThreads; ++i) {
            
            int startIndex = numOpThreadM * i;
            int lastIndex = numOpThreadM * (i + 1);

            if ((i + 1 == numThreads && numOpThreadR > 0) 
            	|| numOpThreadM == 0) {
             	lastIndex += numOpThreadR;
                end = true;
            }    

            points.emplace_back(pool.enqueue_return(calc, startIndex, 
            	lastIndex, size));
            
            if (end) break;
    }

    for (auto& f: points) {
    	FoundPoint tmpFP = f.get();
    	if (tmpFP.value < max.value){
    		max = tmpFP;
    	}
    }

	return max;
}
Пример #2
0
BOOL KPathFinder::FindPath(KCellEx* pSrc, KCellEx* pDest, KLIST_AUTOPATH& lstAutoPath, BOOL bAllObstacle)
{
	BOOL bResult  = false;
	BOOL bRetCode = false;
    KCellEx* pCurrent = NULL;
    KCellEx* pSurround = NULL;
	
	m_setOpenPoint.clear();
    memset(m_pAllWayFindingData, 0, sizeof(KWayFindingData) * m_nXLength * m_nYLength);

    m_setOpenPoint.insert(pSrc);
    pSrc->pWayFindingData->bOpenPoint = true;

    while (m_setOpenPoint.size())
    {
        pCurrent = *m_setOpenPoint.begin();
        m_setOpenPoint.erase(m_setOpenPoint.begin());
        pCurrent->pWayFindingData->bOpenPoint = false;

        pCurrent->pWayFindingData->bClosePoint = true;

        UpdateSurroundPoints(pCurrent, bAllObstacle);
        for (KVEC_SURROUND_POINT::iterator it = m_vecSurroundPoint.begin(); it != m_vecSurroundPoint.end(); ++it)
        {
            pSurround = *it;
            if (pSurround->pWayFindingData->bOpenPoint)
            {
                FoundPoint(pCurrent, pSurround);
            }
            else
            {
                NotFoundPoint(pCurrent, pDest, pSurround);
            }
        }

        if (pDest->pWayFindingData->bOpenPoint)
        {
            MakePath(pDest, lstAutoPath);
            bResult = true;
            break;
        }
    }

	return bResult;
}
FoundPoint calc (const int startIndex, const int lastIndex, const int size) {

	FoundPoint found = FoundPoint();
	for (int i = startIndex; i < lastIndex; ++i) {
		for (int j = 1; j < size - 1; ++j) {
			if (mask[i*size + j] == 1 && (i > 0 && i < size - 1)) {
				for (int sides = 0; sides < N_SIDES; ++sides) {
					int row = i + X_STEPS[sides];
					int col = j + Y_STEPS[sides];
					int pos = row*size + col;
					if (mask[pos] == 0 && matrix[pos] < found.value) {
						found.row = row;
						found.col = col;
						found.value = matrix[pos]; 
					}
				}
			}
		}
	}

	return found;

}