Disposable<Array> qrSolve(const Matrix& a, const Array& b, bool pivot, const Array& d) { const Size m = a.rows(); const Size n = a.columns(); QL_REQUIRE(b.size() == m, "dimensions of A and b don't match"); QL_REQUIRE(d.size() == n || d.empty(), "dimensions of A and d don't match"); Matrix q(m, n), r(n, n); std::vector<Size> lipvt = qrDecomposition(a, q, r, pivot); boost::scoped_array<int> ipvt(new int[n]); std::copy(lipvt.begin(), lipvt.end(), ipvt.get()); Matrix rT = transpose(r); boost::scoped_array<Real> sdiag(new Real[n]); boost::scoped_array<Real> wa(new Real[n]); Array ld(n, 0.0); if (!d.empty()) { std::copy(d.begin(), d.end(), ld.begin()); } Array x(n); Array qtb = transpose(q)*b; MINPACK::qrsolv(n, rT.begin(), n, ipvt.get(), ld.begin(), qtb.begin(), x.begin(), sdiag.get(), wa.get()); return x; }
Variant f_max(int _argc, CVarRef value, CArrRef _argv /* = null_array */) { Variant ret; if (_argv.empty() && value.is(KindOfArray)) { Array v = value.toArray(); if (!v.empty()) { ssize_t pos = v->iter_begin(); if (pos != ArrayData::invalid_index) { ret = v->getValue(pos); while (true) { pos = v->iter_advance(pos); if (pos == ArrayData::invalid_index) break; Variant tmp = v->getValue(pos); if (more(tmp, ret)) { ret = tmp; } } } } } else { ret = value; if (!_argv.empty()) { for (ssize_t pos = _argv->iter_begin(); pos != ArrayData::invalid_index; pos = _argv->iter_advance(pos)) { Variant tmp = _argv->getValue(pos); if (more(tmp, ret)) { ret = tmp; } } } } return ret; }
FittedBondDiscountCurve::FittingMethod::FittingMethod( bool constrainAtZero, const Array& weights, ext::shared_ptr<OptimizationMethod> optimizationMethod, const Array& l2) : constrainAtZero_(constrainAtZero), weights_(weights), l2_(l2), calculateWeights_(weights.empty()), optimizationMethod_(optimizationMethod) {}
req::ptr<PDOResource> PDODriver::createResource(const String& datasource, const String& username, const String& password, const Array& options) { auto const rsrc = createResourceImpl(); auto const& conn = rsrc->conn(); conn->data_source = datasource.toCppString(); conn->username = username.toCppString(); conn->password = password.toCppString(); if (options.exists(PDO_ATTR_AUTOCOMMIT)) { conn->auto_commit = options[PDO_ATTR_AUTOCOMMIT].toInt64(); } else { conn->auto_commit = 1; } if (!conn->create(options)) { Array err; bool hasError = conn->fetchErr(nullptr, err); if (hasError && !err.empty()) { throw_pdo_exception(uninit_null(), "[%" PRId64 "]: %s", err[0].toInt64(), err[1].toString().data()); } return nullptr; } return rsrc; }
String f_system(CStrRef command, Variant return_var /* = null */) { ShellExecContext ctx; FILE *fp = ctx.exec(command); if (!fp) return ""; StringBuffer sbuf; if (fp) { sbuf.read(fp); } Array lines = StringUtil::Explode(sbuf.detach(), "\n"); return_var = ctx.exit(); int count = lines.size(); if (count > 0 && lines[count - 1].toString().empty()) { count--; // remove explode()'s last empty line } for (int i = 0; i < count; i++) { echo(lines[i]); echo("\n"); } if (!count || lines.empty()) { return String(); } return StringUtil::Trim(lines[count - 1], StringUtil::TrimRight); }
String f_exec(CStrRef command, Variant output /* = null */, Variant return_var /* = null */) { ShellExecContext ctx; FILE *fp = ctx.exec(command); if (!fp) return ""; StringBuffer sbuf; sbuf.read(fp); Array lines = StringUtil::Explode(sbuf.detach(), "\n"); return_var = ctx.exit(); int count = lines.size(); if (count > 0 && lines[count - 1].toString().empty()) { count--; // remove explode()'s last empty line } if (!output.is(KindOfArray)) { output = Array(ArrayData::Create()); } for (int i = 0; i < count; i++) { output.append(lines[i]); } if (!count || lines.empty()) { return String(); } return StringUtil::Trim(lines[count - 1], StringUtil::TrimRight); }
Array::Array(const Array& other) : m_imp(0) { if (!other.valid()) return; m_imp = new ArrayImp(other.get_heap(), other.get_ext_heap(), other.type()); if (other.empty()) return; if (m_imp->type.isPod()) { m_imp->data = other.m_imp->data; } else { m_imp->data.resize(other.m_imp->data.size()); byte *p = m_imp->data.data(); for (Array::const_iterator itr = other.begin(); itr != other.end(); ++itr) { //TODO: exception safe code CommonObject obj(m_imp->type, p); m_imp->type.methods().construct(obj, get_heap(), get_ext_heap()); m_imp->type.methods().assign(*itr, obj); p += m_imp->type.payload(); } } }
Variant f_strptime(CStrRef date, CStrRef format) { Array ret = DateTime::Parse(date, format); if (ret.empty()) { return false; } return ret; }
Variant f_setlocale(int _argc, int category, CVarRef locale, CArrRef _argv /* = null_array */) { Array argv = _argv; if (locale.is(KindOfArray)) { if (!argv.empty()) throw_invalid_argument("locale: not string)"); argv = locale; // ignore _argv } for (int i = -1; i < argv.size(); i++) { String slocale; if (i == -1) { if (locale.is(KindOfArray)) continue; slocale = locale.toString(); } else { slocale = argv[i].toString(); } const char *loc = slocale; if (slocale.size() >= 255) { throw_invalid_argument("locale name is too long: %s", loc); return false; } if (strcmp("0", loc) == 0) { loc = NULL; } { Lock lock(s_mutex); const char *retval = setlocale(category, loc); if (retval) { return String(retval, CopyString); } } } return false; }
void scaleKeypoints(Array<float>& keypoints, const float scaleX, const float scaleY) { try { if (scaleX != 1. && scaleY != 1.) { // Error check if (!keypoints.empty() && keypoints.getSize(2) != 3) error(errorMessage, __LINE__, __FUNCTION__, __FILE__); // Get #people and #parts const auto numberPeople = keypoints.getSize(0); const auto numberParts = keypoints.getSize(1); // For each person for (auto person = 0 ; person < numberPeople ; person++) { // For each body part for (auto part = 0 ; part < numberParts ; part++) { const auto finalIndex = 3*(person*numberParts + part); keypoints[finalIndex] *= scaleX; keypoints[finalIndex+1] *= scaleY; } } } } catch (const std::exception& e) { error(e.what(), __LINE__, __FUNCTION__, __FILE__); } }
Variant ArrayUtil::Chunk(CArrRef input, int size, bool preserve_keys /* = false */) { if (size < 1) { throw_invalid_argument("size: %d", size); return uninit_null(); } Array ret = Array::Create(); Array chunk; int current = 0; for (ArrayIter iter(input); iter; ++iter) { if (preserve_keys) { chunk.addLval(iter.first(), true).setWithRef(iter.secondRef()); } else { chunk.appendWithRef(iter.secondRef()); } if ((++current % size) == 0) { ret.append(chunk); chunk.clear(); } } if (!chunk.empty()) { ret.append(chunk); } return ret; }
String f_exec(const String& command, VRefParam output /* = null */, VRefParam return_var /* = null */) { ShellExecContext ctx; FILE *fp = ctx.exec(command.c_str()); if (!fp) return ""; StringBuffer sbuf; sbuf.read(fp); Array lines = StringUtil::Explode(sbuf.detach(), "\n").toArray(); int ret = ctx.exit(); if (WIFEXITED(ret)) ret = WEXITSTATUS(ret); return_var = ret; int count = lines.size(); if (count > 0 && lines[count - 1].toString().empty()) { count--; // remove explode()'s last empty line } if (!output.is(KindOfArray)) { output = Array(ArrayData::Create()); } for (int i = 0; i < count; i++) { output.append(lines[i]); } if (!count || lines.empty()) { return String(); } return f_rtrim(lines[count - 1].toString()); }
Array ArrayUtil::Chunk(CArrRef input, int size, bool preserve_keys /* = false */) { if (size < 1) { throw InvalidArgumentException("size", size); } Array ret = Array::Create(); Array chunk; int current = 0; for (ArrayIter iter(input); iter; ++iter) { if (preserve_keys) { chunk.set(iter.first(), iter.second()); } else { chunk.append(iter.second()); } if ((++current % size) == 0) { ret.append(chunk); chunk.clear(); } } if (!chunk.empty()) { ret.append(chunk); } return ret; }
String HHVM_FUNCTION(exec, const String& command, VRefParam output /* = null */, VRefParam return_var /* = null */) { ShellExecContext ctx; FILE *fp = ctx.exec(command); if (!fp) return empty_string(); StringBuffer sbuf; sbuf.read(fp); Array lines = StringUtil::Explode(sbuf.detach(), "\n").toArray(); int ret = ctx.exit(); if (WIFEXITED(ret)) ret = WEXITSTATUS(ret); return_var = ret; int count = lines.size(); if (count > 0 && lines[count - 1].toString().empty()) { count--; // remove explode()'s last empty line } PackedArrayInit pai(count); for (int i = 0; i < count; i++) { pai.append(lines[i]); } output.wrapped() = pai.toArray(); if (!count || lines.empty()) { return String(); } return HHVM_FN(rtrim)(lines[count - 1].toString()); }
String HHVM_FUNCTION(system, const String& command, VRefParam return_var /* = null */) { ShellExecContext ctx; FILE *fp = ctx.exec(command); if (!fp) return empty_string(); StringBuffer sbuf; if (fp) { sbuf.read(fp); } Array lines = StringUtil::Explode(sbuf.detach(), "\n").toArray(); int ret = ctx.exit(); if (WIFEXITED(ret)) ret = WEXITSTATUS(ret); return_var = ret; int count = lines.size(); if (count > 0 && lines[count - 1].toString().empty()) { count--; // remove explode()'s last empty line } auto& ectx = *g_context; for (int i = 0; i < count; i++) { ectx.write(lines[i].toString()); ectx.write("\n"); } if (!count || lines.empty()) { return String(); } return HHVM_FN(rtrim)(lines[count - 1].toString()); }
Variant f_check_user_func_async(CVarRef handles, int timeout /* = -1 */) { if (handles.isArray()) { return FiberAsyncFunc::Status(handles, timeout); } Array ret = FiberAsyncFunc::Status(CREATE_VECTOR1(handles), timeout); return !ret.empty(); }
TEST(ArrayTest, testOperations) { const int SIZE = 6; typedef Poco::Array<int,SIZE> Array; Array a = { { 1 } }; // use some common STL container operations EXPECT_TRUE(a.size() == SIZE); EXPECT_TRUE(a.max_size() == SIZE); EXPECT_TRUE(a.empty() == false); EXPECT_TRUE(a.front() == a[0]); EXPECT_TRUE(a.back() == a[a.size()-1]); //EXPECT_TRUE(a.data() == &a[0]); // assign a.assign(100); for(int i = 0; i<a.size(); i++){ EXPECT_TRUE(a[i] == 100); } // swap Array b; b.assign(10); for(int i=0; i<SIZE; i++){ EXPECT_TRUE(a[i] == 100); EXPECT_TRUE(b[i] == 10); } a.swap(b); for(int i=0; i<SIZE; i++){ EXPECT_TRUE(a[i] == 10); EXPECT_TRUE(b[i] == 100); } }
Variant f_date_parse_from_format(const String& format, const String& date) { Array ret = DateTime::Parse(format, date); if (ret.empty()) { return false; } return ret; }
Variant f_date_parse(const String& date) { Array ret = DateTime::Parse(date); if (ret.empty()) { return false; } return ret; }
Variant f_strptime(const String& date, const String& format) { Array ret = DateTime::ParseAsStrptime(format, date); if (ret.empty()) { return false; } return ret; }
int getBiggestPerson(const Array<float>& keypoints, const float threshold) { try { if (!keypoints.empty()) { const auto numberPeople = keypoints.getSize(0); auto biggestPoseIndex = -1; auto biggestArea = -1.f; for (auto person = 0 ; person < numberPeople ; person++) { const auto newPersonArea = getKeypointsArea(keypoints, person, threshold); if (newPersonArea > biggestArea) { biggestArea = newPersonArea; biggestPoseIndex = person; } } return biggestPoseIndex; } else return -1; } catch (const std::exception& e) { error(e.what(), __LINE__, __FUNCTION__, __FILE__); return -1; } }
ImmutableObj::ImmutableObj(ObjectData *obj) { // This function assumes the object and object/array down the tree have no // internal reference and does not implements serializable interface. assert(!obj->instanceof(SystemLib::s_SerializableClass)); m_cls = obj->o_getClassName()->copy(true); Array props; ClassInfo::GetArray(obj, props, ClassInfo::GetArrayAll); m_propCount = 0; if (props.empty()) { m_props = nullptr; } else { m_props = (Prop*)malloc(sizeof(Prop) * props.size()); for (ArrayIter it(props); !it.end(); it.next()) { assert(m_propCount < props.size()); Variant key(it.first()); assert(key.isString()); CVarRef value = it.secondRef(); SharedVariant *val = nullptr; if (!value.isNull()) { val = new SharedVariant(value, false, true, true); } m_props[m_propCount].val = val; m_props[m_propCount].name = key.getStringData()->copy(true); m_propCount++; } } }
String f_system(const String& command, VRefParam return_var /* = null */) { ShellExecContext ctx; FILE *fp = ctx.exec(command.c_str()); if (!fp) return ""; StringBuffer sbuf; if (fp) { sbuf.read(fp); } Array lines = StringUtil::Explode(sbuf.detach(), "\n").toArray(); int ret = ctx.exit(); if (WIFEXITED(ret)) ret = WEXITSTATUS(ret); return_var = ret; int count = lines.size(); if (count > 0 && lines[count - 1].toString().empty()) { count--; // remove explode()'s last empty line } for (int i = 0; i < count; i++) { echo(lines[i].toString()); echo("\n"); } if (!count || lines.empty()) { return String(); } return f_rtrim(lines[count - 1].toString()); }
bool CmdWhere::onClient(DebuggerClient *client) { if (DebuggerCommand::onClient(client)) return true; if (client->argCount() > 1) { return help(client); } Array st = fetchStackTrace(client); if (st.empty()) { client->info("(no stacktrace to display or in global scope)"); client->info("if you hit serialization limit, consider do " "\"set sa off\" and then get the stack without args"); return true; } // so list command can default to current frame client->moveToFrame(client->getFrame(), false); if (client->argCount() == 0) { int i = 0; for (ArrayIter iter(st); iter; ++iter) { client->printFrame(i, iter.second()); ++i; if (!client->isApiMode() && i % DebuggerClient::ScrollBlockSize == 0 && client->ask("There are %d more frames. Continue? [Y/n]", st.size() - i) == 'n') { break; } } } else { string snum = client->argValue(1); int num = atoi(snum.c_str()); if (snum[0] == '-') { snum = snum.substr(1); } if (!DebuggerClient::IsValidNumber(snum)) { client->error("The argument, if specified, has to be numeric."); return true; } if (num > 0) { for (int i = 0; i < num && i < st.size(); i++) { client->printFrame(i, st[i]); } } else if (num < 0) { for (int i = st.size() + num; i < st.size(); i++) { client->printFrame(i, st[i]); } } else { client->error("0 was specified for the number of frames"); client->tutorial( "The optional argument is the number of frames to print out. " "Use a positive number to print out innermost frames. Use a negative " "number to print out outermost frames." ); } } return true; }
Variant HHVM_FUNCTION(date_parse, const String& date) { Array ret = DateTime::Parse(date); if (ret.empty()) { return false; } return ret; }
BiCGStabResult BiCGstab::solve(const Array& b, const Array& x0) const { Real bnorm2 = Norm2(b); if (bnorm2 == 0.0) { BiCGStabResult result = { 0, 0.0, b}; return result; } Array x = ((!x0.empty()) ? x0 : Array(b.size(), 0.0)); Array r = b - A_(x); Array rTld = r; Array p, pTld, v, s, sTld, t; Real omega = 1.0; Real rho, rhoTld=1.0; Real alpha = 0.0, beta; Real error = Norm2(r)/bnorm2; Size i; for (i=0; i < maxIter_ && error >= relTol_; ++i) { rho = DotProduct(rTld, r); if (rho == 0.0 || omega == 0.0) break; if (i) { beta = (rho/rhoTld)*(alpha/omega); p = r + beta*(p - omega*v); } else { p = r; } pTld = ((M_)? M_(p) : p); v = A_(pTld); alpha = rho/DotProduct(rTld, v); s = r-alpha*v; if (Norm2(s) < relTol_*bnorm2) { x += alpha*pTld; error = Norm2(s)/bnorm2; break; } sTld = ((M_) ? M_(s) : s); t = A_(sTld); omega = DotProduct(t,s)/DotProduct(t,t); x += alpha*pTld + omega*sTld; r = s - omega*t; error = Norm2(r)/bnorm2; rhoTld = rho; } QL_REQUIRE(i < maxIter_, "max number of iterations exceeded"); QL_REQUIRE(error < relTol_, "could not converge"); BiCGStabResult result = { i, error, x}; return result; }
void ArrayTest::emptyCheck() { Array a; CORRADE_VERIFY(!a); CORRADE_VERIFY(a.empty()); Array b(5); CORRADE_VERIFY(b); CORRADE_VERIFY(!b.empty()); }
void WQLCompile::_gather(Array<stack_el>& stk, stack_el sel, bool or_flag) { UInt32 i = 0; stk.empty(); if ((i = eval_heap.size()) == 0) { return; } while (eval_heap[i-1].op == WQL_DO_NOTHING) { eval_heap.remove(i-1); i--; if (i == 0) { return; } } if (or_flag) { stk.append(stack_el(i-1, EVAL_HEAP)); } else { if (sel.type != EVAL_HEAP) { return; } stk.append(sel); } i = 0; while (i<stk.size()) { int k = stk[i].opn; if ((k < 0) || (stk[i].type != EVAL_HEAP)) { i++; } else { if ( ((eval_heap[k].op != WQL_OR) && (or_flag)) || ((eval_heap[k].op != WQL_AND) && (!or_flag)) ) { i++; } else { // replace the element with disjunction stk[i] = eval_heap[k].getSecond(); stk.insert(i, eval_heap[k].getFirst()); if (or_flag) { eval_heap[k].op = WQL_DO_NOTHING; } } } } }
std::pair<String, int> ExtendedException::getFileAndLine() const { String file = empty_string(); int line = 0; Array bt = getBacktrace(); if (!bt.empty()) { Array top = bt.rvalAt(0).toArray(); if (top.exists(s_file)) file = top.rvalAt(s_file).toString(); if (top.exists(s_line)) line = top.rvalAt(s_line).toInt64(); } return std::make_pair(file, line); }
Array func_get_args(int num_args, CArrRef params, Array &args) { FUNCTION_INJECTION(func_get_args); if (params.empty() && args.empty()) return Array::Create(); if (args.empty()) { if (num_args < params.size()) { return params.slice(0, num_args, false); } return params; } Array derefArgs; for (ArrayIter iter(args); iter; ++iter) { derefArgs.append(iter.second()); } if (params.empty()) return derefArgs; ASSERT(num_args > params.size()); Array ret = Array(params).merge(derefArgs); return ret; }