OpenClonk
C4ValueArray Class Reference

#include <C4ValueArray.h>

Inheritance diagram for C4ValueArray:
[legend]
Collaboration diagram for C4ValueArray:
[legend]

Public Member Functions

 C4ValueArray ()
 
 C4ValueArray (int32_t inSize)
 
 C4ValueArray (const C4ValueArray &)
 
 ~C4ValueArray () override
 
C4ValueArrayoperator= (const C4ValueArray &)
 
int32_t GetSize () const
 
const C4ValueGetItem (int32_t iElem) const
 
const C4Value_GetItem (int32_t iElem) const
 
C4Value operator[] (int32_t iElem) const
 
C4Valueoperator[] (int32_t iElem)
 
void Reset ()
 
void SetItem (int32_t iElemNr, const C4Value &Value)
 
void SetSize (int32_t inSize)
 
void Freeze ()
 
void Thaw ()
 
bool IsFrozen () const
 
void Denumerate (C4ValueNumbers *)
 
bool operator== (const C4ValueArray &) const
 
void CompileFunc (class StdCompiler *pComp, C4ValueNumbers *)
 
C4ValueArrayGetSlice (int32_t startIndex, int32_t endIndex)
 
void SetSlice (int32_t startIndex, int32_t endIndex, const C4Value &Val)
 
void Sort (class C4SortObject &rSort)
 
void SortStrings ()
 
void Sort (bool descending=false)
 
bool SortByProperty (C4String *prop_name, bool descending=false)
 
bool SortByArrayElement (int32_t array_idx, bool descending=false)
 
void IncRef ()
 
void DecRef ()
 

Static Public Attributes

static const int MaxSize = 1000000
 

Protected Attributes

unsigned int RefCnt {0}
 

Detailed Description

Definition at line 23 of file C4ValueArray.h.

Constructor & Destructor Documentation

◆ C4ValueArray() [1/3]

C4ValueArray::C4ValueArray ( )
default

Referenced by GetSlice().

Here is the caller graph for this function:

◆ C4ValueArray() [2/3]

C4ValueArray::C4ValueArray ( int32_t  inSize)

Definition at line 24 of file C4ValueArray.cpp.

25 {
26  SetSize(inSize);
27 }
void SetSize(int32_t inSize)

References SetSize().

Here is the call graph for this function:

◆ C4ValueArray() [3/3]

C4ValueArray::C4ValueArray ( const C4ValueArray ValueArray2)

Definition at line 29 of file C4ValueArray.cpp.

30 {
31  SetSize(ValueArray2.GetSize());
32  for (int32_t i = 0; i < iSize; i++)
33  pData[i].Set(ValueArray2.GetItem(i));
34 }
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
int32_t GetSize() const
Definition: C4ValueArray.h:36

References GetItem(), GetSize(), and SetSize().

Here is the call graph for this function:

◆ ~C4ValueArray()

C4ValueArray::~C4ValueArray ( )
override

Definition at line 36 of file C4ValueArray.cpp.

37 {
38  delete[] pData; pData = nullptr;
39  iSize = iCapacity = 0;
40 }

Member Function Documentation

◆ _GetItem()

const C4Value& C4ValueArray::_GetItem ( int32_t  iElem) const
inline

Definition at line 48 of file C4ValueArray.h.

49  {
50  return pData[iElem];
51  }

Referenced by C4FindObjectInArray::Check().

Here is the caller graph for this function:

◆ CompileFunc()

void C4ValueArray::CompileFunc ( class StdCompiler pComp,
C4ValueNumbers numbers 
)

Definition at line 267 of file C4ValueArray.cpp.

268 {
269  int32_t inSize = iSize;
270  // Size. Reset if not found.
271  try
272  { pComp->Value(inSize); }
273  catch (StdCompiler::NotFoundException *pExc)
274  { Reset(); delete pExc; return; }
275  // Separator
276  pComp->Separator(StdCompiler::SEP_SEP2);
277  // Allocate
278  if (pComp->isDeserializer()) this->SetSize(inSize);
279  // Values
280  pComp->Value(mkArrayAdaptMap(pData, iSize, C4Value(), mkParAdaptMaker(numbers)));
281 }
StdArrayAdapt< T, M > mkArrayAdaptMap(T *pArray, int iSize, M &&map)
Definition: StdAdaptors.h:340
StdParameterAdaptMaker< P > mkParAdaptMaker(P &&rPar)
Definition: StdAdaptors.h:503

