/* * Hash an ishake block and combine it into an existing hash in the way * specified by op. */ void _hash_and_combine(ishake *is, ishake_block block, group_op op) { uint64_t *hash; hash = calloc((size_t)is->output_len/8, sizeof(uint64_t)); _hash_block(block, hash, is->output_len, is->hash_func); _combine(is->hash, hash, (uint16_t)(is->output_len/8), op); free(hash); }
void buddy_free(struct buddy * self, int offset) { assert( offset < (1<< self->level)); int left = 0; int length = 1 << self->level; int index = 0; for (;;) { switch (self->tree[index]) { case NODE_USED: assert(offset == left); _combine(self, index); return; case NODE_UNUSED: assert(0); return; default: length /= 2; if (offset < left + length) { index = index * 2 + 1; } else { left += length; index = index * 2 + 2; } break; } } }
vector<vector<int> > combine(int n, int k) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > ret; vector<bool> r(n, false); vector<int> num; _combine(ret, r, num, 0, k, n); return ret; }
void _combine(vector<vector<int> > &ret, vector<bool> &r, vector<int> &num, int cur, int k, int n) { if (num.size() == k) { ret.push_back(num); return; } for (int i = cur; i < n; i++) { if (r[i]) continue; r[i] = true; num.push_back(i + 1); _combine(ret, r, num, i + 1, k, n); num.pop_back(); r[i] = false; } }
static int _combine(combiner f, int dim, int maxdim, int ninputs, int nlow, int nhigh, PyArrayObject *inputs[], PyArrayObject *masks[], PyArrayObject *output) { int i, j; if (dim == maxdim-1) { Float64 sorted[MAX_ARRAYS]; Float64 *tinputs[MAX_ARRAYS]; UInt8 *tmasks[MAX_ARRAYS]; Float64 *toutput; int cols = inputs[0]->dimensions[dim]; /* Allocate and convert 1 temporary row at a time */ for(i=0; i<ninputs; i++) tinputs[i] = (Float64 *) inputs[i]->data; if (masks) { for(i=0; i<ninputs; i++) tmasks[i] = (UInt8 *) masks[i]->data; } toutput = (Float64 *) output->data; for(j=0; j<cols; j++) { int goodpix = _mask_and_sort( ninputs, j, tinputs, masks ? tmasks : NULL, sorted); toutput[j] = f(goodpix, nlow, nhigh, sorted); } } else { for (i=0; i<inputs[0]->dimensions[dim]; i++) { for(j=0; j<ninputs; j++) { inputs[j]->data += inputs[j]->strides[dim]*i; if (masks) { masks[j]->data += masks[j]->strides[dim]*i; } } output->data += output->strides[dim]*i; _combine(f, dim+1, maxdim, ninputs, nlow, nhigh, inputs, masks, output); for(j=0; j<ninputs; j++) { inputs[j]->data -= inputs[j]->strides[dim]*i; if (masks) { masks[j]->data -= masks[j]->strides[dim]*i; } } output->data -= output->strides[dim]*i; } } return 0; }
void _combine(vector<vector<int>> &ans, vector<int> &res, int s, int n, int k) { if((n - s + 1) < k) return; if(k == 0) { ans.push_back(res); return; } for(int i = s; i <= n; i++) { res.push_back(i); _combine(ans, res, i+1, n, k-1); res.pop_back(); } }
vector<vector<int>> combine(int n, int k) { vector<vector<int>> ans; vector<int> res; _combine(ans, res, 1, n, k); return ans; }
static PyObject * _Py_combine(PyObject *obj, PyObject *args, PyObject *kw) { PyObject *arrays, *output; int nlow=0, nhigh=0, narrays; PyObject *badmasks=Py_None; char *keywds[] = { "arrays", "output", "nlow", "nhigh", "badmasks", "kind", NULL }; char *kind; combiner f; PyArrayObject *arr[MAX_ARRAYS], *bmk[MAX_ARRAYS], *toutput; int i; if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|iiOs:combine", keywds, &arrays, &output, &nlow, &nhigh, &badmasks, &kind)) return NULL; narrays = PySequence_Length(arrays); if (narrays < 0) return PyErr_Format( PyExc_TypeError, "combine: arrays is not a sequence"); if (narrays > MAX_ARRAYS) return PyErr_Format( PyExc_TypeError, "combine: too many arrays."); for(i=0; i<narrays; i++) { PyObject *a = PySequence_GetItem(arrays, i); if (!a) return NULL; arr[i] = NA_InputArray(a, tFloat64, C_ARRAY); if (!arr[i]) return NULL; Py_DECREF(a); if (badmasks != Py_None) { a = PySequence_GetItem(badmasks, i); if (!a) return NULL; bmk[i] = NA_InputArray(a, tUInt8, C_ARRAY); if (!bmk[i]) return NULL; Py_DECREF(a); } } toutput = NA_OutputArray(output, tFloat64, C_ARRAY); if (!toutput) return NULL; for (i=0,f=0; i<ELEM(functions); i++) if (!strcmp(kind, functions[i].name)) { f = functions[i].fptr; break; } if (!f) return PyErr_Format( PyExc_ValueError, "Invalid comination function."); if (_combine( f, 0, arr[0]->nd, narrays, nlow, nhigh, arr, (badmasks != Py_None ? bmk : NULL), toutput) < 0) return NULL; for(i=0; i<narrays; i++) { Py_DECREF(arr[i]); if (badmasks != Py_None) { Py_DECREF(bmk[i]); } } Py_DECREF(toutput); Py_INCREF(Py_None); return Py_None; }
typename std::enable_if<std::is_convertible<U, T>::value, void>::type /* void */ combine(const space_saving<U>& other) { _combine(other); }