OpenClonk
C4ValueArray Class Reference

#include <C4ValueArray.h>

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

Public Types

enum  { MaxSize = 1000000 }
 

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 ()
 

Protected Attributes

unsigned int RefCnt {0}
 

Detailed Description

Definition at line 23 of file C4ValueArray.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
MaxSize 

Definition at line 26 of file C4ValueArray.h.

26 { MaxSize = 1000000 }; // ye shalt not create arrays larger than that!

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.

References SetSize().

25 {
26  SetSize(inSize);
27 }
void SetSize(int32_t inSize)
Here is the call graph for this function:

◆ C4ValueArray() [3/3]

C4ValueArray::C4ValueArray ( const C4ValueArray ValueArray2)

Definition at line 29 of file C4ValueArray.cpp.

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

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
void SetSize(int32_t inSize)
int32_t GetSize() const
Definition: C4ValueArray.h:36
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.

Referenced by C4FindObjectInArray::Check().

49  {
50  return pData[iElem];
51  }
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.

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

Referenced by IsFrozen().

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 }
StdParameterAdaptMaker< P > mkParAdaptMaker(P &&rPar)
Definition: StdAdaptors.h:489
void SetSize(int32_t inSize)
StdArrayAdapt< T, M > mkArrayAdaptMap(T *pArray, int iSize, M &&map)
Definition: StdAdaptors.h:331
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DecRef()

void C4RefCnt::DecRef ( )
inlineinherited

◆ Denumerate()

void C4ValueArray::Denumerate ( C4ValueNumbers numbers)

Definition at line 261 of file C4ValueArray.cpp.

References iSize.

Referenced by C4Value::Denumerate(), and IsFrozen().

262 {
263  for (int32_t i = 0; i < iSize; i++)
264  pData[i].Denumerate(numbers);
265 }
void Denumerate(C4ValueNumbers *)
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

◆ GetSize()

◆ GetSlice()

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

Definition at line 285 of file C4ValueArray.cpp.

References C4ValueArray(), and iSize.

Referenced by C4AulExec::ClearPointers(), IsFrozen(), and C4AulCompiler::ConstexprEvaluator::visit().

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 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IncRef()

void C4RefCnt::IncRef ( )
inlineinherited

◆ IsFrozen()

bool C4ValueArray::IsFrozen ( ) const
inline

Definition at line 63 of file C4ValueArray.h.

References CompileFunc(), Denumerate(), GetSlice(), operator==(), SetSlice(), Sort(), SortByArrayElement(), SortByProperty(), and SortStrings().

Referenced by C4AulExec::ClearPointers().

63 { return constant; }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

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

Definition at line 42 of file C4ValueArray.cpp.

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

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 }
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
void SetSize(int32_t inSize)
int32_t GetSize() const
Definition: C4ValueArray.h:36
Here is the call graph for this function:

◆ operator==()

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

Definition at line 246 of file C4ValueArray.cpp.

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

Referenced by IsFrozen().

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 }
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
int32_t GetSize() const
Definition: C4ValueArray.h:36
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator[]() [1/2]

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

Definition at line 53 of file C4ValueArray.h.

References GetItem(), Reset(), SetItem(), and SetSize().

53 { return GetItem(iElem); }
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
Here is the call graph for this function:

◆ operator[]() [2/2]

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

Definition at line 186 of file C4ValueArray.cpp.

References iSize, MaxSize, and SetSize().

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 }
void SetSize(int32_t inSize)
Here is the call graph for this function:

◆ Reset()

void C4ValueArray::Reset ( )

Definition at line 255 of file C4ValueArray.cpp.

References iSize.

Referenced by CompileFunc(), and operator[]().

256 {
257  delete[] pData; pData = nullptr;
258  iSize = iCapacity = 0;
259 }
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.

References iSize, MaxSize, and SetSize().

Referenced by C4Object::AddObjectAndContentsToArray(), C4MapScriptAlgoModifier::C4MapScriptAlgoModifier(), operator[](), C4ScriptGuiWindowAction::ToC4Value(), and C4ScriptGuiWindow::~C4ScriptGuiWindow().

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 }
void SetSize(int32_t inSize)
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.

References iSize, and MaxSize.

Referenced by C4ValueArray(), C4Game::CastObjects(), CompileFunc(), C4FindObject::FindMany(), C4PropList::GetProperties(), C4Effect::GetProperties(), C4Object::GetProperties(), operator=(), operator[](), C4EditCursor::PerformDuplication(), SetItem(), SetSlice(), C4ScriptGuiWindowAction::ToC4Value(), and C4ScriptGuiWindow::~C4ScriptGuiWindow().

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 }
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.

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

Referenced by C4AulExec::ClearPointers(), and IsFrozen().

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_Type GetType() const
Definition: C4Value.h:161
void SetSize(int32_t inSize)
int32_t GetSize() const
Definition: C4ValueArray.h:36
C4ValueArray * _getArray() const
Definition: C4Value.h:127
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Sort() [1/2]

void C4ValueArray::Sort ( class C4SortObject rSort)

Definition at line 71 of file C4ValueArray.cpp.

References iSize, and C4SortObject::PrepareCache().

Referenced by IsFrozen(), and C4SortObject::SortObjects().

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 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Sort() [2/2]

void C4ValueArray::Sort ( bool  descending = false)

Definition at line 122 of file C4ValueArray.cpp.

References iSize.

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 }

◆ SortByArrayElement()

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

Definition at line 167 of file C4ValueArray.cpp.

References GetSize(), and iSize.

Referenced by IsFrozen().

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 }
int32_t GetSize() const
Definition: C4ValueArray.h:36
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SortByProperty()

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

Definition at line 143 of file C4ValueArray.cpp.

References iSize.

Referenced by IsFrozen().

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 }
Here is the caller graph for this function:

◆ SortStrings()

void C4ValueArray::SortStrings ( )

Definition at line 105 of file C4ValueArray.cpp.

References iSize.

Referenced by IsFrozen().

106 {
107  assert(!constant);
108  std::stable_sort(pData, pData+iSize, C4ValueArraySortStringscomp());
109 }
Here is the caller graph for this function:

◆ Thaw()

void C4ValueArray::Thaw ( )
inline

Definition at line 62 of file C4ValueArray.h.

62 { constant = false; }

Member Data Documentation

◆ RefCnt

unsigned int C4RefCnt::RefCnt {0}
protectedinherited

Definition at line 31 of file C4StringTable.h.

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


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