Esempio n. 1
0
    void serialize_array_impl(SF::Archive & ar, ArrayType & a)
    {
        if (ar.isRead())
        {
            unsigned int count = 0;
            ar & count;

            RCF_VERIFY(
                count == a.size(), 
                RCF::Exception(RCF::_RcfError_RcfError_ArraySizeMismatch(a.size(), count)));

            for (std::size_t i=0; i<a.size(); ++i)
            {
                ar & a[i];
            }
        }
        else if (ar.isWrite())
        {
            unsigned int count = a.size();
            ar & count;

            for (std::size_t i=0; i<a.size(); ++i)
            {
                ar & a[i];
            }
        }
    }
auto FindTheMissingNumberXor(const ArrayType &integers) {
    assert(not integers.empty());

    const auto N = integers.size() + 1u;
    auto xor_of_all = integers[0];
    for (ArrayType::size_type i = 1ul; i < integers.size(); ++i) {
        xor_of_all ^= (i ^ integers[i]);
    }

    return xor_of_all ^ N ^ integers.size();
}
auto RearrangeArrayInPlace(ArrayType elements) {
    for (ArrayType::size_type i = 0; i < elements.size(); ++i) {
        elements[elements[i] % elements.size()] += i * elements.size();
    }

    std::transform(elements.cbegin(), elements.cend(),
    elements.begin(), [&elements](const ArrayType::value_type v) {
        return v / elements.size();
    });

    return elements;
}
Esempio n. 4
0
Epetra_Map_Ptr CreateWavefunctionEpetraMap(typename Wavefunction<Rank>::Ptr psi)
{
	typedef blitz::Array<cplx, Rank> ArrayType;
	

	ArrayType data = psi->GetData();
	typename DistributedModel<Rank>::Ptr distr = psi->GetRepresentation()->GetDistributedModel();
	Epetra_Comm_Ptr comm = CreateDistributedModelEpetraComm<Rank>(distr);

	int localSize = data.size();
	int* myElems = new int[localSize*2];

	//Get strides of the global Rank-dimensional array
	blitz::TinyVector<int, Rank> globalShape = psi->GetRepresentation()->GetFullShape();
	blitz::TinyVector<int, Rank> globalStrides;
	globalStrides(Rank-1) = 1;
	for (int rank=Rank-2; rank>=0; rank--)
	{
		globalStrides(rank) = globalStrides(rank+1)	* globalShape(rank+1);
	}

	//Get maps to global indices for each rank
	blitz::TinyVector<blitz::Range, Rank> globalIndices;
	for (int rank=0; rank<Rank; rank++)
	{
		globalIndices(rank) = distr->GetLocalIndexRange(globalShape(rank), rank);
	}
	
	typename blitz::Array<cplx, Rank>::iterator it = data.begin();
	for (int linearCount=0; linearCount<data.size(); linearCount++)
	{
		//Get global position for the current element
		int globalPos = 0;
		for (int rank=0; rank<Rank; rank++)
		{
			int rankPos = it.position()(rank);
			globalPos += globalStrides(rank) * globalIndices(rank)(rankPos);
		}

		//real component
		myElems[2*linearCount] = 2*globalPos;
		//imaginary component
		myElems[2*linearCount+1] = 2*globalPos + 1;
			
		++it;	
	}

	return Epetra_Map_Ptr( new Epetra_Map(-1, localSize*2, myElems, 0, *comm) );
}
/** Find the Missing Number
 *
 * @reference   https://www.geeksforgeeks.org/find-the-missing-number/
 *
 * You are given a list of n-1 integers and these integers are in the range of 1 to n.
 * There are no duplicates in list. One of the integers is missing in the list.
 * Write an efficient code to find the missing integer.
 *
 * @complexity  O(n)
 */
auto FindTheMissingNumberSum(const ArrayType &integers) {
    assert(not integers.empty());

    const auto N = integers.size() + 1u;
    const auto sum = N * (N + 1) / 2;
    return std::accumulate(integers.cbegin(), integers.cend(), sum, std::minus<ArrayType::value_type> {});
}
/** Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ | Set 1
 *
 * @reference   https://www.geeksforgeeks.org/rearrange-array-arrj-becomes-arri-j/
 *
 * Given an array of size n where all elements are distinct and in range from 0 to n-1,
 * change contents of arr[] so that arr[i] = j is changed to arr[j] = i.
 */
auto RearrangeArraySimple(const ArrayType &elements) {
    auto output = elements;

    for (ArrayType::size_type i = 0; i < elements.size(); ++i) {
        const auto j = elements[i];
        output[j] = i;
    }

    return output;
}
Esempio n. 7
0
void doWork(const std::string &input_file, const std::string &output_file) {
    ArrayType data = readFromFile<ArrayType>(input_file);

    std::cout << "Sorting " << data.size() << " elements..." << std::endl;
    std::clock_t start_time = std::clock();

    ArrayType sorted_data = doSort(data);

    std::clock_t end_time = std::clock();
    const double duration = (end_time - start_time)/double(CLOCKS_PER_SEC);
    std::cout << "Sorting took " << duration << " seconds" << std::endl;

    writeToFile(output_file, sorted_data);
}
Esempio n. 8
0
 inline Vector_const(const ArrayType &inArray,
     typename enable_if<
             boost::is_base_of< const_multi_array_ref<eT, 1>, ArrayType >
         >::type *dummy = NULL)
     : mMemoryHandle(inArray.memoryHandle()),
       mVector(
         const_cast<eT*>(inArray.data()),
         inArray.size(),
         false /* copy_aux_mem */,
         true /* strict */),
       n_rows(mVector.n_rows),
       n_cols(mVector.n_cols),
       n_elem(mVector.n_elem)
     { }
/** Find Duplicates of array using bit array
 *
 * @reference   https://www.geeksforgeeks.org/find-duplicates-of-array-using-bit-array/
 *
 * You have an array of N numbers, where N is at most 32,000. The array may have
 * duplicates entries and you do not know what N is. With only 4 Kilobytes of
 * memory available, how would print all duplicates elements in the array ?
 *
 * @complexity  O(n)
 */
auto FindAllDuplicatesBitArray(const ArrayType &elements) {
    std::vector<bool> counters(elements.size(), false);

    ArrayType output;
    for (const auto elem : elements) {
        if (counters[elem]) {
            output.push_back(elem);
        } else {
            counters[elem] = true;
        }
    }

    return output;
}
Esempio n. 10
0
auto CountInversionsMergeSort(ArrayType values) {
    return MergeSort(values.begin(), values.size());
}