Esempio n. 1
0
int main()
{
	vector<int> vec(10);              // default initialized to 0
	fill(vec.begin(), vec.end(), 1);  // reset each element to 1

	// sum the elements in vec starting the summation with the value 0
	int sum = accumulate(vec.cbegin(), vec.cend(), 0);
	cout << sum << endl;
	
	// set a subsequence of the container to 10
	fill(vec.begin(), vec.begin() + vec.size()/2, 10);
	cout << accumulate(vec.begin(), vec.end(), 0) << endl;

	// reset the same subsequence to 0
	fill_n(vec.begin(), vec.size()/2, 0);
	cout << accumulate(vec.begin(), vec.end(), 0) << endl;

	// create 10 elements on the end of vec each with the value 42
	fill_n(back_inserter(vec), 10, 42);
	cout << accumulate(vec.begin(), vec.end(), 0) << endl;
	
	// concatenate elements in a vector of strings and store in sum 
	vector<string> v;
	string s;
	while (cin >> s)
		v.push_back(s);
	string concat = accumulate(v.cbegin(), v.cend(), string(""));
	cout << concat << endl;
	
	return 0;
}
Esempio n. 2
0
void operators_test() {
  long xnr = 100;
  long ynr = 10;
  long nc = 10;

  LDL_ts x(xnr,nc);
  LDL_ts y(ynr,nc);

  // gernate data
  fill_n(x.getData(),x.nrow()*x.ncol(),100.0);
  fill_n(y.getData(),y.nrow()*y.ncol(),5.0);

  // generate dates
  for(long xi = 0; xi < x.nrow(); xi++)
    x.getDates()[xi] = xi;

  // generate dates
  for(long yi = 0; yi < y.nrow(); yi++)
    y.getDates()[yi] = yi;

  LDL_ts Zplus = x + y;
  LDL_ts Zminus = x - y;
  LDL_ts Zmultiplies = x * y;
  LDL_ts Zdivides = x / y;

  BOOST_CHECK_EQUAL( Zplus.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zplus.ncol(), 10 );

  BOOST_CHECK_EQUAL( Zminus.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zminus.ncol(), 10 );

  BOOST_CHECK_EQUAL( Zmultiplies.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zmultiplies.ncol(), 10 );

  BOOST_CHECK_EQUAL( Zdivides.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zdivides.ncol(), 10 );

  // add test for all== later
  LDL_ts XplusS = x + 100.0;
  LDL_ts SplusX = 100.0 + x;

  LDL_ts XminusS = x - 100.0;
  LDL_ts SminusX = 100.0 - x;

  LDL_ts XmultS = x * 100.0;
  LDL_ts SmultX = 100.0 * x;

  LDL_ts XdivS = x / 100.0;
  LDL_ts SdivX = 100.0 * x;

  vector<bool> xb_eq_scalar = x == 10;
  vector<bool> xb_neq_ts = x != y;
  vector<bool> xb_gt_ts = x > y;
  vector<bool> xb_gte_ts = x >= y;
  vector<bool> xb_lt_ts = x < y;
  vector<bool> xb_lte_ts = x <= y;
}
Esempio n. 3
0
int main()
{
	vector<int>ivec(10);		//default initialized to 0
	fill(ivec.begin(), ivec.end(), 1);		//reset each element to 1

	//print elements in ivec
	auto iter = ivec.begin();
	while (iter != ivec.end())
		cout << *iter++ << " ";
	cout << endl;
	//sum the elements in ivec starting the summation with the value 0
	int sum = accumulate(ivec.begin(), ivec.end(), 0);
	cout << sum << endl;

	//set a subsequence of the container to 10
	fill(ivec.begin(), ivec.begin() + ivec.size() / 2, 10);
	cout << accumulate(ivec.begin(), ivec.end(), 0) << endl;

	//reset the same subsequence to 0
	fill_n(ivec.begin(), ivec.size() / 2, 0);
	cout << accumulate(ivec.begin(), ivec.end(), 0) << endl;

	//concatenates elements	in a vector of strings and store in sum
	vector<string>v;
	string s;
	while (cin >> s)
		v.push_back(s);
	string concat = accumulate(v.begin(), v.end(), string(""));
	cout << concat << endl;
	getchar();
}
int main() {
	vector<int> vec{1, 2, 3, 4, 5, 6};
	fill_n(vec.begin(), vec.size(), 7);
	for (auto i : vec)
		cout << i << " ";
	cout << endl;
	return 0;
}
Esempio n. 5
0
int main()
{
    vector<int> vec{0,1,2,3,4,5,6,7,8,9};
    fill_n(vec.begin(), vec.size(), 0);
    
    for (auto i : vec)
        cout << i << " ";
    cout << endl;
}
Esempio n. 6
0
int main()
{
    vector<int> ivec = {1,2,3,4,5,6,7,8,9};
    fill_n(ivec.begin(), ivec.size(), 0);
    for(auto c : ivec)
        cout << c << " ";
    cout << endl;
    return 0;
}
Esempio n. 7
0
void mixed_operators_test() {
  long xnr = 100;
  long ynr = 10;
  long nc = 10;

  LDL_ts x(xnr,nc);
  LIL_ts y(ynr,nc);

  // gernate data
  fill_n(x.getData(),x.nrow()*x.ncol(),100.0);
  fill_n(y.getData(),y.nrow()*y.ncol(),5);

  // generate dates
  for(long xi = 0; xi < x.nrow(); xi++)
    x.getDates()[xi] = xi;

  // generate dates
  for(long yi = 0; yi < y.nrow(); yi++)
    y.getDates()[yi] = yi;

  LDL_ts Zplus = x + y;
  LDL_ts Zminus = x - y;
  LDL_ts Zmultiplies = x * y;
  LDL_ts Zdivides = x / y;

  BOOST_CHECK_EQUAL( Zplus.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zplus.ncol(), 10 );

  BOOST_CHECK_EQUAL( Zminus.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zminus.ncol(), 10 );

  BOOST_CHECK_EQUAL( Zmultiplies.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zmultiplies.ncol(), 10 );

  BOOST_CHECK_EQUAL( Zdivides.nrow(), y.nrow() );
  BOOST_CHECK_EQUAL( Zdivides.ncol(), 10 );

  vector<bool> xb_eq_scalar = x == static_cast<int>(10);
  vector<bool> xb_neq_ts = x != y;
  vector<bool> xb_gt_ts = x > y;
  vector<bool> xb_gte_ts = x >= y;
  vector<bool> xb_lt_ts = x < y;
  vector<bool> xb_lte_ts = x <= y;
}
Esempio n. 8
0
template <typename T, typename MemoryBlock> void save(
    archive& ar
    , std::string const& path
    , alps::numeric::matrix<T, MemoryBlock> const& m
    , std::vector<std::size_t> size   = std::vector<std::size_t>()
                                        , std::vector<std::size_t> chunk  = std::vector<std::size_t>()
                                                , std::vector<std::size_t> offset = std::vector<std::size_t>()
) {
    using std::copy;
    using std::fill_n;
    using alps::cast;
    typedef typename alps::numeric::matrix<T,MemoryBlock>::const_col_element_iterator col_iterator;
    if (is_continuous<T>::value && m.empty())
        ar.write(path, static_cast<typename scalar_type<alps::numeric::matrix<T, MemoryBlock> >::type const *>(NULL), std::vector<std::size_t>());
    else if (is_continuous<T>::value) {
        std::vector<std::size_t> extent(get_extent(m));
        copy(extent.begin(),extent.end(), std::back_inserter(size));
        // We want to write one column:
        chunk.push_back(1);
        // How much memory does the column and the elements it contains need?
        copy(extent.begin()+1,extent.end(), std::back_inserter(chunk));
        std::size_t const offset_col_index = offset.size();
        fill_n(std::back_inserter(offset), extent.size(), 0);
        // Write column by column
        for(std::size_t j=0; j < num_cols(m); ++j) {
            offset[offset_col_index] = j;
            ar.write(path, get_pointer(*(col(m, j).first)), size, chunk, offset);
        }
    } else if (m.empty())
        ar.write(path, static_cast<int const *>(NULL), std::vector<std::size_t>());
    else if (is_vectorizable(m)) {
        size.push_back(num_cols(m));
        size.push_back(num_rows(m));
        // We want to write element by element:
        chunk.push_back(1);
        chunk.push_back(1);
        std::size_t const offset_col_index = offset.size();
        std::size_t const offset_row_index = offset.size()+1;
        offset.push_back(0);
        offset.push_back(0);
        for(std::size_t j=0; j < num_cols(m); ++j) {
            offset[offset_col_index] = j;
            for(std::size_t i=0; i< num_rows(m); ++i) {
                offset[offset_row_index] = i;
                save(ar, path, m(i,j), size, chunk, offset);
            }
        }
    } else {
        if( ar.is_data(path) )
            ar.delete_data(path);
        for(std::size_t j=0; j < num_cols(m); ++j)
            for(std::size_t i=0; i < num_rows(m); ++i)
                save(ar, ar.complete_path(path) + "/" + cast<std::string>(j) + "/" + cast<std::string>(i), m(i,j) );
    }
}
Esempio n. 9
0
int main(void)
{
    constexpr int size = 10;
    vector<int> ivec(size);

    fill_n(ivec.begin(), ivec.size(), 1);
    for (const int i : ivec)
        cout << i << " ";
    cout << endl;

    return 0;
}
Esempio n. 10
0
void assignment_test() {
  long xnr = 10;
  long xnc = 2;

  long ynr = 100;
  long ync = 50;

  LDL_ts x(xnr,xnc);
  LDL_ts y(ynr,ync);

  fill_n(x.getData(),x.nrow()*x.ncol(),1.0);
  fill_n(y.getData(),y.nrow()*y.ncol(),2.0);

  // test self assignment
  x = x;

  BOOST_CHECK_EQUAL(x.nrow(),xnr);
  BOOST_CHECK_EQUAL(x.ncol(),xnc);
  BOOST_CHECK_EQUAL(x.getData()[0],1.0);


  // TS TS assignment
  BOOST_CHECK_EQUAL(y.getData()[0],2.0);
  y = x;

  BOOST_CHECK_EQUAL(y.nrow(),xnr);
  BOOST_CHECK_EQUAL(y.ncol(),xnc);
  BOOST_CHECK_EQUAL(y.getData()[0],1.0);

  x = 100.0;

  // check that dims are same as before
  BOOST_CHECK_EQUAL(x.nrow(),xnr);
  BOOST_CHECK_EQUAL(x.ncol(),xnc);

  // now all values of x shoud be == 100.0
  for(long i = 0; i < x.nrow()*x.ncol(); i++) {
    BOOST_CHECK_EQUAL(x.getData()[i],100);
  }
}
Esempio n. 11
0
 int minCut(string s) {
     const int n = s.size();
     int f[n+1];
     bool p[n][n];
     fill_n(&p[0][0], n * n, false);
     for (int i = 0; i <= n; i++)
         f[i] = n - i - 1;
     for (int i = n; i >= 0; i--) {
         for (int j = i; j < n; j++) {
             if (s[i] == s[j] && (j - i < 2 || p[i + 1][j - 1])) {
                 p[i][j] = true;
                 f[i] = min(f[i], f[j+1] + 1);
             }
         }
     }
     return f[0];
 }
Esempio n. 12
0
Registers::Registers()
{
    // Set all registers to zero.
    fill_n( registers_, 32, 0 );
    regmap_["$zero"] =  0;
    regmap_["$at"] =  1;
    regmap_["$v0"] =  2;
    regmap_["$v1"] =  3;
    regmap_["$a0"  ] =  4;
    regmap_["$a1"] =  5;
    regmap_["$a2"] =  6;
    regmap_["$a3"] =  7;
    regmap_["$t0"  ] =  8;
    regmap_["$t1"] =  9;
    regmap_["$t2"] = 10;
    regmap_["$t3"] = 11;
    regmap_["$t4"  ] = 12;
    regmap_["$t5"] = 13;
    regmap_["$t6"] = 14;
    regmap_["$t7"] = 15;
    regmap_["$s0"  ] = 16;
    regmap_["$s1"] = 17;
    regmap_["$s2"] = 18;
    regmap_["$s3"] = 19;
    regmap_["$s4"  ] = 20;
    regmap_["$s5"] = 21;
    regmap_["$s6"] = 22;
    regmap_["$s7"] = 23;
    regmap_["$t8"  ] = 24;
    regmap_["$t9"] = 25;
    regmap_["$k0"] = 26;
    regmap_["$k1"] = 27;
    regmap_["$gp"  ] = 28;
    regmap_["$sp"] = 29;
    regmap_["$fp"] = 30;
    regmap_["$ra"] = 31;
}
Esempio n. 13
0
// ********************************************************************************************
//
// ********************************************************************************************
AmbCodes::AmbCodes()
{
	using std::fill_n;

	fill_n(trans_amb_codes, 256, 0);
	trans_amb_codes['Y'] = 2;
	trans_amb_codes['R'] = 3;
	trans_amb_codes['W'] = 4;
	trans_amb_codes['S'] = 5;
	trans_amb_codes['K'] = 6;
	trans_amb_codes['M'] = 7;
	trans_amb_codes['D'] = 8;
	trans_amb_codes['V'] = 9;
	trans_amb_codes['H'] = 10;
	trans_amb_codes['B'] = 11;
	trans_amb_codes['N'] = 12;
	trans_amb_codes['X'] = 13;
	trans_amb_codes['U'] = 14;
	trans_amb_codes['.'] = 15;
	trans_amb_codes['-'] = 16;
	trans_amb_codes['A'] = 1;
	trans_amb_codes['C'] = 1;
	trans_amb_codes['T'] = 1;
	trans_amb_codes['G'] = 1;

	fill_n(untrans_amb_codes+128+ 0*8, 8, 'Y');
	fill_n(untrans_amb_codes+128+ 1*8, 8, 'R');
	fill_n(untrans_amb_codes+128+ 2*8, 8, 'W');
	fill_n(untrans_amb_codes+128+ 3*8, 8, 'S');
	fill_n(untrans_amb_codes+128+ 4*8, 8, 'K');
	fill_n(untrans_amb_codes+128+ 5*8, 8, 'M');
	fill_n(untrans_amb_codes+128+ 6*8, 8, 'D');
	fill_n(untrans_amb_codes+128+ 7*8, 8, 'V');
	fill_n(untrans_amb_codes+128+ 8*8, 8, 'H');
	fill_n(untrans_amb_codes+128+ 9*8, 8, 'B');
	fill_n(untrans_amb_codes+128+10*8, 8, 'N');
	fill_n(untrans_amb_codes+128+11*8, 8, 'X');
	fill_n(untrans_amb_codes+128+12*8, 8, 'U');
	fill_n(untrans_amb_codes+128+13*8, 8, '.');
	fill_n(untrans_amb_codes+128+14*8, 8, '-');
}