References StdCompiler::isDeserializer(), mkArrayAdaptMap(), mkParAdaptMaker(), Reset(), StdCompiler::SEP_SEP2, StdCompiler::Separator(), SetSize(), and StdCompiler::Value().

Here is the call graph for this function:

◆ DecRef()

void C4RefCnt::DecRef ( )
inlineinherited

Definition at line 28 of file C4StringTable.h.

28 { if (!--RefCnt) delete this; }
unsigned int RefCnt
Definition: C4StringTable.h:31

References C4RefCnt::RefCnt.

Referenced by C4Command::Clear(), C4Command::CompileFunc(), C4Property::CompileFunc(), C4Def::GetActionByName(), C4Property::operator=(), C4Object::SetActionByName(), C4AulScriptFunc::SetOverloaded(), C4ScriptHost::UnlinkOwnedFunctions(), C4AulScriptFunc::~C4AulScriptFunc(), C4Property::~C4Property(), C4ScriptGuiWindow::~C4ScriptGuiWindow(), and C4ScriptGuiWindowAction::~C4ScriptGuiWindowAction().

Here is the caller graph for this function:

◆ Denumerate()

void C4ValueArray::Denumerate ( C4ValueNumbers numbers)

Definition at line 261 of file C4ValueArray.cpp.

262 {
263  for (int32_t i = 0; i < iSize; i++)
264  pData[i].Denumerate(numbers);
265 }
void Denumerate(C4ValueNumbers *)

Referenced by C4Value::Denumerate().

Here is the caller graph for this function:

◆ Freeze()

void C4ValueArray::Freeze ( )
inline

Definition at line 61 of file C4ValueArray.h.

61 { constant = true; }

◆ GetItem()

const C4Value& C4ValueArray::GetItem ( int32_t  iElem) const
inline

Definition at line 38 of file C4ValueArray.h.

39  {
40  if (-iSize <= iElem && iElem < 0)
41  return pData[iSize + iElem];
42  else if (0 <= iElem && iElem < iSize)
43  return pData[iElem];
44  else
45  return C4VNull;
46  }
const C4Value C4VNull
Definition: C4Value.cpp:30

References C4VNull.

Referenced by C4MapScriptAlgoModifier::C4MapScriptAlgoModifier(), C4MapScriptAlgoPolygon::C4MapScriptAlgoPolygon(), C4ValueArray(), C4EditCursor::DoContextMenu(), C4Object::DrawLine(), FnParRect(), C4Object::GetParallaxity(), C4MapScriptAlgo::GetXYProps(), C4ScriptGuiWindowAction::Init(), C4MapScriptMatTexMask::Init(), C4Object::IsVisible(), C4ValueArraySortArrayElementcomp::operator()(), operator=(), operator==(), operator[](), C4Landscape::P::PostFreeShape(), C4SortObjectByValue::PrepareCache(), C4Value::ToJSON(), C4FoWLight::Update(), and C4AulCompiler::ConstexprEvaluator::visit().

Here is the caller graph for this function:

◆ GetSize()

◆ GetSlice()

C4ValueArray * C4ValueArray::GetSlice ( int32_t  startIndex,
int32_t  endIndex 
)

Definition at line 285 of file C4ValueArray.cpp.

286 {
287  // adjust indices so that the default end index works and that negative numbers count backwards from the end of the string
288  if (startIndex > iSize) startIndex = iSize;
289  else if (startIndex < -iSize) throw C4AulExecError("array slice: start index out of range");
290  else if (startIndex < 0) startIndex += iSize;
291 
292  if (endIndex > iSize) endIndex = iSize; // this also processes the MAX_INT default if no parameter is given in script
293  else if (endIndex < -iSize) throw C4AulExecError("array slice: end index out of range");
294  else if (endIndex < 0) endIndex += iSize;
295 
296  C4ValueArray* NewArray = new C4ValueArray(std::max(0, endIndex - startIndex));
297  for (int i = startIndex; i < endIndex; ++i)
298  NewArray->pData[i - startIndex] = pData[i];
299  return NewArray;
300 }

References C4ValueArray().

Referenced by C4AulCompiler::ConstexprEvaluator::visit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IncRef()

void C4RefCnt::IncRef ( )
inlineinherited

Definition at line 27 of file C4StringTable.h.

27 { RefCnt++; }

References C4RefCnt::RefCnt.

Referenced by C4Property::C4Property(), C4Command::CompileFunc(), C4Property::CompileFunc(), C4ScriptGuiWindow::CreateFromPropList(), C4Def::GetActionByName(), C4ScriptGuiWindowAction::Init(), C4Property::operator=(), C4ScriptGuiWindowProperty::Set(), C4Command::Set(), C4Object::SetActionByName(), and C4AulScriptFunc::SetOverloaded().

Here is the caller graph for this function:

◆ IsFrozen()

bool C4ValueArray::IsFrozen ( ) const
inline

Definition at line 63 of file C4ValueArray.h.

63 { return constant; }

◆ operator=()

C4ValueArray & C4ValueArray::operator= ( const C4ValueArray ValueArray2)

Definition at line 42 of file C4ValueArray.cpp.

43 {
44  this->SetSize(ValueArray2.GetSize());
45  for (int32_t i = 0; i < iSize; i++)
46  pData[i].Set(ValueArray2.GetItem(i));
47  return *this;
48 }

References GetItem(), GetSize(), and SetSize().

Here is the call graph for this function:

◆ operator==()

bool C4ValueArray::operator== ( const C4ValueArray IntList2) const

Definition at line 246 of file C4ValueArray.cpp.

247 {
248  for (int32_t i=0; i<std::max(iSize, IntList2.GetSize()); i++)
249  if (GetItem(i) != IntList2.GetItem(i))
250  return false;
251 
252  return true;
253 }

References GetItem(), and GetSize().

Here is the call graph for this function:

◆ operator[]() [1/2]

C4Value & C4ValueArray::operator[] ( int32_t  iElem)

Definition at line 186 of file C4ValueArray.cpp.

187 {
188  assert(iElem < MaxSize);
189  assert(iElem >= 0);
190  assert(!constant);
191  if (iElem >= iSize && iElem < MaxSize) this->SetSize(iElem + 1);
192  // out-of-memory? This might not get caught, but it's better than a segfault
193  assert(iElem < iSize);
194  // return
195  return pData[iElem];
196 }
static const int MaxSize
Definition: C4ValueArray.h:26

References MaxSize, and SetSize().

Here is the call graph for this function:

◆ operator[]() [2/2]

C4Value C4ValueArray::operator[] ( int32_t  iElem) const
inline

Definition at line 53 of file C4ValueArray.h.

53 { return GetItem(iElem); }

References GetItem().

Here is the call graph for this function:

◆ Reset()

void C4ValueArray::Reset ( )

Definition at line 255 of file C4ValueArray.cpp.

256 {
257  delete[] pData; pData = nullptr;
258  iSize = iCapacity = 0;
259 }

Referenced by CompileFunc().

Here is the caller graph for this function:

◆ SetItem()

void C4ValueArray::SetItem ( int32_t  iElemNr,
const C4Value Value 
)

Definition at line 198 of file C4ValueArray.cpp.

199 {
200  assert(!constant);
201  // enlarge
202  if (iElem < -iSize)
203  throw C4AulExecError("array access: index out of range");
204  else if (iElem < 0)
205  iElem = iSize + iElem;
206  else if (iElem >= iSize && iElem < MaxSize) this->SetSize(iElem + 1);
207  // out-of-memory? This might not get caught, but it's better than a segfault
208  if (iElem >= iSize)
209  throw C4AulExecError("array access: index too large");
210  // set
211  pData[iElem]=Value;
212 }

References MaxSize, and SetSize().

Referenced by C4Object::AddObjectAndContentsToArray(), C4MapScriptAlgoModifier::C4MapScriptAlgoModifier(), and C4ScriptGuiWindowAction::ToC4Value().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetSize()

void C4ValueArray::SetSize ( int32_t  inSize)

Definition at line 214 of file C4ValueArray.cpp.

