示例#1
0
int main() {
    Optional opt(1729);
    Optional dupe(opt);

    static_assert(IsSame<decltype(opt), Optional<int>>::value);
    static_assert(IsSame<decltype(dupe), Optional<int>>::value);
    static_assert(!IsSame<decltype(dupe), Optional<Optional<int>>>::value);
	return 0;
}
示例#2
0
static void test_peeking_front_buffered_stream(skiatest::Reporter* r,
                                               const SkStream& original,
                                               size_t bufferSize) {
    std::unique_ptr<SkStream> dupe(original.duplicate());
    REPORTER_ASSERT(r, dupe != nullptr);
    auto bufferedStream = SkFrontBufferedStream::Make(std::move(dupe), bufferSize);
    REPORTER_ASSERT(r, bufferedStream != nullptr);

    size_t peeked = 0;
    for (size_t i = 1; !bufferedStream->isAtEnd(); i++) {
        const size_t unpeekableBytes = compare_peek_to_read(r, bufferedStream.get(), i);
        if (unpeekableBytes > 0) {
            // This could not have returned a number greater than i.
            REPORTER_ASSERT(r, unpeekableBytes <= i);

            // We have reached the end of the buffer. Verify that it was at least
            // bufferSize.
            REPORTER_ASSERT(r, peeked + i - unpeekableBytes >= bufferSize);
            // No more peeking is supported.
            break;
        }
        peeked += i;
    }

    // Test that attempting to peek beyond the length of the buffer does not prevent rewinding.
    bufferedStream = SkFrontBufferedStream::Make(original.duplicate(), bufferSize);
    REPORTER_ASSERT(r, bufferedStream != nullptr);

    const size_t bytesToPeek = bufferSize + 1;
    SkAutoMalloc peekStorage(bytesToPeek);
    SkAutoMalloc readStorage(bytesToPeek);

    for (size_t start = 0; start <= bufferSize; start++) {
        // Skip to the starting point
        REPORTER_ASSERT(r, bufferedStream->skip(start) == start);

        const size_t bytesPeeked = bufferedStream->peek(peekStorage.get(), bytesToPeek);
        if (0 == bytesPeeked) {
            // Peeking should only fail completely if we have read/skipped beyond the buffer.
            REPORTER_ASSERT(r, start >= bufferSize);
            break;
        }

        // Only read the amount that was successfully peeked.
        const size_t bytesRead = bufferedStream->read(readStorage.get(), bytesPeeked);
        REPORTER_ASSERT(r, bytesRead == bytesPeeked);
        REPORTER_ASSERT(r, !memcmp(peekStorage.get(), readStorage.get(), bytesPeeked));

        // This should be safe to rewind.
        REPORTER_ASSERT(r, bufferedStream->rewind());
    }
}
示例#3
0
文件: message.cpp 项目: xrmb/nxtv
const NXPart* NXLFSMessageCache::insert(NXPart* data, int oid)
{
  NXMutexLocker ml(m_mutex, "insert1", data->msgid);

  if(NXLOG.cache > 1)
    printf("cache:\t%-10s %s\n", "insert1", data->msgid);
  dupe(data->msgid, oid);

  trim();

  oid %= m_listlen;

  NXfree(m_list[oid]);
  m_list[oid] = data;

  return m_list[oid];
}
示例#4
0
文件: message.cpp 项目: xrmb/nxtv
const NXPart* NXLFSMessageCache::insert(const NXPart* data, int oid)
{
  NXMutexLocker ml(m_mutex, "insert2", data->msgid);

  if(NXLOG.cache > 1)
    printf("cache:\t%-10s %s\n", "insert2", data->msgid);
  dupe(data->msgid, oid);

  trim();
  
  size_t size = data->len + sizeof(NXPart) - 1;
  oid %= m_listlen;

  //--- reuse memory ---
  if(m_list[oid] == NULL || _msize(m_list[oid]) < size)
  {
    m_list[oid] = (NXPart*)NXrealloc(m_list[oid], size);
  }
  memcpy_s(m_list[oid], size, data, size); // ERR


  return m_list[oid];
}
示例#5
0
/* determine which arcs are undirected. */
SEXP which_undirected(SEXP arcs, SEXP nodes) {

int i = 0, nrow = length(arcs)/2, nlvls = 0;
int *coords = NULL, *id = NULL;
SEXP result, labels, try, arc_id;

  /* get the node labels from the arcs, or use those passed down from R. */
  if (isNull(nodes))
    PROTECT(labels = unique(arcs));
  else
    labels = nodes;

  nlvls = length(labels);

  /* match the node labels in the arc set. */
  PROTECT(try = match(labels, arcs, 0));
  coords = INTEGER(try);

  /* initialize the checklist. */
  PROTECT(arc_id = allocVector(INTSXP, nrow));
  id = INTEGER(arc_id);

  /* fill the checklist with the UPTRI() coordinates, which uniquely
   * identify an arc modulo its direction. */
  for (i = 0; i < nrow; i++)
    id[i] = UPTRI(coords[i], coords[i + nrow], nlvls);

  PROTECT(result = dupe(arc_id));

  if (isNull(nodes))
    UNPROTECT(4);
  else
    UNPROTECT(3);

  return result;

}/*WHICH_UNDIRECTED*/
main() {
    int type;
    double op2;
    char opStr[MAXOPLEN];

    while ((type = getop(opStr)) != EOF) {
        switch (type) {
            case NUMBER:
                push(atof(opStr));
                break;
            case '+':
                push(pop() + pop());
                break;
            case '*':
                push(pop() * pop());
                break;
            case '/': case '%':
                op2 = pop();
                if (op2 != 0.0) {
                    if (type == '/')
                        push(pop() / op2);
                    else
                        push(modulo(pop(), op2));
                }
                else
                    printf("ERROR: Divide by zero\n");
                break;
            case '-':
                op2 = pop();
                push(pop() - op2);
                break;
            case DUPE:
                if (getop(opStr) == NUMBER) {
                    int p = atoi(opStr);
                    char c = getop(opStr);
                    dupe(p, c);
                }
                else {
                    printf("ERROR: Could not duplicate value\n");
                }
                break;
            case SWAP:
            {
                int p1, p2;
                if (getop(opStr) == NUMBER)
                    p1 = atoi(opStr);
                else
                    printf("ERROR: Could not swap values\n");
                if (getop(opStr) == NUMBER)
                    p2 = atoi(opStr);
                else
                    printf("ERROR: Could not swap values\n");
                swap(p1, p2);
                break;
            }
            case DEL:
            {
                if (getop(opStr) == NUMBER) {
                    int p = atoi(opStr);
                    del(p);
                }
                else
                    printf("ERROR: Could not delete value\n");
                break;
            }
            case PRINT:
            {
                if (getop(opStr) == NUMBER) {
                    int p = atoi(opStr);
                    print(p);
                }
                else
                    printf("ERROR: Could not print\n");
                break;
            }
            case CLEAR:
                clear();
                break;
            case '\n':
                printf("\t%.8g\n", pop());
                break;
            default:
                printf("ERROR: Unknown command '%s'\n", opStr);
                break;
        }
    }
    return 0;
}
示例#7
0
double castelo_prior(SEXP beta, SEXP target, SEXP parents, SEXP children,
                     SEXP debug) {

    int i = 0, k = 0, t = 0, nnodes = 0, cur_arc = 0;
    int nbeta = LENGTH(VECTOR_ELT(beta, 0));
    int *temp = NULL, *debuglevel = LOGICAL(debug), *aid = INTEGER(VECTOR_ELT(beta, 2));
    double prior = 0, result = 0;
    double *bkwd = REAL(VECTOR_ELT(beta, 4)), *fwd = REAL(VECTOR_ELT(beta, 3));
    short int *adjacent = NULL;
    SEXP nodes, try;

    /* get the node labels. */
    nodes = getAttrib(beta, install("nodes"));
    nnodes = LENGTH(nodes);

    /* match the target node. */
    PROTECT(try = match(nodes, target, 0));
    t = INT(try);
    UNPROTECT(1);

    /* find out which nodes are parents and which nodes are children. */
    adjacent = allocstatus(nnodes);

    PROTECT(try = match(nodes, parents, 0));
    temp = INTEGER(try);
    for (i = 0; i < LENGTH(try); i++)
        adjacent[temp[i] - 1] = PARENT;
    UNPROTECT(1);

    PROTECT(try = match(nodes, children, 0));
    temp = INTEGER(try);
    for (i = 0; i < LENGTH(try); i++)
        adjacent[temp[i] - 1] = CHILD;
    UNPROTECT(1);

    /* prior probabilities table lookup. */
    for (i = t + 1; i <= nnodes; i++) {

        /* compute the arc id. */
        cur_arc = UPTRI3(t, i, nnodes);

        /* look up the prior probability. */
        for (/*k,*/ prior = ((double)1/3); k < nbeta; k++) {

            /* arcs are ordered, so we can stop early in the lookup. */
            if (aid[k] > cur_arc)
                break;

            if (aid[k] == cur_arc) {

                switch(adjacent[i - 1]) {

                case PARENT:
                    prior = bkwd[k];
                    break;
                case CHILD:
                    prior = fwd[k];
                    break;
                default:
                    prior = 1 - bkwd[k] - fwd[k];

                }/*SWITCH*/

                break;

            }/*THEN*/

        }/*FOR*/

        if (*debuglevel > 0) {

            switch(adjacent[i - 1]) {

            case PARENT:
                Rprintf("  > found arc %s -> %s, prior pobability is %lf.\n",
                        NODE(i - 1), NODE(t - 1), prior);
                break;
            case CHILD:
                Rprintf("  > found arc %s -> %s, prior probability is %lf.\n",
                        NODE(t - 1), NODE(i - 1), prior);
                break;
            default:
                Rprintf("  > no arc between %s and %s, prior probability is %lf.\n",
                        NODE(t - 1), NODE(i - 1), prior);

            }/*SWITCH*/

        }/*THEN*/

        /* move to log-scale and divide by the non-informative log(1/3), so that
         * the contribution of each arc whose prior has not been not specified by
         * the user is zero; overflow is likely otherwise. */
        result += log(prior / ((double)1/3));

    }/*FOR*/

    return result;

}/*CASTELO_PRIOR*/

