Esempio n. 1
0
void printLocalMax(const int *arr, size_t len, size_t window) {
  MaxHeap<ValueWrapper> h;
  for (int i = 0; i < window - 1; ++i) {
    h.add(ValueWrapper(arr[i], i));
  }
  for (int i = window - 1; i < len; ++i) {
    h.add(ValueWrapper(arr[i], i));
    for(;;) {
      ValueWrapper *r = h.top();
      if (i - r->pos < window) {
	cout << r->value << endl;
	break;
      }
      h.pop();
    }
  }
}
Esempio n. 2
0
ValueWrapper SceneManagerCallback_script( const char* symName, const char* method,
                                          boost::python::list& argList, int cascadeEvents )
{
    //int i;
    std::string    theMethod( method );
    cppintrospection::ValueList theArgs;
    //printf("SceneManagerCallback_script: hi! %s, %s, [%s]\n", symName, types, args.c_str());
    t_symbol *s = gensym( symName );

    if (!s->s_thing)
    {
        //std::cout << "oscParser: Could not find referenced object named: " << symName << std::endl;
        return ValueWrapper(0);
    }

    // get osgInrospection::Value from passed UserData by casting as the proper
    // referenced object pointer:
    cppintrospection::Value classInstance;
    if (s->s_type == REFERENCED_STATESET)
        classInstance = cppintrospection::Value(dynamic_cast<ReferencedStateSet*>(s->s_thing));
    else
        classInstance = cppintrospection::Value(dynamic_cast<ReferencedNode*>(s->s_thing));

    // the getInstanceType() method however, gives us the real type being pointed at:
    const cppintrospection::Type &classType = classInstance.getInstanceType();

    if (! classType.isDefined())
    {
        std::cout << "ERROR: oscParser cound not process message '" << symName
                  << ". cppintrospection has no data for that node." << std::endl;
        return ValueWrapper(0);
    }

    size_t nbArgs = boost::python::len( argList );
    double da; float fa; int ia; std::string sa;

    for ( size_t i = 0; i < nbArgs; i++ )
    {

        if ( pyextract( argList[i], &da ) )
            theArgs.push_back( (double) da );
        else if ( pyextract( argList[i], &fa ) )
            theArgs.push_back( (float) fa );
        else if ( pyextract( argList[i], &ia ) )
            theArgs.push_back( (int) ia );
        else
        {
            sa = pyextract( argList[i] );
            if (sa != "" )
                theArgs.push_back( (const char*) sa.c_str() );
        }
    }

    // invoke the method on the node, and if it doesn't work, then just forward
    // the message:
    cppintrospection::Value v;
    bool eventScriptCalled = false;
    if (cascadeEvents)
    {
        if (s->s_type == REFERENCED_NODE)
        {
            //printf("calling eventscript...\n");
            eventScriptCalled = dynamic_cast<ReferencedNode*>(s->s_thing)->callEventScript( theMethod, theArgs );
        }
    }

    if (eventScriptCalled)
    { // cascaded event script called successful, do not invokeMethod, return nothing to the python script
        return ValueWrapper(0);
    }
    else
    { // no cascaded event assigned to theMethod or cascaded event script failed.  call invokeMethod and return result to python script
        if (invokeMethod(classInstance, classType, theMethod, theArgs, v))
        {
            return ValueWrapper(v);
        }
        else
        {
            if (spinApp::Instance().getContext()->isServer())
            {
                lo_message msg = lo_message_new();
                lo_message_add_string(msg, theMethod.c_str());
                for ( size_t i = 0; i < nbArgs; i++ )
                {
                    if ( pyextract( argList[i], &da ) ) lo_message_add_float(msg, (float) da );
                    else if ( pyextract( argList[i], &fa ) ) lo_message_add_float(msg, (float) fa );
                    else if ( pyextract( argList[i], &ia ) ) lo_message_add_float(msg, (float) ia );
                    else
                    {
                        sa = pyextract( argList[i] );
                        if (sa != "" ) lo_message_add_string(msg, (const char*) sa.c_str() );
                    }
                }
                std::string path = "/SPIN/" + spinApp::Instance().getSceneID() + "/" + s->s_name;
                std::vector<lo_address>::iterator addrIter;
                for (addrIter = spinApp::Instance().getContext()->lo_txAddrs_.begin(); addrIter != spinApp::Instance().getContext()->lo_txAddrs_.end(); ++addrIter)
                {
                    lo_send_message_from((*addrIter), spinApp::Instance().getContext()->lo_infoServ_, path.c_str(), msg);
                }
            }
        }

    }
    //pthread_mutex_unlock(&sceneMutex);
    return ValueWrapper(0);
}