215 {
216  if(inSize == iSize) return;
217  assert(!constant);
218 
219  // array not larger than allocated memory? Well, just ignore the additional allocated mem then
220  if (inSize <= iCapacity)
221  {
222  // free values in undefined area, do nothing if new is larger than old
223  for (int i=inSize; i<iSize; i++) pData[i].Set0();
224  iSize=inSize;
225  return;
226  }
227 
228  // bounds check
229  if (inSize > MaxSize) return;
230 
231  // create new array
232  C4Value* pnData = new C4Value [inSize];
233  if (!pnData) return;
234 
235  // move existing values
236  int32_t i;
237  for (i=0; i<iSize; i++)
238  pnData[i] = pData[i];
239 
240  // replace
241  delete[] pData;
242  pData = pnData;
243  iSize = iCapacity = inSize;
244 }
int iSize
Definition: TstC4NetIO.cpp:32

References iSize, and MaxSize.

Referenced by C4ValueArray(), C4Game::CastObjects(), CompileFunc(), C4FindObject::FindMany(), operator=(), operator[](), SetItem(), SetSlice(), and C4ScriptGuiWindowAction::ToC4Value().

Here is the caller graph for this function:

◆ SetSlice()

void C4ValueArray::SetSlice ( int32_t  startIndex,
int32_t  endIndex,
const C4Value Val 
)

Definition at line 303 of file C4ValueArray.cpp.

304 {
305  // maximum bounds
306  if(startIndex >= MaxSize) throw C4AulExecError("array slice: start index exceeds maximum capacity");
307 
308  // index from back
309  if(startIndex < 0) startIndex += iSize;
310  if(endIndex < 0) endIndex += iSize;
311 
312  // ensure relevant bounds
313  if(startIndex < 0) throw C4AulExecError("array slice: start index out of range");
314  if(endIndex < 0) throw C4AulExecError("array slice: end index out of range");
315  if(endIndex < startIndex)
316  endIndex = startIndex;
317 
318  // setting an array?
319  if(Val.GetType() == C4V_Array)
320  {
321  const C4ValueArray &Other = *Val._getArray(); // Remember that &Other could be equal to this, carefull with modifying pData
322 
323  // Calculcate new size
324  int32_t iNewEnd = std::min(startIndex + Other.GetSize(), (int32_t)MaxSize);
325  int32_t iNewSize = iNewEnd;
326  if(endIndex < iSize)
327  iNewSize += iSize - endIndex;
328  iNewSize = std::min(iNewSize, (int32_t)MaxSize);
329  int32_t iOtherSize = Other.GetSize();
330 
331  if(iNewSize != iSize)
332  {
333  int32_t i,j;
334  C4Value* pnData = pData;
335 
336  if(iNewSize > iCapacity)
337  {
338  pnData = new C4Value [iNewSize];
339 
340  // Copy first part of old array
341  for(i = 0; i < startIndex && i < iSize; ++i)
342  pnData[i] = pData[i];
343  }
344 
345  // Copy the second slice of the new array
346  for(i = iNewEnd, j = endIndex; i < iNewSize; ++i, ++j)
347  {
348  assert(j < iSize);
349  pnData[i] = pData[j];
350  }
351 
352  // Copy the data
353  // Since pnData and pData can be the same, we can not copy with
354  //for(i = startIndex, j = 0; i < iNewEnd; ++i, ++j)
355  // but have to start from the end of the copied sequence. That works, since j <= i
356  for(i = iNewEnd - 1, j = iNewEnd - startIndex - 1; i >= startIndex; --i, --j)
357  {
358  assert(j < iOtherSize);
359  pnData[i] = Other.pData[j];
360  }
361 
362  // Other values should have been initialized to 0 by new
363  if(pData != pnData)
364  {
365  delete [] pData;
366  pData = pnData;
367  iCapacity = iSize = iNewSize;
368  }
369  else
370  {
371  // "ignore" the now unused part
372  for(i = iNewSize; i < iSize; ++i)
373  pData[i].Set0();
374  iSize = iNewSize;
375  }
376 
377  } else // slice has the same size as inserted array
378  // Copy the data. changing pData does not break because if &Other == this and iNewSize == iSize, nothing happens at all
379  for(int32_t i = startIndex, j = 0; j < iOtherSize; i++, j++)
380  pData[i] = Other.pData[j];
381 
382  } else /* if(Val.GetType() != C4V_Array) */ {
383  if(endIndex > MaxSize) endIndex = iSize;
384 
385  // Need resize?
386  if(endIndex > iSize) SetSize(endIndex);
387 
388  // Fill
389  for(int32_t i = startIndex; i < endIndex; i++)
390  pData[i] = Val;
391 
392  }
393 
394 }
@ C4V_Array
Definition: C4Value.h:30
C4V_Type GetType() const
Definition: C4Value.h:161
C4ValueArray * _getArray() const
Definition: C4Value.h:127

