Skip to content

Commit efb3752

Browse files
Merge pull request #21 from gismo/hot-fix-lookup
Fix the lookup function to use const reference
2 parents a2b4171 + af95366 commit efb3752

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

gsLookupFunction.h

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ class gsLookupFunction : public gsFunctionSet<T>
3434
typedef memory::unique_ptr< gsLookupFunction > uPtr;
3535
typedef gsLookupFunctionSingle<T> Piece_t;
3636

37-
typedef std::vector<Piece_t> Container;
37+
// Using unique_ptr
38+
typedef std::vector<memory::unique_ptr<Piece_t>> Container;
3839

3940
// Constructor
4041
gsLookupFunction(size_t nPatches = 0)
41-
: m_container()
42+
: m_container(nPatches)
4243
{
43-
m_container.reserve(nPatches);
44+
// Use n nullptrs to initialize the container
4445
}
4546
/// Constructor with points and data for a single patch
4647
gsLookupFunction(const gsMatrix<T> & points,
@@ -55,7 +56,24 @@ class gsLookupFunction : public gsFunctionSet<T>
5556
{
5657
}
5758

58-
GISMO_CLONE_FUNCTION(gsLookupFunction)
59+
// clone function for deep copying the lookup function
60+
private:
61+
virtual gsLookupFunction* clone_impl() const override
62+
{
63+
gsLookupFunction* result = new gsLookupFunction(m_container.size());
64+
// Deep copy each element
65+
for (size_t i = 0; i < m_container.size(); ++i) {
66+
if (m_container[i]) {
67+
result->m_container[i].reset(new Piece_t(*m_container[i]));
68+
}
69+
}
70+
return result;
71+
}
72+
public:
73+
memory::unique_ptr<gsLookupFunction> clone() const
74+
{
75+
return memory::unique_ptr<gsLookupFunction>(clone_impl());
76+
}
5977

6078
/// Number of pieces
6179
index_t nPieces() const override
@@ -65,7 +83,8 @@ class gsLookupFunction : public gsFunctionSet<T>
6583
const gsFunction<T> & piece(const index_t k) const override
6684
{
6785
GISMO_ASSERT(k < m_container.size(), "Piece index out of bounds");
68-
return m_container[k];
86+
GISMO_ASSERT(m_container[k], "Piece at index " + std::to_string(k) + " is not initialized");
87+
return *m_container[k];
6988
}
7089

7190
/// See \a gsFunctionSet
@@ -75,16 +94,16 @@ class gsLookupFunction : public gsFunctionSet<T>
7594
/// Domain dimension
7695
short_t domainDim() const override
7796
{
78-
if (m_container.size() > 0)
79-
return m_container[0].domainDim();
97+
if (!m_container.empty() && m_container[0])
98+
return m_container[0]->domainDim();
8099
return 0;
81100
}
82101

83102
/// Target dimension
84103
short_t targetDim() const override
85104
{
86-
if (m_container.size() > 0)
87-
return m_container[0].targetDim();
105+
if (!m_container.empty() && m_container[0])
106+
return m_container[0]->targetDim();
88107
return 0;
89108
}
90109

@@ -110,21 +129,22 @@ class gsLookupFunction : public gsFunctionSet<T>
110129
void eval_into(const index_t patch, const gsMatrix<T>& u, gsMatrix<T>& result) const
111130
{
112131
GISMO_ASSERT(patch < m_container.size(), "Patch index out of bounds");
113-
m_container[patch].eval_into(u, result);
132+
GISMO_ASSERT(m_container[patch], "Lookup function for the specified patch is null");
133+
m_container[patch]->eval_into(u, result);
114134
}
115135

116136
/// Evaluate derivatives for specific patch
117137
void deriv_into(const index_t patch, const gsMatrix<T>& u, gsMatrix<T>& result) const
118138
{
119139
GISMO_ASSERT(patch < m_container.size(), "Patch index out of bounds");
120-
m_container[patch].deriv_into(u, result);
140+
m_container[patch]->deriv_into(u, result);
121141
}
122142

123143
/// Evaluate second derivatives for specific patch
124144
void deriv2_into(const index_t patch, const gsMatrix<T>& u, gsMatrix<T>& result) const
125145
{
126146
GISMO_ASSERT(patch < m_container.size(), "Patch index out of bounds");
127-
m_container[patch].deriv2_into(u, result);
147+
m_container[patch]->deriv2_into(u, result);
128148
}
129149

130150
/// Get the lookup function container
@@ -136,14 +156,16 @@ class gsLookupFunction : public gsFunctionSet<T>
136156
/// Add lookup function (appends to the end)
137157
void add(const gsMatrix<T> & points, const gsMatrix<T> & data)
138158
{
139-
m_container.push_back(gsLookupFunctionSingle<T>(points, data));
159+
// Use unique_ptr - more efficient than shared_ptr
160+
m_container.push_back(memory::make_unique(new Piece_t(points, data)));
140161
}
141162

142163
/// Set lookup function for a specific patch index
143164
void set(index_t patch, const gsMatrix<T> & points, const gsMatrix<T> & data)
144165
{
145166
GISMO_ASSERT(patch < m_container.size(), "Patch index out of bounds");
146-
m_container[patch] = gsLookupFunctionSingle<T>(points, data);
167+
// Reset with unique_ptr - old object is automatically destroyed
168+
m_container[patch].reset(new Piece_t(points, data));
147169
}
148170

149171
protected:
@@ -302,8 +324,8 @@ class gsLookupFunctionSingle : public gsFunction<T>
302324

303325
protected:
304326

305-
gsMatrix<T> m_points;
306-
gsMatrix<T> m_data;
327+
const gsMatrix<T>& m_points;
328+
const gsMatrix<T>& m_data;
307329

308330
std::map<gsVector<T>,index_t,Compare> m_map;
309331

0 commit comments

Comments
 (0)