Exemplo n.º 1
0
/**
 * Returns the next element in the iteration.
 *
 * Calling this method repeatedly until the hasNext()
 * method returns false will return each element
 * in the underlying collection exactly once.
 *
 * This method has a time complexity of O(1).
 *
 * @return void * retptr != (void*)-1: The next element in the iteration.
 *                                     May be NULL.
 * @return void * retptr == (void*)-1: An error, see pbl_errno:
 *
 * <BR>PBL_ERROR_NOT_FOUND               - The iteration has no more elements.
 * <BR>PBL_ERROR_CONCURRENT_MODIFICATION - The underlying collection was modified concurrently.
 */
void * pblIteratorNext(
PblIterator * iterator /** The iterator to return the next element for */
)
{
    void * element;
    int hasNext = pblIteratorHasNext( iterator );
    if( hasNext < 0 )
    {
        return (void*)-1;
    }

    if( !hasNext )
    {
        pbl_errno = PBL_ERROR_NOT_FOUND;
        return (void*)-1;
    }

    if( PBL_SET_IS_HASH_SET( iterator->collection ) )
    {
        PblHashIterator * hashIterator = (PblHashIterator *)iterator;

        if( !hashIterator->next )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }

        hashIterator->current = hashIterator->next;
        hashIterator->prev = hashIterator->next;
        hashIterator->next
                = pblHashElementNext( (PblHashSet *)hashIterator->collection,
                                      hashIterator->next );

        element = *( hashIterator->current );
    }
    else if( PBL_SET_IS_TREE_SET( iterator->collection ) )
    {
        PblTreeIterator * treeIterator = (PblTreeIterator *)iterator;

        if( !treeIterator->next )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }

        treeIterator->current = treeIterator->next;
        treeIterator->prev = treeIterator->next;
        treeIterator->next = pblTreeNodeNext( treeIterator->next );

        element = treeIterator->current->element;
    }
    else if( PBL_LIST_IS_LINKED_LIST( iterator->collection ) )
    {
        if( !iterator->next )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }

        iterator->current = iterator->next;
        iterator->prev = iterator->next;
        iterator->next = iterator->next->next;

        element = iterator->current->element;
    }
    else
    {
        element = pblListGet( (PblList*)iterator->collection, iterator->index );
        if( element == (void*)-1 )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }
    }

    iterator->lastIndexReturned = iterator->index;
    iterator->index++;

    return element;
}
Exemplo n.º 2
0
/**
 * Returns the previous element in the iteration.
 *
 * This method may be called repeatedly to iterate through the list backwards,
 * or intermixed with calls to next to go back and forth.
 * (Note that alternating calls to next and previous will return the same element repeatedly.)
 *
 * This method has a time complexity of O(1).
 *
 * @return void * retptr != (void*)-1: The previous element in the iteration.
 *                                     May be NULL.
 * @return void * retptr == (void*)-1: An error, see pbl_errno:
 *
 * <BR>PBL_ERROR_NOT_FOUND               - The iteration has no more elements.
 * <BR>PBL_ERROR_CONCURRENT_MODIFICATION - The underlying collection was modified concurrently.
 */
void * pblIteratorPrevious(
PblIterator * iterator /** The iterator to return the previous element for */
)
{
    void * element;
    int hasPrevious = pblIteratorHasPrevious( iterator );
    if( hasPrevious < 0 )
    {
        return (void*)-1;
    }
    if( !hasPrevious )
    {
        pbl_errno = PBL_ERROR_NOT_FOUND;
        return (void*)-1;
    }

    if( PBL_SET_IS_HASH_SET( iterator->collection ) )
    {
        PblHashIterator * hashIterator = (PblHashIterator *)iterator;

        if( !hashIterator->prev )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }

        hashIterator->current = hashIterator->prev;
        hashIterator->next = hashIterator->prev;
        hashIterator->prev
                = pblHashElementPrevious(
                                          (PblHashSet *)hashIterator->collection,
                                          hashIterator->prev );

        element = *( hashIterator->current );
    }
    else if( PBL_SET_IS_TREE_SET( iterator->collection ) )
    {
        PblTreeIterator * treeIterator = (PblTreeIterator *)iterator;

        if( !treeIterator->prev )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }

        treeIterator->current = treeIterator->prev;
        treeIterator->next = treeIterator->prev;
        treeIterator->prev = pblTreeNodePrevious( treeIterator->prev );

        element = treeIterator->current->element;
    }
    else if( PBL_LIST_IS_LINKED_LIST( iterator->collection ) )
    {
        if( !iterator->prev )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }

        iterator->current = iterator->prev;
        iterator->next = iterator->prev;
        iterator->prev = iterator->prev->prev;

        element = iterator->current->element;
    }
    else
    {
        element = pblListGet( (PblList*)iterator->collection, iterator->index
                - 1 );

        if( element == (void*)-1 )
        {
            pbl_errno = PBL_ERROR_NOT_FOUND;
            return (void*)-1;
        }
    }

    iterator->index--;
    iterator->lastIndexReturned = iterator->index;

    return element;
}
bool PtidesPlatformDirector_TransferInputs(struct PtidesPlatformDirector* director, struct IOPort* port) {
    bool result = false;
    struct PtidesDirector* ptidesDirector = (struct PtidesDirector*) director->_getEmbeddedPtidesDirector(director);

#ifdef _debugging
    fprintf(stderr, "%s:%d: PtidesPlatformDirector_TransferInputs(%p) %s start\n", __FILE__, __LINE__, director, ((struct Director *) director)->getFullName((struct Director *)director));
#endif
    for (int channelIndex = 0; channelIndex < port->getWidth(port); channelIndex++) {
        if (port->hasToken(port, channelIndex)) {
            Token* t = port->get(port, channelIndex);

            struct PtidesPort* associatedPort = ((struct PtidesPort*) port)->_associatedPort;
            if (associatedPort->isNetworkReceiverPort(associatedPort)) {
                RecordToken r = t->payload.Record;
                Time recordTimestamp = r->timestamp;
                int recordMicrostep = r->microstep;
                Time sourceTimestamp = director->getModelTime(director);

                PblList* farReceivers = associatedPort->deepGetReceivers(associatedPort);
                if (pblListSize(farReceivers) >= channelIndex) {
                    PblList* receivers = pblListGet(farReceivers, channelIndex);
                    for (int i = 0; i < pblListSize(receivers); i++) {
                        struct PtidesEvent* newEvent = PtidesEvent_New();
                        newEvent->_ioPort = (struct IOPort*)associatedPort;
                        newEvent->_channel = channelIndex;
                        newEvent->_timestamp = recordTimestamp;
                        newEvent->_microstep = recordMicrostep;
                        newEvent->_depth = -1;
                        newEvent->_token = (r->payload);
                        newEvent->_receiver = pblListGet(receivers, i);
                        newEvent->_sourceTimestamp = sourceTimestamp;
                        ptidesDirector->addInputEvent(ptidesDirector, associatedPort,
                                                      newEvent, associatedPort->deviceDelay);
                    }
                }
            } else if (associatedPort->isSensorPort(associatedPort)) {
                PblList* farReceivers = associatedPort->deepGetReceivers(associatedPort);
                if (pblListSize(farReceivers) >= channelIndex) {
                    PblList* receivers = pblListGet(farReceivers, channelIndex);
                    for (int i = 0; i < pblListSize(receivers); i++) {
                        struct PtidesEvent* newEvent = PtidesEvent_New();
                        newEvent->_ioPort = (struct IOPort*)associatedPort;
                        newEvent->_channel = channelIndex;
                        newEvent->_timestamp = ptidesDirector->getModelTime(ptidesDirector);
                        newEvent->_microstep = 1;
                        newEvent->_depth = -1;
                        newEvent->_token = t;
                        newEvent->_receiver = pblListGet(receivers, i);
                        newEvent->_sourceTimestamp = ptidesDirector->getModelTime(ptidesDirector);
                        ptidesDirector->addInputEvent(ptidesDirector, associatedPort,
                                                      newEvent, associatedPort->deviceDelay);
                    }
                }
            } else {
                associatedPort->sendInside(associatedPort, channelIndex, t);
            }
            result = true;
        }
    }

#ifdef _debugging
    fprintf(stderr, "%s:%d: PtidesPlatformDirector_TransferInputs(%p) %s end\n", __FILE__, __LINE__, director,  ((struct Director *) director)->getFullName((struct Director *)director));
#endif
    return result;
}