int findLocal(vector<int>& nums, int leftidx, int rightidx) { if (leftidx==rightidx) return leftidx; int mid=(leftidx+rightidx)/2; if (nums[mid]>nums[mid+1]) return findLocal(nums,leftidx,mid); else return findLocal(nums,mid+1,rightidx); }
Symbol * SymbolTable::findInClass(std::string key) { Symbol *sym = NULL; if ((sym = findLocal(key)) != NULL) { return sym; } if ((sym = findSuper(key)) != NULL) { return sym; } return NULL; }
void Resolver::visit(NameLValue& lvalue, int dummy) { // Look up the variable in this procedure. gc<ResolvedName> resolved = findLocal(lvalue.name()); // TODO(bob): Report error if variable is immutable. if (!resolved.isNull()) { lvalue.setResolved(resolved); return; } // See if it's a closure. int closure = resolveClosure(lvalue.name()); if (closure != -1) { // TODO(bob): Report error if variable is immutable. gc<ResolvedName> resolved = new ResolvedName(-1); resolved->makeClosure(closure); lvalue.setResolved(resolved); return; } // Not a local variable. See if it's a top-level one. int module = compiler_.getModuleIndex(module_); int index = module_.findVariable(lvalue.name()); if (index != -1) { lvalue.setResolved(new ResolvedName(module, index)); return; } compiler_.reporter().error(lvalue.pos(), "Variable '%s' is not defined.", lvalue.name()->cString()); // Put a fake slot in so we can continue and report more errors. lvalue.setResolved(new ResolvedName(0)); }
void Resolver::visit(NameExpr& expr, int dummy) { // See if it's defined in this scope. gc<ResolvedName> local = findLocal(expr.name()); if (!local.isNull()) { expr.setResolved(local); return; } // See if it's a closure. int closure = resolveClosure(expr.name()); if (closure != -1) { gc<ResolvedName> resolved = new ResolvedName(-1); resolved->makeClosure(closure); expr.setResolved(resolved); return; } // See if it's a top-level name in this module. if (resolveTopLevelName(module_, expr)) return; // See if it's an imported name. Walk through the modules this one imports. // TODO(bob): Need to handle name collisions. for (int i = 0; i < module_.imports().count(); i++) { Module* import = module_.imports()[i]; if (resolveTopLevelName(*import, expr)) return; } compiler_.reporter().error(expr.pos(), "Variable '%s' is not defined.", expr.name()->cString()); // Resolve it to some fake local so compilation can continue and report // more errors. expr.setResolved(new ResolvedName(0)); }
void ServiceManager::receive(Message* msg) { RScopeLock lock(_mutex); // is this a response for one of our requests? if (msg->getMeta().find("um.rpc.respId") != msg->getMeta().end()) { std::string respId = msg->getMeta("um.rpc.respId"); if (_findRequests.find(respId) != _findRequests.end()) { // put message into responses and signal waiting thread _findResponses[respId] = new Message(*msg); _findRequests[respId]->signal(); } } // is someone simply asking for a service via find? if (msg->getMeta().find("um.rpc.type") != msg->getMeta().end() && msg->getMeta("um.rpc.type").compare("discover") == 0) { ServiceFilter filter(msg); std::set<ServiceDescription> foundSvcs = findLocal(filter); if (foundSvcs.size() > 0) { ServiceDescription svcDesc = (*(foundSvcs.begin())); Message* foundMsg = svcDesc.toMessage(); foundMsg->setReceiver(msg->getMeta("um.rpc.mgrId")); foundMsg->putMeta("um.rpc.respId", msg->getMeta("um.rpc.reqId")); foundMsg->putMeta("um.rpc.mgrId", _svcSub->getUUID()); // if (_svcPub->isPublishingTo(msg->getMeta("um.rpc.mgrId"))) { _svcPub->send(foundMsg); // } else { // // queue message and send in welcome // _pendingMessages[msg->getMeta("um.rpc.mgrId")].push_back(std::make_pair(Thread::getTimeStampMs(), foundMsg)); // } delete foundMsg; } } // is this the start of a continuous query? if (msg->getMeta().find("um.rpc.type") != msg->getMeta().end() && msg->getMeta("um.rpc.type").compare("startDiscovery") == 0) { ServiceFilter filter(msg); _remoteQueries[filter.getUUID()] = filter; UM_LOG_INFO("Received query for '%s'", filter._svcName.c_str()); // do we have such a service? std::set<ServiceDescription> foundSvcs = findLocal(filter); std::set<ServiceDescription>::iterator svcDescIter = foundSvcs.begin(); while(svcDescIter != foundSvcs.end()) { Message* foundMsg = svcDescIter->toMessage(); foundMsg->setReceiver(msg->getMeta("um.rpc.mgrId")); foundMsg->putMeta("um.rpc.filterId", filter.getUUID()); foundMsg->putMeta("um.rpc.type", "discovered"); foundMsg->putMeta("um.rpc.mgrId", _svcSub->getUUID()); _svcPub->send(foundMsg); delete foundMsg; svcDescIter++; } } // is this the end of a continuous query? if (msg->getMeta().find("um.rpc.type") != msg->getMeta().end() && msg->getMeta("um.rpc.type").compare("stopDiscovery") == 0) { ServiceFilter filter(msg); if (_remoteQueries.find(filter.getUUID()) != _remoteQueries.end()) { _remoteQueries.erase(filter.getUUID()); } } // is this a reply to a continuous service query? if (msg->getMeta().find("um.rpc.type") != msg->getMeta().end() && (msg->getMeta("um.rpc.type").compare("discovered") == 0 || msg->getMeta("um.rpc.type").compare("vanished") == 0)) { // _svcQueries comparator uses filter uuid ServiceFilter keyFilter(""); keyFilter._uuid = msg->getMeta("um.rpc.filterId"); if (_localQueries.find(keyFilter) != _localQueries.end()) { ResultSet<ServiceDescription>* listener = _localQueries[keyFilter]; assert(msg->getMeta("um.rpc.desc.channel").size() > 0); assert(msg->getMeta("um.rpc.mgrId").size() > 0); std::string svcChannel = msg->getMeta("um.rpc.desc.channel"); std::string managerId = msg->getMeta("um.rpc.mgrId"); if (_remoteSvcDesc.find(managerId) == _remoteSvcDesc.end() || _remoteSvcDesc[managerId].find(svcChannel) == _remoteSvcDesc[managerId].end()) { _remoteSvcDesc[managerId][svcChannel] = ServiceDescription(msg); _remoteSvcDesc[managerId][svcChannel]._svcManager = this; } assert(_remoteSvcDesc.find(managerId) != _remoteSvcDesc.end()); assert(_remoteSvcDesc[managerId].find(svcChannel) != _remoteSvcDesc[managerId].end()); if (msg->getMeta("um.rpc.type").compare("discovered") == 0) { listener->added(_remoteSvcDesc[managerId][svcChannel]); } else { listener->removed(_remoteSvcDesc[managerId][svcChannel]); _remoteSvcDesc[managerId].erase(svcChannel); } } } }
void ServiceManager::receive(Message* msg) { ScopeLock lock(&_mutex); // is this a response for one of our requests? if (msg->getMeta().find("respId") != msg->getMeta().end()) { string respId = msg->getMeta("respId"); if (_findRequests.find(respId) != _findRequests.end()) { _findResponses[respId] = new Message(*msg); _findRequests[respId].signal(); } } // is someone asking for a service? if (msg->getMeta().find("type") != msg->getMeta().end() && msg->getMeta("type").compare("serviceDisc") == 0) { ServiceFilter* filter = new ServiceFilter(msg); std::set<ServiceDescription*> foundSvcs = findLocal(filter); delete filter; if (foundSvcs.size() > 0) { ServiceDescription* svcDesc = (*(foundSvcs.begin())); Message* foundMsg = svcDesc->toMessage(); foundMsg->setMeta("respId", msg->getMeta("reqId")); foundMsg->setMeta("desc:channel", svcDesc->getChannelName()); _svcPub->send(foundMsg); delete foundMsg; } } // is this the start of a continuous query? if (msg->getMeta().find("type") != msg->getMeta().end() && msg->getMeta("type").compare("serviceDiscStart") == 0) { ServiceFilter* filter = new ServiceFilter(msg); _remoteQueries[filter->_uuid] = filter; // do we have such a service? std::set<ServiceDescription*> foundSvcs = findLocal(filter); std::set<ServiceDescription*>::iterator svcDescIter = foundSvcs.begin(); while(svcDescIter != foundSvcs.end()) { if (filter->matches(*svcDescIter)) { Message* foundMsg = (*svcDescIter)->toMessage(); foundMsg->setMeta("filterId", filter->_uuid); foundMsg->setMeta("type", "serviceDiscFound"); foundMsg->setMeta("desc:channel", (*svcDescIter)->getChannelName()); _svcPub->send(foundMsg); delete foundMsg; } svcDescIter++; } } // is this the end of a continuous query? if (msg->getMeta().find("type") != msg->getMeta().end() && msg->getMeta("type").compare("serviceDiscStop") == 0) { ServiceFilter* filter = new ServiceFilter(msg); if (_remoteQueries.find(filter->_uuid) != _remoteQueries.end()) { delete _remoteQueries[filter->_uuid]; _remoteQueries.erase(filter->_uuid); } delete filter; } // is this a reply to a continuous service query? if (msg->getMeta().find("type") != msg->getMeta().end() && (msg->getMeta("type").compare("serviceDiscFound") == 0 || msg->getMeta("type").compare("serviceDiscRemoved") == 0)) { // _svcQueries comparator uses filter uuid ServiceFilter* keyFilter = new ServiceFilter(""); keyFilter->_uuid = msg->getMeta("filterId"); if (_localQueries.find(keyFilter) != _localQueries.end()) { ResultSet<ServiceDescription>* listener = _localQueries[keyFilter]; assert(msg->getMeta("desc:channel").size() > 0); if (_remoteSvcDesc.find(msg->getMeta("desc:channel")) == _remoteSvcDesc.end()) _remoteSvcDesc[msg->getMeta("desc:channel")] = shared_ptr<ServiceDescription>(new ServiceDescription(msg)); if (msg->getMeta("type").compare("serviceDiscFound") == 0) { listener->added(_remoteSvcDesc[msg->getMeta("desc:channel")]); } else { listener->removed(_remoteSvcDesc[msg->getMeta("desc:channel")]); _remoteSvcDesc.erase(msg->getMeta("desc:channel")); } } delete keyFilter; } }
int32_t TZGNCore::findBestMatch(const UnicodeString& text, int32_t start, uint32_t types, UnicodeString& tzID, UTimeZoneFormatTimeType& timeType, UErrorCode& status) const { timeType = UTZFMT_TIME_TYPE_UNKNOWN; tzID.setToBogus(); if (U_FAILURE(status)) { return 0; } // Find matches in the TimeZoneNames first TimeZoneNames::MatchInfoCollection *tznamesMatches = findTimeZoneNames(text, start, types, status); if (U_FAILURE(status)) { return 0; } int32_t bestMatchLen = 0; UTimeZoneFormatTimeType bestMatchTimeType = UTZFMT_TIME_TYPE_UNKNOWN; UnicodeString bestMatchTzID; // UBool isLongStandard = FALSE; // workaround - see the comments below UBool isStandard = FALSE; // TODO: Temporary hack (on hack) for short standard name/location name conflict (found in zh_Hant), should be removed after CLDR 21m1 integration if (tznamesMatches != NULL) { UnicodeString mzID; for (int32_t i = 0; i < tznamesMatches->size(); i++) { int32_t len = tznamesMatches->getMatchLengthAt(i); if (len > bestMatchLen) { bestMatchLen = len; if (!tznamesMatches->getTimeZoneIDAt(i, bestMatchTzID)) { // name for a meta zone if (tznamesMatches->getMetaZoneIDAt(i, mzID)) { fTimeZoneNames->getReferenceZoneID(mzID, fTargetRegion, bestMatchTzID); } } UTimeZoneNameType nameType = tznamesMatches->getNameTypeAt(i); if (U_FAILURE(status)) { break; } switch (nameType) { case UTZNM_LONG_STANDARD: // isLongStandard = TRUE; case UTZNM_SHORT_STANDARD: // this one is never used for generic, but just in case isStandard = TRUE; // TODO: Remove this later, see the comments above. bestMatchTimeType = UTZFMT_TIME_TYPE_STANDARD; break; case UTZNM_LONG_DAYLIGHT: case UTZNM_SHORT_DAYLIGHT: // this one is never used for generic, but just in case bestMatchTimeType = UTZFMT_TIME_TYPE_DAYLIGHT; break; default: bestMatchTimeType = UTZFMT_TIME_TYPE_UNKNOWN; } } } delete tznamesMatches; if (U_FAILURE(status)) { return 0; } if (bestMatchLen == (text.length() - start)) { // Full match //tzID.setTo(bestMatchTzID); //timeType = bestMatchTimeType; //return bestMatchLen; // TODO Some time zone uses a same name for the long standard name // and the location name. When the match is a long standard name, // then we need to check if the name is same with the location name. // This is probably a data error or a design bug. /* if (!isLongStandard) { tzID.setTo(bestMatchTzID); timeType = bestMatchTimeType; return bestMatchLen; } */ // TODO The deprecation of commonlyUsed flag introduced the name // conflict not only for long standard names, but short standard names too. // These short names (found in zh_Hant) should be gone once we clean // up CLDR time zone display name data. Once the short name conflict // problem (with location name) is resolved, we should change the condition // below back to the original one above. -Yoshito (2011-09-14) if (!isStandard) { tzID.setTo(bestMatchTzID); timeType = bestMatchTimeType; return bestMatchLen; } } } // Find matches in the local trie TimeZoneGenericNameMatchInfo *localMatches = findLocal(text, start, types, status); if (U_FAILURE(status)) { return 0; } if (localMatches != NULL) { for (int32_t i = 0; i < localMatches->size(); i++) { int32_t len = localMatches->getMatchLength(i); // TODO See the above TODO. We use len >= bestMatchLen // because of the long standard/location name collision // problem. If it is also a location name, carrying // timeType = UTZFMT_TIME_TYPE_STANDARD will cause a // problem in SimpleDateFormat if (len >= bestMatchLen) { bestMatchLen = localMatches->getMatchLength(i); bestMatchTimeType = UTZFMT_TIME_TYPE_UNKNOWN; // because generic localMatches->getTimeZoneID(i, bestMatchTzID); } } delete localMatches; } if (bestMatchLen > 0) { timeType = bestMatchTimeType; tzID.setTo(bestMatchTzID); } return bestMatchLen; }
Symbol * SymbolTable::findLocal(std::string key, TType* type) { Symbol *s = findLocal(key); return (s && s->getTType() == type) ? s : NULL; }
int findPeakElement(vector<int>& nums) { // O(logn) -> D&C? tree? // find max? return findLocal(nums,0,nums.size()-1); }