References C4Value::_getArray(), C4V_Array, GetSize(), C4Value::GetType(), iSize, MaxSize, and SetSize().

Here is the call graph for this function:

◆ Sort() [1/2]

void C4ValueArray::Sort ( bool  descending = false)

Definition at line 122 of file C4ValueArray.cpp.

123 {
124  assert(!constant);
125  // sort by whatever type the values have
126  std::stable_sort(pData, pData+iSize, C4ValueArraySortcomp());
127  if (descending) std::reverse(pData, pData+iSize);
128 }

◆ Sort() [2/2]

void C4ValueArray::Sort ( class C4SortObject rSort)

Definition at line 71 of file C4ValueArray.cpp.

72 {
73  assert(!constant);
74  if (rSort.PrepareCache(this))
75  {
76  // Initialize position array
77  intptr_t i, *pPos = new intptr_t[iSize];
78  for (i = 0; i < iSize; i++) pPos[i] = i;
79  // Sort
80  std::stable_sort(pPos, pPos+iSize, C4SortObjectSTLCache(rSort, pData));
81  // Save actual object pointers in array (hacky).
82  for (i = 0; i < iSize; i++)
83  pPos[i] = reinterpret_cast<intptr_t>(pData[pPos[i]]._getObj());
84  // Set the values
85  for (i = 0; i < iSize; i++)
86  pData[i].SetPropList(reinterpret_cast<C4PropList *>(pPos[i]));
87  delete [] pPos;
88  }
89  else
90  // Be sure to use stable sort, as otherweise the algorithm isn't garantueed
91  // to produce identical results on all platforms!
92  std::stable_sort(pData, pData+iSize, C4SortObjectSTL(rSort));
93 }

References iSize, and C4SortObject::PrepareCache().

Referenced by C4SortObject::SortObjects().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SortByArrayElement()

bool C4ValueArray::SortByArrayElement ( int32_t  array_idx,
bool  descending = false 
)

Definition at line 167 of file C4ValueArray.cpp.

168 {
169  assert(element_idx>=0);
170  assert(!constant);
171  // expect this to be an array of arrays and sort by given element
172  // make sure we're all arrays before
173  for (int32_t i=0; i<iSize; ++i)
174  {
175  if (!pData[i].getArray())
176  return false;
177  if (pData[i]._getArray()->GetSize() <= element_idx)
178  return false;
179  }
180  // now sort
181  std::stable_sort(pData, pData+iSize, C4ValueArraySortArrayElementcomp(element_idx));
182  if (descending) std::reverse(pData, pData+iSize);
183  return true;
184 }

References GetSize(), and iSize.

Here is the call graph for this function:

◆ SortByProperty()

bool C4ValueArray::SortByProperty ( C4String prop_name,
bool  descending = false 
)

Definition at line 143 of file C4ValueArray.cpp.

144 {
145  assert(!constant);
146  // expect this to be an array of proplists and sort by given property
147  // make sure we're all proplists before
148  for (int32_t i=0; i<iSize; ++i)
149  if (!pData[i].getPropList())
150  return false;
151  // now sort
152  std::stable_sort(pData, pData+iSize, C4ValueArraySortPropertycomp(prop_name));
153  if (descending) std::reverse(pData, pData+iSize);
154  return true;
155 }

References iSize.

◆ SortStrings()

void C4ValueArray::SortStrings ( )

Definition at line 105 of file C4ValueArray.cpp.

106 {
107  assert(!constant);
108  std::stable_sort(pData, pData+iSize, C4ValueArraySortStringscomp());
109 }

◆ Thaw()

void C4ValueArray::Thaw ( )
inline

Definition at line 62 of file C4ValueArray.h.

62 { constant = false; }

Member Data Documentation

◆ MaxSize

const int C4ValueArray::MaxSize = 1000000
static

Definition at line 26 of file C4ValueArray.h.

Referenced by operator[](), SetItem(), SetSize(), and SetSlice().

◆ RefCnt

unsigned int C4RefCnt::RefCnt {0}
protectedinherited

Definition at line 31 of file C4StringTable.h.

Referenced by C4RefCnt::DecRef(), and C4RefCnt::IncRef().


The documentation for this class was generated from the following files: