Example #1
0
bool FSMDirector_DirectorTransferModalOutputs(struct FSMDirector* director) {
    struct CompositeActor* container = director->container;
    PblIterator* outports = pblIteratorNew(container->outputPortList(container));
    while (pblIteratorHasNext(outports)) {
        struct IOPort* port = pblIteratorNext(outports);
        director->directorTransferModalOutputs1(director, port);
    }
    pblIteratorFree(outports);
    return true;
}
/**
 * Aggregates a collection by calling the aggregation function on every element
 * of the collection while running through the collection with an iterator.
 *
 * The application context supplied is passed to the aggregation function
 * for each element.
 *
 * If the aggregation function for an element returns a value > 0,
 * the aggregation is terminated immediately and the return value of
 * the aggregation function is given back to the caller.
 *
 * Otherwise the aggregation is continued to the next element
 * of the collection until the iterator reaches its end.
 *
 * This method has a time complexity of O(N),
 * with N being the size of the collection.
 *
 * @return int rc  > 0: The aggregation was terminated with the return code of the aggregation function.
 * @return int rc == 0: The aggregation succeeded, all elements where aggregated.
 * @return int rc  < 0: An error, see pbl_errno:
 *
 * <BR>PBL_ERROR_PARAM_COLLECTION        - The collection cannot be iterated.
 * <BR>PBL_ERROR_CONCURRENT_MODIFICATION - The collection was modified concurrently.
 */
int pblCollectionAggregate(
PblCollection * collection,  /** The collection to aggregate.                                 */
void * context,              /** The application context to pass to the aggregation function. */
int ( *aggregation )         /** The aggregation function called on every collection element. */
    (
        void * context,      /** The application context passed.                              */
        int index,           /** The index of the element passed.                             */
        void * element       /** The collection element to aggregate.                         */
    )
)
{
    PblIterator iteratorBuffer;
    PblIterator * iterator = (PblIterator *)&iteratorBuffer;
    int rc = 0;
    int hasNext;
    int index = 0;
    void * element;

    /*
     * Get the iterator for this collection
     */
    if( pblIteratorInit( collection, &iteratorBuffer ) < 0 )
    {
        return -1;
    }

    while( ( hasNext = pblIteratorHasNext( iterator ) ) > 0 )
    {
        element = pblIteratorNext( iterator );
        if( element == (void*)-1 )
        {
            // Concurrent modification
            //
            return -1;
        }

        /*
         * Call the aggregation function on the element
         */
        rc = ( *( aggregation ) )( context, index++, element );
        if( rc > 0 )
        {
            return rc;
        }

    }
    if( hasNext < 0 )
    {
        // Concurrent modification
        //
        return -1;
    }

    return 0;
}
struct Director* PtidesPlatformDirector__GetEmbeddedPtidesDirector(struct PtidesPlatformDirector* director) {
    struct CompositeActor* container = director->container;

    PblIterator* actors = pblIteratorNew(container->_containedEntities);
    while (pblIteratorHasNext(actors)) {
        struct CompositeActor* actor = (struct CompositeActor*) pblIteratorNext(actors);
        pblIteratorFree(actors);
        return actor->getDirector(actor);
    }
    pblIteratorFree(actors);
    return NULL;
}
/**
 * Returns true if this map contains a mapping for the specified value.
 *
 * This method has a time complexity of O(N).
 *
 * @return int rc >  0: The map contains a mapping for the specified value.
 * @return int rc == 0: The map did not contain a mapping for the value.
 * @return int rc <  0:  An error, see pbl_errno:
 *
 * <BR>PBL_ERROR_CONCURRENT_MODIFICATION - The underlying collection was modified concurrently.
 */
int pblMapContainsValue( /*                                                  */
PblMap * map, /**           The map to check                                 */
void * value, /**           Value whose presence in this map is to be tested */
size_t valueLength /**      Length of the value                              */
)
{
    int hasNext;
    void * element;
    PblMapEntry * entry;

    PblIterator iterator;
    pblIteratorInit( map->entrySet, &iterator );

    while( ( hasNext = pblIteratorHasNext( &iterator ) ) > 0 )
    {
        element = pblIteratorNext( &iterator );
        if( element == (void*)-1 )
        {
            // Concurrent modification
            //
            return -1;
        }

        entry = (PblMapEntry *)element;
        if( !entry )
        {
            continue;
        }

        if( entry->valueLength != valueLength )
        {
            continue;
        }

        if( 0 == valueLength )
        {
            return 1;
        }

        if( 0 == memcmp( value, entry->buffer + entry->keyLength, valueLength ) )
        {
            return 1;
        }
    }

    return 0;
}
/**
 * Copies all of the mappings from the specified source map to this map.
 * These mappings will replace any mappings that this map had for any
 * of the keys currently in the specified map.
 *
 * For hash maps this method has a time complexity of O(M).
 * For tree maps this method has a time complexity of O(M * Log N).
 * With M being the number of elements in the source map and
 * N being the number of elements in the target map.
 *
 * @return int rc == 0: Ok.
 * @return int rc <  0:  An error, see pbl_errno:
 *
 * <BR>PBL_ERROR_CONCURRENT_MODIFICATION - The source map was modified concurrently.
 * <BR>PBL_ERROR_OUT_OF_MEMORY - Out of memory.
 */
int pblMapPutAll( /*                                            */
PblMap * map, /**                The map to copy the entries to */
PblMap * sourceMap /**         The map to copy the entries from */
)
{
    int hasNext;
    void * element;
    PblMapEntry * entry;

    PblIterator iterator;
    pblIteratorInit( sourceMap->entrySet, &iterator );

    while( ( hasNext = pblIteratorHasNext( &iterator ) ) > 0 )
    {
        element = pblIteratorNext( &iterator );
        if( element == (void*)-1 )
        {
            // Concurrent modification
            //
            return -1;
        }

        entry = (PblMapEntry *)element;
        if( !entry )
        {
            continue;
        }

        if( pblMapAdd( map, entry->buffer, entry->keyLength, entry->buffer
                + entry->keyLength, entry->valueLength ) < 0 )
        {
            return -1;
        }
    }

    return 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;
}