本文整理汇总了C++中CArrRef::getKey方法的典型用法代码示例。如果您正苦于以下问题:C++ CArrRef::getKey方法的具体用法?C++ CArrRef::getKey怎么用?C++ CArrRef::getKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CArrRef
的用法示例。
在下文中一共展示了CArrRef::getKey方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RandomKeys
Variant ArrayUtil::RandomKeys(CArrRef input, int num_req /* = 1 */) {
int count = input.size();
if (num_req <= 0 || num_req > count) {
raise_warning("Second argument has to be between 1 and the "
"number of elements in the array");
return uninit_null();
}
std::vector<ssize_t> indices;
indices.reserve(count);
for (ssize_t pos = input->iter_begin(); pos != ArrayData::invalid_index;
pos = input->iter_advance(pos)) {
indices.push_back(pos);
}
php_array_data_shuffle(indices);
if (num_req == 1) {
return input->getKey(indices[0]);
}
Array ret = Array::Create();
for (int i = 0; i < num_req; i++) {
ssize_t pos = indices[i];
ret.append(input->getKey(pos));
}
return ret;
}
示例2: RandomKeys
Variant ArrayUtil::RandomKeys(CArrRef input, int num_req /* = 1 */) {
int count = input.size();
if (num_req <= 0 || num_req > count) {
return null;
}
std::vector<ssize_t> indices;
indices.reserve(count);
for (ssize_t pos = input->iter_begin(); pos != ArrayData::invalid_index;
pos = input->iter_advance(pos)) {
indices.push_back(pos);
}
random_shuffle(indices.begin(), indices.end());
if (num_req == 1) {
return input->getKey(indices[0]);
}
Array ret = Array::Create();
for (int i = 0; i < num_req; i++) {
ssize_t pos = indices[i];
ret.append(input->getKey(pos));
}
return ret;
}
示例3: MultiSort
bool Array::MultiSort(std::vector<SortData> &data, bool renumber) {
ASSERT(!data.empty());
int count = -1;
for (unsigned int k = 0; k < data.size(); k++) {
SortData &opaque = data[k];
ASSERT(opaque.array);
ASSERT(opaque.cmp_func);
int size = opaque.array->size();
if (count == -1) {
count = size;
} else if (count != size) {
throw_invalid_argument("arrays: (inconsistent sizes)");
return false;
}
opaque.positions.reserve(size);
CArrRef arr = *opaque.array;
if (!arr.empty()) {
for (ssize_t pos = arr->iter_begin(); pos != ArrayData::invalid_index;
pos = arr->iter_advance(pos)) {
opaque.positions.push_back(pos);
}
}
}
if (count == 0) {
return true;
}
int *indices = (int *)malloc(sizeof(int) * count);
for (int i = 0; i < count; i++) {
indices[i] = i;
}
zend_qsort(indices, count, sizeof(int), multi_compare_func, (void *)&data);
for (unsigned int k = 0; k < data.size(); k++) {
SortData &opaque = data[k];
CArrRef arr = *opaque.array;
Array sorted;
for (int i = 0; i < count; i++) {
ssize_t pos = opaque.positions[indices[i]];
Variant k(arr->getKey(pos));
if (renumber && k.isInteger()) {
sorted.append(arr->getValueRef(pos));
} else {
sorted.set(k, arr->getValueRef(pos));
}
}
*opaque.original = sorted;
}
free(indices);
return true;
}
示例4: RandomKeys
Variant ArrayUtil::RandomKeys(CArrRef input, int num_req /* = 1 */) {
int count = input.size();
if (num_req <= 0 || num_req > count) {
raise_warning("Second argument has to be between 1 and the "
"number of elements in the array");
return init_null();
}
if (num_req == 1) {
// Iterating through the counter is correct but a bit inefficient
// compared to being able to access the right offset into array data,
// but necessary for this code to be agnostic to the array's internal
// representation. Assuming uniform distribution, we'll expect to
// iterate through half of the array's data.
ssize_t index = f_rand(0, count-1);
ssize_t pos = input->iter_begin();
while (index--) {
pos = input->iter_advance(pos);
}
return input->getKey(pos);
}
std::vector<ssize_t> indices;
indices.reserve(count);
for (ssize_t pos = input->iter_begin(); pos != ArrayData::invalid_index;
pos = input->iter_advance(pos)) {
indices.push_back(pos);
}
php_array_data_shuffle(indices);
Array ret = Array::Create();
for (int i = 0; i < num_req; i++) {
ssize_t pos = indices[i];
ret.append(input->getKey(pos));
}
return ret;
}
示例5: Reverse
Variant ArrayUtil::Reverse(CArrRef input, bool preserve_keys /* = false */) {
if (input.empty()) {
return input;
}
Array ret = Array::Create();
for (ssize_t pos = input->iter_end(); pos != ArrayData::invalid_index;
pos = input->iter_rewind(pos)) {
Variant key(input->getKey(pos));
if (preserve_keys || key.isString()) {
ret.setWithRef(key, input->getValueRef(pos), true);
} else {
ret.appendWithRef(input->getValueRef(pos));
}
}
return ret;
}
示例6: diffImpl
Array Array::diffImpl(CArrRef array, bool by_key, bool by_value, bool match,
PFUNC_CMP key_cmp_function,
const void *key_data,
PFUNC_CMP value_cmp_function,
const void *value_data) const {
ASSERT(by_key || by_value);
ASSERT(by_key || key_cmp_function == NULL);
ASSERT(by_value || value_cmp_function == NULL);
PFUNC_CMP value_cmp_as_string_function = value_cmp_function;
if (!value_cmp_function) {
value_cmp_function = SortStringAscending;
value_cmp_as_string_function = CompareAsStrings;
}
Array ret = Array::Create();
if (by_key && !key_cmp_function) {
// Fast case
for (ArrayIter iter(*this); iter; ++iter) {
Variant key(iter.first());
CVarRef value(iter.secondRef());
bool found = false;
if (array->exists(key)) {
if (by_value) {
found = value_cmp_as_string_function(
value, array.rvalAt(key, AccessFlags::Key), value_data) == 0;
} else {
found = true;
}
}
if (found == match) {
ret.addLval(key, true).setWithRef(value);
}
}
return ret;
}
if (!key_cmp_function) {
key_cmp_function = SortRegularAscending;
}
vector<int> perm1;
SortData opaque1;
int bottom = 0;
int top = array.size();
PFUNC_CMP cmp;
const void *cmp_data;
if (by_key) {
cmp = key_cmp_function;
cmp_data = key_data;
} else {
cmp = value_cmp_function;
cmp_data = value_data;
}
SortImpl(perm1, array, opaque1, cmp, by_key, cmp_data);
for (ArrayIter iter(*this); iter; ++iter) {
Variant target;
if (by_key) {
target = iter.first();
} else {
target = iter.second();
}
int mid = -1;
int min = bottom;
int max = top;
while (min < max) {
mid = (max + min) / 2;
ssize_t pos = opaque1.positions[perm1[mid]];
int cmp_res = cmp(target,
by_key ? array->getKey(pos)
: array->getValueRef(pos),
cmp_data);
if (cmp_res > 0) { // outer is bigger
min = mid + 1;
} else if (cmp_res == 0) {
break;
} else {
max = mid;
}
}
bool found = false;
if (min < max) { // found
// if checking both, check value
if (by_key && by_value) {
CVarRef val(iter.secondRef());
// Have to look up and down for matches
for (int i = mid; i < max; i++) {
ssize_t pos = opaque1.positions[perm1[i]];
if (key_cmp_function(target, array->getKey(pos), key_data) != 0) {
break;
}
if (value_cmp_as_string_function(val, array->getValueRef(pos),
value_data) == 0) {
found = true;
break;
}
}
if (!found) {
for (int i = mid-1; i >= min; i--) {
//.........这里部分代码省略.........