/* complete a prior as per Castelo & Siebes. */
SEXP castelo_completion(SEXP prior, SEXP nodes)  {

    int i = 0, k = 0, cur = 0, narcs1 = 0, narcs2 = 0, nnodes = LENGTH(nodes);
    int *m1 = NULL, *m2 = NULL, *und = NULL, *aid = NULL, *poset = NULL, *id = NULL;
    double *d1 = NULL, *d2 = NULL, *p = NULL;
    SEXP df, arc_id, undirected, a1, a2, match1, match2, prob;
    SEXP result, colnames, from, to, nid, dir1, dir2;

    /* compute numeric IDs for the arcs. */
    a1 = VECTOR_ELT(prior, 0);
    a2 = VECTOR_ELT(prior, 1);
    narcs1 = LENGTH(a1);
    PROTECT(match1 = match(nodes, a1, 0));
    PROTECT(match2 = match(nodes, a2, 0));
    m1 = INTEGER(match1);
    m2 = INTEGER(match2);
    PROTECT(arc_id = allocVector(INTSXP, narcs1));
    aid = INTEGER(arc_id);

    c_arc_hash(&narcs1, &nnodes, m1, m2, aid, NULL, TRUE);

    /* duplicates correspond to undirected arcs. */
    PROTECT(undirected = dupe(arc_id));
    und = INTEGER(undirected);

    /* extract the components from the prior. */
    prob = VECTOR_ELT(prior, 2);
    p = REAL(prob);

    /* count output arcs. */
    for (i = 0; i < narcs1; i++)
        narcs2 += 2 - und[i];
    narcs2 /= 2;

    /* allocate the columns of the return value. */
    PROTECT(from = allocVector(STRSXP, narcs2));
    PROTECT(to = allocVector(STRSXP, narcs2));
    PROTECT(nid = allocVector(INTSXP, narcs2));
    id = INTEGER(nid);
    PROTECT(dir1 = allocVector(REALSXP, narcs2));
    d1 = REAL(dir1);
    PROTECT(dir2 = allocVector(REALSXP, narcs2));
    d2 = REAL(dir2);

    /* sort the strength coefficients. */
    poset = alloc1dcont(narcs1);
    for (k = 0; k < narcs1; k++)
        poset[k] = k;
    R_qsort_int_I(aid, poset, 1, narcs1);

    for (i = 0, k = 0; i < narcs1; i++) {

        cur = poset[i];

#define ASSIGN(A1, A2, D1, D2) \
  SET_STRING_ELT(from,  k, STRING_ELT(A1, cur)); \
  SET_STRING_ELT(to,  k, STRING_ELT(A2, cur)); \
  id[k] = aid[i]; \
  D1[k] = p[cur]; \
  if ((und[cur] == TRUE) && (i < narcs1 - 1)) \
    D2[k] = p[poset[++i]]; \
  else \
    D2[k] = (1 - D1[k])/2;

        /* copy the node labels. */
        if (m1[cur] < m2[cur]) {

            ASSIGN(a1, a2, d1, d2);

        }/*THEN*/
        else {

            ASSIGN(a2, a1, d2, d1);

        }/*ELSE*/

        if (d1[k] + d2[k] > 1) {

            UNPROTECT(9);

            error("the probabilities for arc %s -> %s sum to %lf.",
                  CHAR(STRING_ELT(from, k)), CHAR(STRING_ELT(to, k)), d1[k] + d2[k]);

        }/*THEN*/

        /* move to the next arc. */
        k++;

    }/*FOR*/

    /* set up the return value. */
    PROTECT(result = allocVector(VECSXP, 5));
    SET_VECTOR_ELT(result, 0, from);
    SET_VECTOR_ELT(result, 1, to);
    SET_VECTOR_ELT(result, 2, nid);
    SET_VECTOR_ELT(result, 3, dir1);
    SET_VECTOR_ELT(result, 4, dir2);
    PROTECT(colnames = allocVector(STRSXP, 5));
    SET_STRING_ELT(colnames, 0, mkChar("from"));
    SET_STRING_ELT(colnames, 1, mkChar("to"));
    SET_STRING_ELT(colnames, 2, mkChar("aid"));
    SET_STRING_ELT(colnames, 3, mkChar("fwd"));
    SET_STRING_ELT(colnames, 4, mkChar("bkwd"));
    setAttrib(result, R_NamesSymbol, colnames);
    PROTECT(df = minimal_data_frame(result));

    UNPROTECT(12);

    return df;

}/*CASTELO_COMPLETION*/
bool ADM_Composer::decompressImage(ADMImage *out,ADMCompressedImage *in,uint32_t ref)
{
 ADMImage *tmpImage=NULL;
 _VIDEOS  *v=_segments.getRefVideo(ref);
 uint32_t refOnly=v->decoder->dontcopy(); // can we skip one memcpy ?
 // get settings from pref...
 
// This is only an empty Shell
    if(refOnly)
    {
                uint32_t w,h;
                if(_scratch) // Can we reuse the old scratch memory ?
                {
                    _scratch->getWidthHeight(&w,&h);
                    if(w!=_imageBuffer->_width || _imageBuffer->_height!=h)
                    {
                        delete _scratch;
                        _scratch=NULL;
                    }
                }
                if(!_scratch)
                {
                  _scratch=new ADMImageRef(_imageBuffer->_width,_imageBuffer->_height);
                }
                tmpImage=_scratch;
        }
        else
        {
                tmpImage=_imageBuffer;
       }
    //
    tmpImage->_colorspace=ADM_COLOR_YV12;
	// Decode it
        if (!v->decoder->uncompress (in, tmpImage))
	    {
            printf("[decompressImage] uncompress failed\n");
            return false;
        }
        //
        if(tmpImage->_noPicture && refOnly)
        {
            printf("[decompressImage] NoPicture\n");
            // Fill in with black
            return false;
        }
        aprintf("[::Decompress] in:%"PRIu32" out:%"PRIu32" flags:%x\n",in->demuxerPts,out->Pts,out->flags);
	// If not quant and it is already YV12, we can stop here
    // Also, if the image is decoded through hw, dont do post proc
	if(tmpImage->refType!=ADM_HW_NONE || 
                    ((!tmpImage->quant || !tmpImage->_qStride) && tmpImage->_colorspace==ADM_COLOR_YV12))
	{
		out->_Qp=2;
		out->duplicate(tmpImage);
		aprintf("[decompressImage] : No quant avail\n");
		return true;
	}
	// We got everything, let's go
	// 1 compute average quant
	int qz;
	uint32_t sumit=0;
    // Dupe infos
    out->copyInfo(tmpImage);


    // Do postprocessing if any
	for(uint32_t z=0;z<tmpImage->_qSize;z++)
	{
            qz=(int)tmpImage->quant[z];
			sumit+=qz;
	}
	sumit+=(tmpImage->_qSize-1);
	float sum=(float)sumit;
	sum/=tmpImage->_qSize;
	if(sum>31) sum=31;
	if(sum<1) sum=1;

    // update average Q
	tmpImage->_Qp=out->_Qp=(uint32_t)floor(sum);
	// Pp deactivated ?
	if(!_pp->postProcType || !_pp->postProcStrength || tmpImage->_colorspace!=ADM_COLOR_YV12)
        {
                dupe(tmpImage,out,v);
                aprintf("EdCache: Postproc disabled\n");
		return 1;
	}
    /* Do it!*/
    _pp->process(tmpImage,out);
    return true;
}