OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 ()
 
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
 

Detailed Description

Definition at line 23 of file C4ValueArray.h.

Member Enumeration Documentation

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

Definition at line 23 of file C4ValueArray.cpp.

Referenced by GetSlice().

24  : pData(nullptr), iSize(0), iCapacity(0), constant(false)
25 {
26 }

Here is the caller graph for this function:

C4ValueArray::C4ValueArray ( int32_t  inSize)

Definition at line 28 of file C4ValueArray.cpp.

References SetSize().

29  : pData(nullptr), iSize(0), iCapacity(0), constant(false)
30 {
31  SetSize(inSize);
32 }
void SetSize(int32_t inSize)

Here is the call graph for this function:

C4ValueArray::C4ValueArray ( const C4ValueArray ValueArray2)

Definition at line 34 of file C4ValueArray.cpp.

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

35  : pData(nullptr), iSize(0), iCapacity(0), constant(false)
36 {
37  SetSize(ValueArray2.GetSize());
38  for (int32_t i = 0; i < iSize; i++)
39  pData[i].Set(ValueArray2.GetItem(i));
40 }
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 ( )

Definition at line 42 of file C4ValueArray.cpp.

43 {
44  delete[] pData; pData = nullptr;
45  iSize = iCapacity = 0;
46 }

Member Function Documentation

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:

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

Definition at line 273 of file C4ValueArray.cpp.

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

274 {
275  int32_t inSize = iSize;
276  // Size. Reset if not found.
277  try
278  { pComp->Value(inSize); }
279  catch (StdCompiler::NotFoundException *pExc)
280  { Reset(); delete pExc; return; }
281  // Separator
282  pComp->Separator(StdCompiler::SEP_SEP2);
283  // Allocate
284  if (pComp->isDeserializer()) this->SetSize(inSize);
285  // Values
286  pComp->Value(mkArrayAdaptMap(pData, iSize, C4Value(), mkParAdaptMaker(numbers)));
287 }
StdParameterAdaptMaker< P > mkParAdaptMaker(P &&rPar)
Definition: StdAdaptors.h:469
void SetSize(int32_t inSize)
StdArrayAdapt< T, M > mkArrayAdaptMap(T *pArray, int iSize, M &&map)
Definition: StdAdaptors.h:311

Here is the call graph for this function:

void C4RefCnt::DecRef ( )
inlineinherited

Definition at line 28 of file C4StringTable.h.

References C4RefCnt::RefCnt.

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

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

Here is the caller graph for this function:

void C4ValueArray::Denumerate ( C4ValueNumbers numbers)

Definition at line 267 of file C4ValueArray.cpp.

Referenced by C4Value::Denumerate().

268 {
269  for (int32_t i = 0; i < iSize; i++)
270  pData[i].Denumerate(numbers);
271 }
void Denumerate(C4ValueNumbers *)

Here is the caller graph for this function:

void C4ValueArray::Freeze ( )
inline

Definition at line 61 of file C4ValueArray.h.

61 { constant = true; }
const C4Value& C4ValueArray::GetItem ( int32_t  iElem) const
inline

Definition at line 38 of file C4ValueArray.h.

References C4VNull.

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

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:32

Here is the caller graph for this function:

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

Definition at line 291 of file C4ValueArray.cpp.

References C4ValueArray().

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4RefCnt::IncRef ( )
inlineinherited

Definition at line 27 of file C4StringTable.h.

References C4RefCnt::RefCnt.

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

27 { RefCnt++; }
unsigned int RefCnt
Definition: C4StringTable.h:31

Here is the caller graph for this function:

bool C4ValueArray::IsFrozen ( ) const
inline

Definition at line 63 of file C4ValueArray.h.

63 { return constant; }
C4ValueArray & C4ValueArray::operator= ( const C4ValueArray ValueArray2)

Definition at line 48 of file C4ValueArray.cpp.

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

49 {
50  this->SetSize(ValueArray2.GetSize());
51  for (int32_t i = 0; i < iSize; i++)
52  pData[i].Set(ValueArray2.GetItem(i));
53  return *this;
54 }
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:

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

Definition at line 252 of file C4ValueArray.cpp.

References GetItem(), and GetSize().

253 {
254  for (int32_t i=0; i<std::max(iSize, IntList2.GetSize()); i++)
255  if (GetItem(i) != IntList2.GetItem(i))
256  return false;
257 
258  return true;
259 }
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:

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

Definition at line 53 of file C4ValueArray.h.

References GetItem().

53 { return GetItem(iElem); }
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38

Here is the call graph for this function:

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

Definition at line 192 of file C4ValueArray.cpp.

References MaxSize, and SetSize().

193 {
194  assert(iElem < MaxSize);
195  assert(iElem >= 0);
196  assert(!constant);
197  if (iElem >= iSize && iElem < MaxSize) this->SetSize(iElem + 1);
198  // out-of-memory? This might not get caught, but it's better than a segfault
199  assert(iElem < iSize);
200  // return
201  return pData[iElem];
202 }
void SetSize(int32_t inSize)

Here is the call graph for this function:

void C4ValueArray::Reset ( )

Definition at line 261 of file C4ValueArray.cpp.

Referenced by CompileFunc().

262 {
263  delete[] pData; pData = nullptr;
264  iSize = iCapacity = 0;
265 }

Here is the caller graph for this function:

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

Definition at line 204 of file C4ValueArray.cpp.

References MaxSize, and SetSize().

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4ValueArray::SetSize ( int32_t  inSize)

Definition at line 220 of file C4ValueArray.cpp.

References MaxSize.

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

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

Here is the caller graph for this function:

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

Definition at line 309 of file C4ValueArray.cpp.

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

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

Here is the call graph for this function:

void C4ValueArray::Sort ( class C4SortObject rSort)

Definition at line 77 of file C4ValueArray.cpp.

References C4SortObject::PrepareCache().

Referenced by C4SortObject::SortObjects().

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4ValueArray::Sort ( bool  descending = false)

Definition at line 128 of file C4ValueArray.cpp.

129 {
130  assert(!constant);
131  // sort by whatever type the values have
132  std::stable_sort(pData, pData+iSize, C4ValueArraySortcomp());
133  if (descending) std::reverse(pData, pData+iSize);
134 }
bool C4ValueArray::SortByArrayElement ( int32_t  array_idx,
bool  descending = false 
)

Definition at line 173 of file C4ValueArray.cpp.

References GetSize().

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

Here is the call graph for this function:

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

Definition at line 149 of file C4ValueArray.cpp.

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

Definition at line 111 of file C4ValueArray.cpp.

112 {
113  assert(!constant);
114  std::stable_sort(pData, pData+iSize, C4ValueArraySortStringscomp());
115 }
void C4ValueArray::Thaw ( )
inline

Definition at line 62 of file C4ValueArray.h.

62 { constant = false; }

Member Data Documentation

unsigned int C4RefCnt::RefCnt
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: