示例#1
0
int main()
{
// Sequence creation
    std::vector<int> ilvec = initialize<int>();
    std::vector<int> ivec(ilvec.cbegin(), ilvec.cend());

    constexpr int arr_sz(11);
    ilvec = initialize<int>(arr_sz);
    std::array<int, arr_sz> ilarr({1,2,3,4,5,6,7,8,9,50,61});

    ilvec = initialize<int>();
    std::list<int> ilst(ilvec.cbegin(), ilvec.cend());

    ilvec = initialize<int>();
    std::forward_list<int> iflst(ilvec.cbegin(), ilvec.cend());

    ilvec = initialize<int>();
    std::deque<int> ideq(ilvec.cbegin(), ilvec.cend());

    int iarr[]{1, 2, 3, 6, 5, 4, 3, 44, 5, 232, 45, 76, 654, 96, 687, 4, 65, 89,343, 23};

// Print
    print(ivec, "ivec");
    print(ilarr, "ilarr");
    print(ilst, "ilst");
    print(iflst, "iflst");
    print(ideq, "ideq");
    print(iarr, "iarr");

// Use algorithms
    int val1 = 85, val2 = 54;
    std::cout << "sum = " << std::accumulate(ivec.cbegin(), ivec.cend(), 0) << std::endl;
    std::cout << "difference = " << std::accumulate(ideq.cbegin(), ideq.cend(), 10, diff<int>) << std::endl;
    std::cout << "inner product = " << std::inner_product(ilarr.cbegin(), ilarr.cend(),ilst.cbegin(), 0) << std::endl;
    std::cout << "custom inner product = " << std::inner_product(iflst.cbegin(), iflst.cend(), std::begin(iarr), 777, mod<int>, diff<int>) << std::endl;

    std::vector<int> cpy(ilst.size()), cpy1(ivec.size());
    std::partial_sum(ilst.cbegin(), ilst.cend(), cpy.begin());
    print(cpy, "cpy");

    std::partial_sum(ivec.cbegin(), ivec.cend(), cpy1.begin(), diff<int>);
    print(cpy1, "cpy1");

    std::vector<int> cpy2(arr_sz), cpy3(20);
    std::adjacent_difference(ilarr.cbegin(), ilarr.cend(), cpy2.begin());
    print(cpy2, "cpy2");

    std::adjacent_difference(std::begin(iarr), std::end(iarr), cpy3.begin(), mod<int>);
    print(cpy3, "cpy3");

    print(iflst, "iflst");
    std::iota(iflst.begin(), iflst.end(), val1);
    print(iflst, "iflst");

    return 0;
}
示例#2
0
// Turn v into a modifiable list including type t.
void addtype(V v, T t) {
  L l=L(v); T lt=l->t;
  if (lt&t || IMPURE(lt)) get(v);
  else {
    I n=l->l, c=next_pow_2(n), s=t_sizeof(lt);
    DECL_ARR(V,vs,c); DO(i,n) { vs[i] = cpy1(list_ats(l,i,s)); }
    if (l->r<=1) {
      FREE(l->p); l->r=1; l->t|=t; l->c=c; l->o=0; l->p=(P)vs;
    } else {
      l->r--; L(v) = wrapL(lt|t, c, n, 0, vs);
    }
  }
示例#3
0
void listcpy(L d, L s, I i) {
  s->r--; if (s->l) {
    d->l += s->l;
    if (s->r && s->t&COMP_t) {
      // Increase reference count of source's children
      I ss=t_sizeof(s->t);
      DO(j,s->l) (**(I**)(LIST_PTR_ATS(s,j,ss)))++;
    }
    if (PURE(d->t)==PURE(s->t)) listcpy1(d,s,i);
    else { DO(j,s->l) LIST_AT(d,i+j) = cpy1(list_at(s,j)); }
  }
  if (!s->r) { FREEL(s); }
}
void TestVectorManipulation(size_t n)
{
    typedef typename Vector::value_type T;

    thrust::host_vector<T> src = unittest::random_samples<T>(n);
    ASSERT_EQUAL(src.size(), n);

    // basic initialization
    Vector test0(n);
    Vector test1(n, (T) 3);
    ASSERT_EQUAL(test0.size(), n);
    ASSERT_EQUAL(test1.size(), n);
    ASSERT_EQUAL((test1 == std::vector<T>(n, (T) 3)), true);
   
    // initializing from other vector
    std::vector<T> stl_vector(src.begin(), src.end());
    Vector cpy0 = src;
    Vector cpy1(stl_vector);
    Vector cpy2(stl_vector.begin(), stl_vector.end());
    ASSERT_EQUAL(cpy0, src);
    ASSERT_EQUAL(cpy1, src);
    ASSERT_EQUAL(cpy2, src);

    // resizing
    Vector vec1(src);
    vec1.resize(n + 3);
    ASSERT_EQUAL(vec1.size(), n + 3);
    vec1.resize(n);
    ASSERT_EQUAL(vec1.size(), n);
    ASSERT_EQUAL(vec1, src); 
    
    vec1.resize(n + 20, (T) 11);
    Vector tail(vec1.begin() + n, vec1.end());
    ASSERT_EQUAL( (tail == std::vector<T>(20, (T) 11)), true);

    vec1.resize(0);
    ASSERT_EQUAL(vec1.size(), 0);
    ASSERT_EQUAL(vec1.empty(), true);
    vec1.resize(10);
    ASSERT_EQUAL(vec1.size(), 10);
    vec1.clear();
    ASSERT_EQUAL(vec1.size(), 0);
    vec1.resize(5);
    ASSERT_EQUAL(vec1.size(), 5);

    // push_back
    Vector vec2;
    for(size_t i = 0; i < 10; ++i){
        ASSERT_EQUAL(vec2.size(), i);
        vec2.push_back( (T) i );
        ASSERT_EQUAL(vec2.size(), i + 1);
        for(size_t j = 0; j <= i; j++)
            ASSERT_EQUAL(vec2[j],     j);
        ASSERT_EQUAL(vec2.back(), i);
    }

    // pop_back
    for(size_t i = 10; i > 0; --i){
        ASSERT_EQUAL(vec2.size(), i);
        ASSERT_EQUAL(vec2.back(), i-1);
        vec2.pop_back();
        ASSERT_EQUAL(vec2.size(), i-1);
        for(size_t j = 0; j < i; j++)
            ASSERT_EQUAL(vec2[j], j);
    }

    //TODO test swap, erase(pos), erase(begin, end)
}