Пример #1
0
 /* [begin, end)区间
  * 归并排序
  */
 void _sort(ListNode *begin, ListNode *end)
 {
     //处理0~1个节点
     if (begin == NULL || begin->next == end)
     {
         return ;
     }
     //处理两个节点,交换数据
     else if (begin->next->next == end)
     {
         if ( begin->val > begin->next->val)
         {
             int t = begin->val;
             begin->val = begin->next->val;
             begin->next->val = t;
         }
         return ;
     }
     // 寻找中间节点
     ListNode * p1 = begin, *p2 = begin->next;
     while(p2 != end)
     {
         p1 = p1->next;
         p2 = p2->next;
         if(p2 != end)
         {
             p2 = p2->next;
         }
     }
     //排序
     _sort(begin, p1);
     _sort(p1, end);
     _merge(begin, p1, end);
 }
Пример #2
0
void _sort(node *p, int *a)
    {
    if (p != NULL)
        {
        _sort(p->left, a);
        a[_index++] = p->key;
        _sort(p->right, a);
        }
    }
Пример #3
0
void _sort(node *p, void *a, size_t width)
    {
    if (p != NULL)
        {
        _sort(p->left, a, width);
        memcpy((char*)a+(_index++)*width, p+1, width);
        _sort(p->right, a, width);
        }
    }
Пример #4
0
		void _sort(Itr begin, Itr end)
		{
			if (std::distance(begin, end) <= CUTOFF) return;
			
			boundary_t<Itr> boundary = _partition(begin, end);
			
			_sort(begin, boundary.first);
			_sort(boundary.second, end);
		}
Пример #5
0
void _sort(Bnode *t)
{
    int i, j;
    if (t != NULL)
    {
        for (i = 0; i < t->n; i++)
        {
            _sort(t->ptr[i]);
            printf("%c\n", t->key[i]);
            _sort(t->ptr[i+1]);
        }
    }
}
Пример #6
0
/*
    Computes the array {C,lenB} of congruence classes $m \mod p$ 
    for which we need to $\mu_i(m)$, in ascending order.

    Returns the number of elements written into the array as 
    as lenC.  Note that 1 <= lenC <= lenB.
 */
void _congruence_class(long *C, long *lenC, long ind, 
                       const mon_t *B, long lenB, 
                       long n, long d, long p)
{
    long i, j, k, u, v;

    *lenC = 0;
    for (i = 0; i < lenB; i++)
        for (j = 0; j < lenB; j++)
        {
            for (k = 0; k <= n; k++)
            {
                u = mon_get_exp(B[i], k);
                v = mon_get_exp(B[j], k);
                if ((p * (u + 1) - (v + 1)) % d != 0)
                {
                    break;
                }
            }

            if (k > n)
            {
                u = mon_get_exp(B[i], ind);
                v = mon_get_exp(B[j], ind);
                C[(*lenC)++] = ((p * (u + 1) - (v + 1)) / d) % p;
            }
        }

    /* Remove duplicates */
    _remove_duplicates(C, lenC);
    _sort(C, *lenC);
}
Пример #7
0
void pair_binner::add_lens_id( const ssize_t & new_id, const mass_type & m, const flt_type & z,
		const flt_type & mag, const flt_type & weight)
{
	// Check if we're already buffering a different lens
	if(_buffering_lens_id_)
	{
		if(_buffering_lens_id_->id!=new_id)
		{
			// If we get here, this is a new lens, so sort any existing data, which will empty the buffer
			_sort();
		}
		else
		{
			// If we get here, we're already buffering this lens, so just return
			return;
		}
	}

	auto new_lens_id = lens_id(new_id,m,z,mag,_unmasked_frac_bin_limits_,_unmasked_fracs_, weight);

	// Check if this id has been used in the past
	if(_past_lens_ids_.find(new_lens_id)!=_past_lens_ids_.end())
	{
		// If we've used it before, throw an exception. This implementation can't handle that
		throw std::runtime_error("Lens has been added to binner multiple times with another lens in between. This implementation is optimized\nand only allows each lens to be binned once.");
	}

	_buffering_lens_id_ = boost::optional<lens_id>(new_lens_id);

	_sorted_ = false;
}
Пример #8
0
 types::ndarray<typename E::dtype, types::array<long, E::value>>
 sort(E const &expr, long axis)
 {
   auto out = expr.copy();
   _sort(out, axis);
   return out;
 }
Пример #9
0
		void sort(Itr begin, Itr end)
		{
			_shuffle(begin, end);
			
			_sort(begin, end);
			
			_insertion_sort(begin, end);
		}
	void super_block_bin::update_blocks()
	{
		for (auto & it : blocks)
		{
			it->update();
		}

		_sort();
	}
