Skip to content

Commit ac169dc

Browse files
committed
Add function to make a flat list of any value.
1 parent e653942 commit ac169dc

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Sources/NodeEngine/NE_Value.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,13 @@ void FlatEnumerate (const ValueConstPtr& value, const std::function<void (const
212212
});
213213
}
214214

215+
ValueConstPtr FlattenValue (const ValueConstPtr& value)
216+
{
217+
ListValuePtr listValue (new ListValue ());
218+
FlatEnumerate (value, [&] (const ValueConstPtr& value) {
219+
listValue->Push (value);
220+
});
221+
return listValue;
222+
}
223+
215224
}

Sources/NodeEngine/NE_Value.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ bool IsComplexType (const ValueConstPtr& val)
208208
ValueConstPtr CreateSingleValue (const ValueConstPtr& value);
209209
IListValueConstPtr CreateListValue (const ValueConstPtr& value);
210210
void FlatEnumerate (const ValueConstPtr& value, const std::function<void (const ValueConstPtr&)>& processor);
211+
ValueConstPtr FlattenValue (const ValueConstPtr& value);
211212

212213
}
213214

Sources/NodeEngineTest/ValueTest.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,25 @@ TEST (ConstNonConstTest)
165165
ASSERT (IntValue::Get (cval) == 5);
166166
}
167167

168+
TEST (FlattenTest)
169+
{
170+
ListValuePtr theListValue (new ListValue ());
171+
172+
ValuePtr innerSingleValue (new IntValue (1));
173+
ListValuePtr innerListValue (new ListValue ());
174+
innerListValue->Push (ValuePtr (new IntValue (2)));
175+
innerListValue->Push (ValuePtr (new IntValue (3)));
176+
177+
theListValue->Push (innerSingleValue);
178+
theListValue->Push (innerListValue);
179+
180+
ValueConstPtr flatten = FlattenValue (theListValue);
181+
ASSERT (Value::IsType<ListValue> (flatten));
182+
ListValueConstPtr flattenList = Value::Cast<ListValue> (flatten);
183+
ASSERT (flattenList->GetSize () == 3);
184+
ASSERT (IntValue::Get (flattenList->GetValue (0)) == 1);
185+
ASSERT (IntValue::Get (flattenList->GetValue (1)) == 2);
186+
ASSERT (IntValue::Get (flattenList->GetValue (2)) == 3);
187+
}
188+
168189
}

0 commit comments

Comments
 (0)