typename std::enable_if< std::is_base_of<BaseSet, TSet>::value, Object>::type BaseSet::php_skip(const Variant& n) { if (!n.isInteger()) { SystemLib::throwInvalidArgumentExceptionObject( "Parameter n must be an integer"); } int64_t len = n.toInt64(); if (len <= 0) { // We know the resulting Set will simply be a copy of this Set, // so we can just call Clone() and return early here. return Object::attach(TSet::Clone(this)); } auto set = req::make<TSet>(); if (len >= m_size) { // We know the resulting Set will be empty, so we can return // early here. return Object{std::move(set)}; } size_t sz = size_t(m_size) - size_t(len); assert(sz); set->reserve(sz); set->setSize(sz); set->setPosLimit(sz); uint32_t frPos = nthElmPos(len); auto table = set->hashTab(); auto mask = set->tableMask(); for (uint32_t toPos = 0; toPos < sz; ++toPos, ++frPos) { while (isTombstone(frPos)) { assert(frPos + 1 < posLimit()); ++frPos; } auto& toE = set->data()[toPos]; dupElm(data()[frPos], toE); *findForNewInsert(table, mask, toE.probe()) = toPos; if (toE.hasIntKey()) { set->updateNextKI(toE.ikey); } else { assert(toE.hasStrKey()); set->updateIntLikeStrKeys(toE.skey); } } return Object{std::move(set)}; }
ALWAYS_INLINE typename std::enable_if< std::is_base_of<BaseMap, TMap>::value, Object>::type BaseMap::php_slice(const Variant& start, const Variant& len) { int64_t istart; int64_t ilen; if (!start.isInteger() || (istart = start.toInt64()) < 0) { SystemLib::throwInvalidArgumentExceptionObject( "Parameter start must be a non-negative integer"); } if (!len.isInteger() || (ilen = len.toInt64()) < 0) { SystemLib::throwInvalidArgumentExceptionObject( "Parameter len must be a non-negative integer"); } size_t skipAmt = std::min<size_t>(istart, m_size); size_t sz = std::min<size_t>(ilen, size_t(m_size) - skipAmt); auto map = req::make<TMap>(); map->reserve(sz); map->setSize(sz); map->setPosLimit(sz); uint32_t frPos = nthElmPos(skipAmt); auto table = map->hashTab(); auto mask = map->tableMask(); for (uint32_t toPos = 0; toPos < sz; ++toPos, ++frPos) { frPos = skipTombstonesNoBoundsCheck(frPos); auto& toE = map->data()[toPos]; dupElm(data()[frPos], toE); *findForNewInsert(table, mask, toE.probe()) = toPos; if (toE.hasIntKey()) { map->updateNextKI(toE.ikey); } else { assert(toE.hasStrKey()); map->updateIntLikeStrKeys(toE.skey); } } return Object{std::move(map)}; }
ALWAYS_INLINE typename std::enable_if< std::is_base_of<BaseMap, TMap>::value, Object>::type BaseMap::php_map(const Variant& callback) const { VMRegGuard _; CallCtx ctx; vm_decode_function(callback, nullptr, false, ctx); if (!ctx.func) { SystemLib::throwInvalidArgumentExceptionObject( "Parameter must be a valid callback"); } auto map = req::make<TMap>(); if (!m_size) return Object{std::move(map)}; assert(posLimit() != 0); assert(hashSize() > 0); assert(map->arrayData() == staticEmptyMixedArray()); map->m_arr = MixedArray::asMixed(MixedArray::MakeReserveMixed(cap())); map->setIntLikeStrKeys(intLikeStrKeys()); wordcpy(map->hashTab(), hashTab(), hashSize()); { uint32_t used = posLimit(); int32_t version = m_version; uint32_t i = 0; // When the loop below finishes or when an exception is thrown, // make sure that posLimit() get set to the correct value and // that m_pos gets set to point to the first element. SCOPE_EXIT { map->setPosLimit(i); map->arrayData()->m_pos = map->nthElmPos(0); }; constexpr int64_t argc = useKey ? 2 : 1; TypedValue argv[argc]; for (; i < used; ++i) { const Elm& e = data()[i]; Elm& ne = map->data()[i]; if (isTombstone(i)) { ne.data.m_type = e.data.m_type; continue; } TypedValue* tv = &ne.data; if (useKey) { if (e.hasIntKey()) { argv[0].m_type = KindOfInt64; argv[0].m_data.num = e.ikey; } else { argv[0].m_type = KindOfString; argv[0].m_data.pstr = e.skey; } } argv[argc-1] = e.data; g_context->invokeFuncFew(tv, ctx, argc, argv); if (UNLIKELY(version != m_version)) { tvRefcountedDecRef(tv); throw_collection_modified(); } if (e.hasStrKey()) { e.skey->incRefCount(); } ne.ikey = e.ikey; ne.data.hash() = e.data.hash(); map->incSize(); // Needed so that the new elements are accounted for when GC scanning. map->incPosLimit(); } } return Object{std::move(map)}; }