Пример #11
0
surface_density_type pair_binner::delta_Sigma_x_stderr_for_bin(const distance_type & R, const mass_type & m,
													const flt_type & z, const flt_type & mag)
{
	_sort();
	ssize_t R_i = R_limits().get_bin_index(R);
	ssize_t m_i = m_limits().get_bin_index(m);
	ssize_t z_i = z_limits().get_bin_index(z);
	ssize_t mag_i = mag_limits().get_bin_index(mag);
	return _pair_bin_summaries_.at(R_i).at(m_i).at(z_i).at(mag_i).delta_Sigma_x_stderr();
}
Пример #12
0
void btv_balance(node *base, size_t *num, size_t width)
    {
    void *tmp;
    int ntmp = *num;
    tmp = malloc(width*ntmp);
    _index = 0;
    _sort(base->left, tmp, width);
    ntmp = *num;
    btv_deleteall(base, num);
    _index = 0;
    base->left = _balance(ntmp, tmp, width);
    *num = ntmp;
    free(tmp);
    }
Пример #13
0
void bti_balance(node *base, size_t *num)
    {
    int *tmp;
    int ntmp = *num;
    tmp = (int*)malloc(sizeof(int)*(ntmp));
    _index = 0;
    _sort(base->left, tmp);
    ntmp = *num;
    bti_deleteall(base, num);
    _index = 0;
    base->left = _balance(ntmp, tmp);
    *num = ntmp;
    free(tmp);
    }
void Profile::debugPrintDataSampleStyle() const
{
    typedef Vector<NameCountPair> NameCountPairVector;

    FunctionCallHashCount countedFunctions;
    printf("Call graph:\n");
    m_head->debugPrintDataSampleStyle(0, countedFunctions);

    printf("\nTotal number in stack:\n");
    NameCountPairVector sortedFunctions(countedFunctions.size());
    copyToVector(countedFunctions, sortedFunctions);

#if PLATFORM(ANDROID)
    _sort(sortedFunctions.begin(), sortedFunctions.end(), functionNameCountPairComparator);
#else
    std::sort(sortedFunctions.begin(), sortedFunctions.end(), functionNameCountPairComparator);
#endif
    for (NameCountPairVector::iterator it = sortedFunctions.begin(); it != sortedFunctions.end(); ++it)
        printf("        %-12d%s\n", (*it).second, UString((*it).first).UTF8String().c_str());

    printf("\nSort by top of stack, same collapsed (when >= 5):\n");
}
Пример #15
0
void eRenderJobQueue::render(const eCamera &cam, eInt renderWhat, eInt renderFlags)
{
    if (!m_sorted)
    {
        _sort();
        m_sorted = eTRUE;
    }

    for (eU32 i=0; i<m_jobsInUse; i++)
    {
        eRenderJob *job = m_jobs[i];
        eASSERT(job->insts.size() > 0);

        // check if job has to be rendered
        if (job->mat->blending && !(renderWhat & eRJW_ALPHA_ON))
            continue;
        else if (!job->mat->blending && !(renderWhat & eRJW_ALPHA_OFF))
            continue;

        if (job->mat->lighted && !(renderWhat & eRJW_LIGHTED_ON))
            continue;
        else if (!job->mat->lighted && !(renderWhat & eRJW_LIGHTED_OFF))
            continue;

        if (job->castsShadows && !(renderWhat & eRJW_SHADOWS_ON))
            continue;
        else if (!job->castsShadows && !(renderWhat & eRJW_SHADOWS_OFF))
            continue;

        // render geometry
        const eBool useMats = !(renderFlags & eRJF_MATERIALS_OFF);
        if (useMats)
            job->mat->activate();

        cam.activate(job->insts[0].modelMtx);
        eGfx->renderGeometry(job->geo, job->insts);
    }
}
Пример #16
0
void Tours::sort_jobs(){
    for(unsigned int i = 0; i < tours_.size(); ++i) 
        _sort(i);
}
Пример #17
0
surface_density_type pair_binner::delta_Sigma_x_stderr_for_bin(ssize_t R_i, ssize_t m_i, ssize_t z_i, ssize_t mag_i)
{
	_sort();
	return _pair_bin_summaries_.at(R_i).at(m_i).at(z_i).at(mag_i).delta_Sigma_t_stderr();
}
Пример #18
0
void pair_binner::sort() const
{
	_sort();
}
Пример #19
0
void precompute_muex(fmpz **mu, long M, 
                     const long **C, const long *lenC, 
                     const fmpz *a, long n, long p, long N)
{
    const long ve = (p == 2) ? M / 4 + 1 : M / (p * (p - 1)) + 1;

    fmpz_t P, pNe, pe;
    fmpz_t apow, f, g, h;

    fmpz *nu;
    long *v;

    long i, j;

    fmpz_init_set_ui(P, p);
    fmpz_init(pNe);
    fmpz_init(pe);
    fmpz_pow_ui(pNe, P, N + ve);
    fmpz_pow_ui(pe, P, ve);

    fmpz_init(apow);
    fmpz_init(f);
    fmpz_init(g);
    fmpz_init(h);

    /* Precompute $(l!)^{-1}$ */
    nu = _fmpz_vec_init(M + 1);
    v  = malloc((M + 1) * sizeof(long));

    {
        long *D, lenD = 0, k = 0;

        for (i = 0; i <= n; i++)
            lenD += lenC[i];

        D = malloc(lenD * sizeof(long));

        for (i = 0; i <= n; i++)
            for (j = 0; j < lenC[i]; j++)
                D[k++] = C[i][j];

        _remove_duplicates(D, &lenD);
        _sort(D, lenD);

        precompute_nu(nu, v, M, D, lenD, p, N + ve);

        free(D);
    }

    for (i = 0; i <= n; i++)
    {
        long m = -1, quo, idx, w;
        fmpz *z;

        /* Set apow = a[i]^{-(p-1)} mod p^N */
        fmpz_invmod(apow, a + i, pNe);
        fmpz_powm_ui(apow, apow, p - 1, pNe);

        /*
            Run over all relevant m in [0, M]. 
            Note that lenC[i] > 0 for all i.
         */
        for (quo = 0; m <= M; quo++)
        {
            for (idx = 0; idx < lenC[i]; idx++)
            {
                m = quo * p + C[i][idx];

                if (m > M)
                    break;

                /*
                    Note that $\mu_m$ is equal to 
                    $\sum_{k=0}^{\floor{m/p}} p^{\floor{m/p}-k}\nu_{m-pk}\nu_k$
                    where $\nu_i$ denotes the number with unit part nu[i] 
                    and valuation v[i].
                 */
                w = (p == 2) ? (3 * m) / 4 - (m == 3 || m == 7) : m / p;
                z = mu[i] + lenC[i] * quo + idx;
                fmpz_zero(z);
                fmpz_one(h);
                for (j = 0; j <= m / p; j++)
                {
                    fmpz_pow_ui(f, P, ve + w - j + v[m - p*j] + v[j]);
                    fmpz_mul(g, nu + (m - p*j), nu + j);

                    fmpz_mul(f, f, g);
                    fmpz_mul(f, f, h);

                    fmpz_add(z, z, f);
                    fmpz_mod(z, z, pNe);

                    /* Set h = a[i]^{- (j+1)(p-1)} mod p^{N+e} */
                    fmpz_mul(h, h, apow);
                    fmpz_mod(h, h, pNe);
                }
                fmpz_divexact(z, z, pe);
            }
        }
    }

    fmpz_clear(P);
    fmpz_clear(pNe);
    fmpz_clear(pe);

    fmpz_clear(apow);
    fmpz_clear(f);
    fmpz_clear(g);
    fmpz_clear(h);

    _fmpz_vec_clear(nu, M + 1);
    free(v);
} 
Пример #20
0
	anagram::anagram(const string& word)
	{
		_orig = boost::to_lower_copy(word);
		_sorted = _sort(_orig);
	}
