OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4SortObjectMultiple Class Reference

#include <C4FindObject.h>

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

Public Member Functions

 C4SortObjectMultiple (int32_t iCnt, C4SortObject **ppSorts, bool fFreeArray=true)
 
 ~C4SortObjectMultiple () override
 
void SortObjects (C4ValueArray *pArray)
 

Static Public Member Functions

static C4SortObjectCreateByValue (const C4Value &Data, const C4Object *context=nullptr)
 
static C4SortObjectCreateByValue (int32_t iType, const C4ValueArray &Data, const C4Object *context=nullptr)
 

Protected Member Functions

int32_t Compare (C4Object *pObj1, C4Object *pObj2) override
 
bool PrepareCache (const C4ValueArray *pObjs) override
 
int32_t CompareCache (int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override
 

Detailed Description

Definition at line 457 of file C4FindObject.h.

Constructor & Destructor Documentation

C4SortObjectMultiple::C4SortObjectMultiple ( int32_t  iCnt,
C4SortObject **  ppSorts,
bool  fFreeArray = true 
)
inline

Definition at line 460 of file C4FindObject.h.

461  : C4SortObject(), fFreeArray(fFreeArray), iCnt(iCnt), ppSorts(ppSorts) {}
C4SortObject()=default
int iCnt
Definition: TstC4NetIO.cpp:32
C4SortObjectMultiple::~C4SortObjectMultiple ( )
override

Definition at line 979 of file C4FindObject.cpp.

980 {
981  for (int32_t i=0; i<iCnt; ++i) delete ppSorts[i];
982  if (fFreeArray) delete [] ppSorts;
983 }
int iCnt
Definition: TstC4NetIO.cpp:32

Member Function Documentation

int32_t C4SortObjectMultiple::Compare ( C4Object pObj1,
C4Object pObj2 
)
overrideprotectedvirtual

Implements C4SortObject.

Definition at line 985 of file C4FindObject.cpp.

986 {
987  // return first comparison that's nonzero
988  int32_t iCmp;
989  for (int32_t i=0; i<iCnt; ++i)
990  if ((iCmp = ppSorts[i]->Compare(pObj1, pObj2)))
991  return iCmp;
992  // all comparisons equal
993  return 0;
994 }
int32_t Compare(C4Object *pObj1, C4Object *pObj2) override
int iCnt
Definition: TstC4NetIO.cpp:32
int32_t C4SortObjectMultiple::CompareCache ( int32_t  iObj1,
int32_t  iObj2,
C4Object pObj1,
C4Object pObj2 
)
overrideprotectedvirtual

Reimplemented from C4SortObject.

Definition at line 1005 of file C4FindObject.cpp.

1006 {
1007  // return first comparison that's nonzero
1008  int32_t iCmp;
1009  for (int32_t i=0; i<iCnt; ++i)
1010  if ((iCmp = ppSorts[i]->CompareCache(iObj1, iObj2, pObj1, pObj2)))
1011  return iCmp;
1012  // all comparisons equal
1013  return 0;
1014 }
int32_t CompareCache(int32_t iObj1, int32_t iObj2, C4Object *pObj1, C4Object *pObj2) override
int iCnt
Definition: TstC4NetIO.cpp:32
C4SortObject * C4SortObject::CreateByValue ( const C4Value Data,
const C4Object context = nullptr 
)
staticinherited

Definition at line 824 of file C4FindObject.cpp.

References C4Value::getArray().

Referenced by C4FindObject::CreateByValue(), and C4SortObject::CreateByValue().

825 {
826  // Must be an array
827  const C4ValueArray *pArray = C4Value(DataVal).getArray();
828  if (!pArray) return nullptr;
829  const C4ValueArray &Data = *pArray;
830  int32_t iType = Data[0].getInt();
831  return CreateByValue(iType, Data, context);
832 }
C4ValueArray * getArray() const
Definition: C4Value.h:118
static C4SortObject * CreateByValue(const C4Value &Data, const C4Object *context=nullptr)

Here is the call graph for this function:

Here is the caller graph for this function:

C4SortObject * C4SortObject::CreateByValue ( int32_t  iType,
const C4ValueArray Data,
const C4Object context = nullptr 
)
staticinherited

Definition at line 834 of file C4FindObject.cpp.

References C4SO_Distance, C4SO_Func, C4SO_Mass, C4SO_Multiple, C4SO_Random, C4SO_Reverse, C4SO_Speed, C4SO_Value, C4SortObject::CreateByValue(), C4ValueArray::GetSize(), C4Object::GetX(), C4Object::GetY(), iSize, and C4SortObjectFunc::SetPar().

835 {
836  switch (iType)
837  {
838  case C4SO_Reverse:
839  {
840  // create child sort
841  C4SortObject *pChildSort = C4SortObject::CreateByValue(Data[1], context);
842  if (!pChildSort) return nullptr;
843  // wrap
844  return new C4SortObjectReverse(pChildSort);
845  }
846 
847  case C4SO_Multiple:
848  {
849  // Trivial case (one sort)
850  if (Data.GetSize() == 2)
851  {
852  return C4SortObject::CreateByValue(Data[1], context);
853  }
854  // Create all children
855  int32_t i;
856  C4SortObject **ppSorts = new C4SortObject *[Data.GetSize() - 1];
857  for (i = 0; i < Data.GetSize() - 1; i++)
858  {
859  ppSorts[i] = C4SortObject::CreateByValue(Data[i+1], context);
860  }
861  // Count real entries, move them to start of list
862  int32_t iSize = 0;
863  for (i = 0; i < Data.GetSize() - 1; i++)
864  if (ppSorts[i])
865  if (iSize++ != i)
866  ppSorts[iSize-1] = ppSorts[i];
867  // Create
868  return new C4SortObjectMultiple(iSize, ppSorts);
869  }
870 
871  case C4SO_Distance:
872  {
873  int32_t x = Data[1].getInt();
874  int32_t y = Data[2].getInt();
875  if (context)
876  {
877  x += context->GetX();
878  y += context->GetY();
879  }
880  return new C4SortObjectDistance(x, y);
881  }
882 
883  case C4SO_Random:
884  return new C4SortObjectRandom();
885 
886  case C4SO_Speed:
887  return new C4SortObjectSpeed();
888 
889  case C4SO_Mass:
890  return new C4SortObjectMass();
891 
892  case C4SO_Value:
893  return new C4SortObjectValue();
894 
895  case C4SO_Func:
896  {
897  // Get function name
898  C4String *pStr = Data[1].getStr();
899  if (!pStr) return nullptr;
900  // Construct
901  C4SortObjectFunc *pSO = new C4SortObjectFunc(pStr);
902  // Add parameters
903  for (int i = 2; i < Data.GetSize(); i++)
904  pSO->SetPar(i - 2, Data[i]);
905  // Done
906  return pSO;
907  }
908 
909  }
910  return nullptr;
911 }
int32_t GetY() const
Definition: C4Object.h:287
int32_t GetX() const
Definition: C4Object.h:286
int32_t GetSize() const
Definition: C4ValueArray.h:36
static C4SortObject * CreateByValue(const C4Value &Data, const C4Object *context=nullptr)
void SetPar(int i, const C4Value &val)
int iSize
Definition: TstC4NetIO.cpp:32

Here is the call graph for this function:

bool C4SortObjectMultiple::PrepareCache ( const C4ValueArray pObjs)
overrideprotectedvirtual

Reimplemented from C4SortObject.

Definition at line 996 of file C4FindObject.cpp.

997 {
998  bool fCaches = false;
999  for (int32_t i=0; i<iCnt; ++i)
1000  fCaches |= ppSorts[i]->PrepareCache(pObjs);
1001  // return wether a sort citerion uses a cache
1002  return fCaches;
1003 }
int iCnt
Definition: TstC4NetIO.cpp:32
bool PrepareCache(const C4ValueArray *pObjs) override
void C4SortObject::SortObjects ( C4ValueArray pArray)
inherited

Definition at line 913 of file C4FindObject.cpp.

References C4ValueArray::Sort().

Referenced by C4FindObject::FindMany().

914 {
915  pArray->Sort(*this);
916 }
void Sort(class C4SortObject &rSort)

Here is the call graph for this function:

Here is the caller graph for this function:


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