DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
OptimizedIndexSet.t.cc
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (c) 2017-2025 The Regents of the University of Michigan and DFT-FE
4// authors.
5//
6// This file is part of the DFT-FE code.
7//
8// The DFT-FE code is free software; you can use it, redistribute
9// it, and/or modify it under the terms of the GNU Lesser General
10// Public License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12// The full text of the license can be found in the file LICENSE at
13// the top level of the DFT-FE distribution.
14//
15// ---------------------------------------------------------------------
16//
17
18/*
19 * @author Bikash Kanungo
20 */
21
22#include <iterator>
23#include <algorithm>
24#include <type_traits>
25#include <Exceptions.h>
26namespace dftfe
27{
28 namespace utils
29 {
30 //
31 // Constructor
32 //
33 template <typename T>
35 const std::set<T> &inputSet /*=std::set<T>()*/)
39 {
40 bool isValid = std::is_same<size_type, T>::value ||
41 std::is_same<global_size_type, T>::value;
43 isValid,
44 "OptimizedIndexSet expects the template parameter to be of type unsigned int or unsigned long int.");
45 if (!inputSet.empty())
46 {
47 typename std::set<T>::const_iterator itLastRange = inputSet.begin();
48 typename std::set<T>::const_iterator itPrev = inputSet.begin();
49 typename std::set<T>::const_iterator it = itPrev;
50 it++;
51 for (; it != inputSet.end(); ++it)
52 {
53 bool isContiguous = ((*it - 1) == *(itPrev));
54 if (!isContiguous)
55 {
56 d_contiguousRanges.push_back(*itLastRange);
57 d_contiguousRanges.push_back(*(itPrev) + 1);
58 itLastRange = it;
59 }
60 itPrev = it;
61 }
62
63 d_contiguousRanges.push_back(*itLastRange);
64 d_contiguousRanges.push_back(*(itPrev) + 1);
65
68 size_type cumulativeEntries = 0;
69 for (unsigned int i = 0; i < d_numContiguousRanges; ++i)
70 {
71 d_numEntriesBefore[i] = cumulativeEntries;
72 cumulativeEntries +=
73 d_contiguousRanges[2 * i + 1] - d_contiguousRanges[2 * i];
74 }
75 }
76 }
77
78 template <typename T>
79 void
81 size_type &pos,
82 bool & found) const
83 {
84 found = false;
85 /*
86 * The logic used for finding an index is as follows:
87 * 1. Find the position of the element in d_contiguousRanges
88 * which is greater than (strictly greater) the input index.
89 * Let's call this position as upPos and the value at upPos as
90 * upVal. The complexity of finding it is
91 * O(log(size of d_contiguousRanges))
92 * 2. Since d_contiguousRanges stores pairs of startId and endId
93 * (endId not inclusive) of contiguous ranges in inputSet,
94 * any index for which upPos is even (i.e., it corresponds to a
95 * startId) cannot belong to inputSet. Why? Consider two consequtive
96 * ranges [k1,k2) and [k3,k4) where k1 < k2 < k3 < k4. If upVal for index
97 * corresponds to k3 (i.e., startId of a range), then (a) index does not
98 * lie in the [k3,k4) as index < upVal (=k3). (b) index cannot lie in
99 * [k1,k2), because if index lies in [k1,k2), then upVal should be k2 (not
100 * k3)
101 * 3. If upPos is odd (i.e, it corresponds to an endId), we find the
102 * relative position of index in that range. Subsequently, we determine
103 * the global position of index in inputSet by adding the relative
104 * position to the number of entries in inputSet prior to the range where
105 * index lies
106 */
107
108 auto up = std::upper_bound(d_contiguousRanges.begin(),
109 d_contiguousRanges.end(),
110 index);
111 if (up != d_contiguousRanges.end())
112 {
113 size_type upPos = std::distance(d_contiguousRanges.begin(), up);
114 if (upPos % 2 == 1)
115 {
116 found = true;
117 size_type rangeId = upPos / 2;
118 pos = d_numEntriesBefore[rangeId] + index -
119 d_contiguousRanges[upPos - 1];
120 }
121 }
122 }
123
124 template <typename T>
125 bool
127 {
129 return false;
130 else
132 }
133 } // end of namespace utils
134} // end of namespace dftfe
size_type d_numContiguousRanges
Store the number of contiguous ranges in the input set of indices.
Definition OptimizedIndexSet.h:64
std::vector< size_type > d_numEntriesBefore
Definition OptimizedIndexSet.h:77
std::vector< T > d_contiguousRanges
Definition OptimizedIndexSet.h:73
void getPosition(const T &index, size_type &pos, bool &found) const
Definition OptimizedIndexSet.t.cc:80
OptimizedIndexSet(const std::set< T > &inputSet=std::set< T >())
Constructor.
Definition OptimizedIndexSet.t.cc:34
Definition Cell.h:36
void throwException(bool condition, std::string msg="")
Definition pseudoPotentialToDftfeConverter.cc:34
unsigned int size_type
Definition TypeConfig.h:6