Пример #21
0
	bool anagram::_matches(const string& word)
	{
		string cp = boost::to_lower_copy(word);

		return _sort(cp) == _sorted && _orig != cp;
	}
Пример #22
0
    void Polygon::push(const Ring &v, int beg, bool within)
    {
        int sz = v.size();
        double coords[2];

        mbr(v.xa(), v.ya(), v.xb(), v.yb());

        int f = 0, offset = 0;
        bool turn = false;
        double xa, ya, xb, yb;

        for (int i = beg; i < beg + sz; ) {

            int ind = i % sz;
            OuterRing rng;

            xa = xb = v[ind].x();
            ya = yb = v[ind].y();

            rng.push(xa, ya);

            if (turn && 0 == offset) {
                if (0 == f % 2) b_[f].push_back(index_t(xa, -f));
                else b_[f].push_back(index_t(ya, -f));
            }

            offset = 0;
            turn = false;

            for (int j = (ind+1) % sz; true; j = (j+1) % sz) {
                const point_t &p = v[j];
                rng.push(p.x(), p.y());

                if (p.x() < xa) xa = p.x();
                if (p.x() > xb) xb = p.x();
                if (p.y() < ya) ya = p.y();
                if (p.y() > yb) yb = p.y();

                coords[(f+1)%2] = p.x();
                coords[f%2] = p.y();

                if (fuzzy_eq(coords[0], mbr_[f])) {
                    turn = fuzzy_eq(coords[1], mbr_[(f+1)%4]);
                    break;
                } else if (fuzzy_eq(coords[1], mbr_[(f+1)%4])) {
                    rng.push(corner_[f].x(), corner_[f].y());
                    offset = 1;
                    turn = true;
                    break;
                }
            }

            int bak = rng.size() - 1 - offset;

            if (within) {
                int t = (f + offset) % 4;
                int kk = (1 == offset || !turn) ? outer_.size() : -f-1;

                if (0 == t % 2) b_[t].push_back(index_t(rng[bak].x(), kk));
                else b_[t].push_back(index_t(rng[bak].y(), kk));

                if (1 == offset) rng.corner();
            }

            if (turn) f = (f+1) % 4;
            i += bak;

            rng.mbr(xa, ya, xb, yb);
            outer_.push_back(rng);
        }

        if (within) _sort(f%4);
    }
Пример #23
0
void Btree_sort(Bnode *base)
{
    _sort(base->ptr[0]);
}
Пример #24
0
 ListNode *sortList(ListNode *head)
 {
     _sort(head, NULL);
     return head;
 }