listfunc.cc

来自「Gambit 是一个游戏库理论软件」· CC 代码 · 共 952 行 · 第 1/2 页

CC
952
字号
  int i;  if( ((NumberPortion*) param[1])->Value() < gNumber(0) )    throw gclRuntimeError( "Invalid list Length" );  p = new ListPortion();  p->SetDataType( param[0]->Spec().Type );  for( i = 1; i <= (double) ((NumberPortion*) param[1])->Value(); i++ )    p->Append( new NumberPortion( ((NumberPortion*) param[0])->Value()+				      (gNumber)(i-1)*				      ((NumberPortion*) param[2])->Value()));  return p;}static Portion* GSM_List_Nfg(GSM &, Portion **param){  ListPortion* p;  int i;  if( ((NumberPortion*) param[1])->Value() < gNumber(0) )    throw gclRuntimeError( "Invalid list Length" );  p = new ListPortion();  p->SetDataType( param[0]->Spec().Type );  Nfg& nfg =      * (((NfgPortion *) param[0])->Value());    for( i = 1; i <= (double) ((NumberPortion*) param[1])->Value(); i++ )      p->Append(new NfgPortion(new Nfg(nfg)));  return p;}static Portion* GSM_List_Efg(GSM &, Portion **param){  ListPortion* p;  if (((NumberPortion*) param[1])->Value() < gNumber(0)) {    throw gclRuntimeError( "Invalid list length" );  }  p = new ListPortion();  p->SetDataType( param[0]->Spec().Type );  efgGame &efg = *(((EfgPortion*) param[0])->Value());  for (int i = 1; i <= (double) ((NumberPortion*) param[1])->Value(); i++) {    p->Append(new EfgPortion(new efgGame(efg)));  }    return p;}static Portion* GSM_Dot_Check( ListPortion* p1, ListPortion* p2 ){  int i;  if( p1->Length() != p2->Length() )    throw gclRuntimeError( "Mismatched dimentionalities" );  for( i = 1; i <= p1->Length(); i++ )    if( (*p1)[ i ]->Spec().ListDepth > 0 )      throw gclRuntimeError("Can only operate on 1-D lists");  return 0;}static Portion* GSM_Dot(GSM &, Portion **param){  int i;  Portion* p;  ListPortion* p1 = (ListPortion*) param[0];  ListPortion* p2 = (ListPortion*) param[1];  p = GSM_Dot_Check( p1, p2 );  if( p != 0 )    return p;  p = new NumberPortion( (gRational) 0 );  for( i = 1; i <= p1->Length(); i++ )  {    ((NumberPortion*) p)->SetValue(((NumberPortion *) p)->Value() +      (((NumberPortion*) (*p1)[i])->Value() *       ((NumberPortion*) (*p2)[i])->Value()));  }  return p;}static Portion* GSM_ArgMax(GSM &, Portion **param){  Portion* p;  gRational max = 0;  int index = 0;  int i;  if (param[0]->Spec().ListDepth <= 0) {    throw gclRuntimeError("Internal error in ArgMax[]");  }  for( i = ((ListPortion*) param[0])->Length(); i >= 1; i-- )  {    p = (*(ListPortion*) param[0])[i];    if( p->Spec() == porNUMBER )    {      if( ((NumberPortion*) p)->Value() >= gNumber(max) ||	 i == ((ListPortion*) param[0])->Length() )      {	max = ((NumberPortion*) p)->Value();	index = i;      }    }    else      throw gclRuntimeError( "Bad dimensionality" );  }  return new NumberPortion( index );}static Portion* GSM_Transpose(GSM &, Portion **param){  int i;  int j;  int Length;  int width = 0;  ListPortion* p;  ListPortion* s;  if (param[0]->Spec().ListDepth <= 0) {    throw gclRuntimeError("Internal error in Transpose[]");  }  Length = ((ListPortion*) param[0])->Length();  for( i = 1; i <= Length; i++ )  {    if( (*(ListPortion*) param[0])[i]->Spec().ListDepth == 0 )      throw gclRuntimeError( "Bad dimensionality" );    if( i == 1 )      width = ((ListPortion*) (*(ListPortion*) param[0])[i])->Length();    else       if( ((ListPortion*) (*(ListPortion*) param[0])[i])->Length() != width )	throw gclRuntimeError( "Bad dimensionality" );  }  p = new ListPortion();  for( i = 1; i <= width; i++ ) {    s = new ListPortion();    for( j = 1; j <= Length; j++ ) {      s->Append( (*(ListPortion*)(*(ListPortion*) param[0])[j])[i]->ValCopy());    }    p->Append( s );  }  return p;}static Portion* GSM_Inverse(GSM &, Portion **param){  int i;  int Length;  int width = 0;  ListPortion *p;  ListPortion *s;  if (param[0]->Spec().ListDepth <= 0) {    throw gclRuntimeError("Internal error in Inverse[]");  }  Length = ((ListPortion*) param[0])->Length();  for( i = 1; i <= Length; i++ )  {    if( (*(ListPortion*) param[0])[i]->Spec().ListDepth == 0 )      throw gclRuntimeError( "Bad dimensionality" );    if( i == 1 ) {      width = ((ListPortion*) (*(ListPortion*) param[0])[i])->Length();      if(width != Length)        throw gclRuntimeError( "Not a square matrix" );    }    else       if( ((ListPortion*) (*(ListPortion*) param[0])[i])->Length() != width )	      throw gclRuntimeError( "Bad dimensionality" );  }  int j;  gPrecision precis = precRATIONAL;  for(i=1; precis==precRATIONAL && i <= Length; i++ )    for(j=1; precis==precRATIONAL && j<=width; j++)      if((((NumberPortion*) ((*(ListPortion*) (*(ListPortion*) param[0])[i])[j]))->Value()).Precision() != precRATIONAL) 	precis = precDOUBLE;  gNumber gn;  if(precis == precRATIONAL) {    gSquareMatrix<gRational> A(Length);    for(i=1;i<=Length;i++)      for(j=1;j<=width;j++) {	gn = ((NumberPortion*) ((*(ListPortion*) (*(ListPortion*) param[0])[i])[j]))->Value();	A(i,j) = gn.gNumber::operator gRational();      }    gSquareMatrix<gRational> AA(A.Inverse());    p = new ListPortion();    p->SetDataType( param[0]->Spec().Type );    for( i = 1; i <= width; i++ ) {      s = new ListPortion();      s->SetDataType( param[0]->Spec().Type );      for( j = 1; j <= Length; j++ )	  s->Append( new NumberPortion( AA(i,j) ));      p->Append( s );    }  }  else {    gSquareMatrix<double> B(Length);    for(i=1;i<=Length;i++)      for( j=1;j<=width;j++)	B(i,j)= (double)(((NumberPortion*) ((*(ListPortion*) (*(ListPortion*) param[0])[i])[j]))->Value());    gSquareMatrix<double> BB(B.Inverse());    p = new ListPortion();    p->SetDataType( param[0]->Spec().Type );    for( i = 1; i <= width; i++ ) {      s = new ListPortion();      s->SetDataType( param[0]->Spec().Type );      for( j = 1; j <= Length; j++ )	  s->Append( new NumberPortion( BB(i,j) ));      p->Append( s );    }  }  return p;}void Init_listfunc(GSM *gsm){  gclFunction *FuncObj;#ifdef UNUSED  gclParameter x_Int[] =  {    gclParameter( "x", porNUMBER )  };#endif // UNUSED  gclParameter x_Number[] =  {    gclParameter( "x", porNUMBER )  };  FuncObj = new gclFunction(*gsm, "Concat", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Concat_List, 				       PortionSpec(porANYTYPE, 1), 2));  FuncObj->SetParamInfo(0, 0, gclParameter("x", 					    PortionSpec(porANYTYPE, NLIST)));  FuncObj->SetParamInfo(0, 1, gclParameter("y", 					    PortionSpec(porANYTYPE, NLIST)));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "Index", 2);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Index, 				       PortionSpec(porINTEGER, 1), 2, 0,				       funcNONLISTABLE ));  FuncObj->SetParamInfo(0, 0, gclParameter("list", 					    PortionSpec(porANYTYPE, NLIST)));  FuncObj->SetParamInfo(0, 1, gclParameter("x", porANYTYPE));  FuncObj->SetFuncInfo(1, gclSignature(GSM_Index, 				       PortionSpec(porINTEGER, 1), 2, 0,				       funcNONLISTABLE ));  FuncObj->SetParamInfo(1, 0, gclParameter("list", 					    PortionSpec(porANYTYPE, NLIST)));  FuncObj->SetParamInfo(1, 1, gclParameter("x", PortionSpec(porANYTYPE, 1)));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "NumElements", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_NumElements, porINTEGER, 1,				       0, funcNONLISTABLE ));  FuncObj->SetParamInfo(0, 0, gclParameter("list",                               PortionSpec(porANYTYPE, 1, porNULLSPEC )));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "Length", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_LengthList, porINTEGER, 1));  FuncObj->SetParamInfo(0, 0, gclParameter("list",                               PortionSpec(porANYTYPE, 1, porNULLSPEC )));  gsm->AddFunction(FuncObj);			  FuncObj = new gclFunction(*gsm, "NumChars", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_LengthText, porINTEGER, 1));  FuncObj->SetParamInfo(0, 0, gclParameter("text", porTEXT));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "Contains", 2);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Contains, porBOOLEAN, 2,				       0, funcNONLISTABLE));  FuncObj->SetParamInfo(0, 0, gclParameter("list", 					    PortionSpec(porANYTYPE, NLIST)));  FuncObj->SetParamInfo(0, 1, gclParameter("x", porANYTYPE));  FuncObj->SetFuncInfo(1, gclSignature(GSM_Contains, porBOOLEAN, 2,				       0, funcNONLISTABLE));  FuncObj->SetParamInfo(1, 0, gclParameter("list", 					    PortionSpec(porANYTYPE, NLIST)));  FuncObj->SetParamInfo(1, 1, gclParameter("x", 					    PortionSpec(porANYTYPE,1)));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "NthElement", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_NthElement, porANYTYPE, 2));  FuncObj->SetParamInfo(0, 0, gclParameter("list", 					    PortionSpec(porANYTYPE, NLIST),					    REQUIRED, BYREF));  FuncObj->SetParamInfo(0, 1, gclParameter("n", porINTEGER));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "Remove", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Remove, 				       PortionSpec(porANYTYPE, 1), 2));  FuncObj->SetParamInfo(0, 0, gclParameter("list",					    PortionSpec(porANYTYPE, NLIST)));  FuncObj->SetParamInfo(0, 1, gclParameter("n", porNUMBER));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "NthElement", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_NthElementText, porTEXT, 2));  FuncObj->SetParamInfo(0, 0, gclParameter("text", porTEXT));  FuncObj->SetParamInfo(0, 1, gclParameter("n", porINTEGER));  gsm->AddFunction(FuncObj);  //--------------------------- Text -----------------------    FuncObj = new gclFunction(*gsm, "Text", 2);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Text_Number, porTEXT, 1, x_Number));  FuncObj->SetFuncInfo(1, gclSignature(GSM_TextText, porTEXT, 1));  FuncObj->SetParamInfo(1, 0, gclParameter("x", porTEXT));  gsm->AddFunction(FuncObj);    //-------------------------- Integer ------------------------  FuncObj = new gclFunction(*gsm, "Integer", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_IntegerNumber, porNUMBER,				       1, x_Number));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "StartWatch", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_StartWatch, porNUMBER, 0));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "StopWatch", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_StopWatch, porNUMBER, 0));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "ElapsedTime", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_ElapsedTime, porNUMBER, 0));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "IsWatchRunning", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_IsWatchRunning, porBOOLEAN, 0));  gsm->AddFunction(FuncObj);  //-------------------------- List -----------------------------  FuncObj = new gclFunction(*gsm, "List", 5);  FuncObj->SetFuncInfo(0, gclSignature(GSM_List, PortionSpec(porANYTYPE, 1), 				       2, 0, funcNONLISTABLE));  FuncObj->SetParamInfo(0, 0, gclParameter("x", porANYTYPE & 					    ~(porNUMBER | porNUMBER | porNFG | porEFG )));  FuncObj->SetParamInfo(0, 1, gclParameter("length", porNUMBER, 					    new NumberPortion(1)));  FuncObj->SetFuncInfo(1, gclSignature(GSM_List_List, 				       PortionSpec(porANYTYPE, 2), 				       2, 0, funcNONLISTABLE));  FuncObj->SetParamInfo(1, 0, gclParameter("x", PortionSpec(porANYTYPE,1)));  FuncObj->SetParamInfo(1, 1, gclParameter("length", porNUMBER, 					    new NumberPortion(1)));  FuncObj->SetFuncInfo(2, gclSignature(GSM_List_Number,				       PortionSpec(porNUMBER, 1),				       3, 0, funcNONLISTABLE));  FuncObj->SetParamInfo(2, 0, gclParameter("x", porNUMBER));  FuncObj->SetParamInfo(2, 1, gclParameter("length", porNUMBER,					    new NumberPortion(1)));  FuncObj->SetParamInfo(2, 2, gclParameter("delta", porNUMBER,					    new NumberPortion(0)));  FuncObj->SetFuncInfo(3, gclSignature(GSM_List_Nfg,				       PortionSpec(porNFG, 1), 				       2, 0, funcNONLISTABLE));  FuncObj->SetParamInfo(3, 0, gclParameter("x", porNFG));  FuncObj->SetParamInfo(3, 1, gclParameter("length", porNUMBER,					    new NumberPortion(1)));  FuncObj->SetFuncInfo(4, gclSignature(GSM_List_Efg,				       PortionSpec(porEFG, 1), 				       2, 0, funcNONLISTABLE));  FuncObj->SetParamInfo(4, 0, gclParameter("x", porEFG));  FuncObj->SetParamInfo(4, 1, gclParameter("length", porNUMBER,					    new NumberPortion(1)));  gsm->AddFunction(FuncObj);  //--------------------------- Dot ----------------------------  FuncObj = new gclFunction(*gsm, "Dot", 1);    FuncObj->SetFuncInfo(0, gclSignature(GSM_Dot, porNUMBER, 2));  FuncObj->SetParamInfo(0, 0, gclParameter("x", PortionSpec(porNUMBER,1)));  FuncObj->SetParamInfo(0, 1, gclParameter("y", PortionSpec(porNUMBER,1)));  gsm->AddFunction(FuncObj);  //----------------------- ArgMax ------------------------  FuncObj = new gclFunction(*gsm, "ArgMax", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_ArgMax, porNUMBER, 1));  FuncObj->SetParamInfo(0, 0, gclParameter("x", PortionSpec(porNUMBER,1)));  gsm->AddFunction(FuncObj);  //------------------ Transpose -----------------------  FuncObj = new gclFunction(*gsm, "Transpose", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Transpose, 				       PortionSpec(porANYTYPE, 2), 1));  FuncObj->SetParamInfo(0, 0, gclParameter("x", PortionSpec(porANYTYPE,2), 					    REQUIRED, BYVAL));  gsm->AddFunction(FuncObj);  //------------------ Inverse -----------------------  FuncObj = new gclFunction(*gsm, "Inverse", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Inverse, 				       PortionSpec(porNUMBER,2), 1));  FuncObj->SetParamInfo(0, 0, gclParameter("x", PortionSpec(porNUMBER,2), 					    REQUIRED, BYVAL));  gsm->AddFunction(FuncObj);  //------------------ Sort -----------------------  FuncObj = new gclFunction(*gsm, "Sort", 4);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Sort_Number,				       PortionSpec(porNUMBER, 1), 1));  FuncObj->SetParamInfo(0, 0, gclParameter("x", PortionSpec(porNUMBER,1)));  FuncObj->SetFuncInfo(1, gclSignature(GSM_Sort_Text,				       PortionSpec(porTEXT, 1), 1));  FuncObj->SetParamInfo(1, 0, gclParameter("x", PortionSpec(porTEXT,1)));  /*  FuncObj->SetFuncInfo(2, gclSignature(GSM_Sort_By_Integer,				       PortionSpec(porANYTYPE, 1), 2));  FuncObj->SetParamInfo(2, 0, gclParameter("x", PortionSpec(porANYTYPE,1)));  FuncObj->SetParamInfo(2, 1, gclParameter("by", PortionSpec(porNUMBER,1)));  */  FuncObj->SetFuncInfo(2, gclSignature(GSM_Sort_By_Number,				       PortionSpec(porANYTYPE, 1), 2));  FuncObj->SetParamInfo(2, 0, gclParameter("x", PortionSpec(porANYTYPE,1)));  FuncObj->SetParamInfo(2, 1, gclParameter("by", PortionSpec(porNUMBER,1)));  FuncObj->SetFuncInfo(3, gclSignature(GSM_Sort_By_Text,				       PortionSpec(porANYTYPE, 1), 2));  FuncObj->SetParamInfo(3, 0, gclParameter("x", PortionSpec(porANYTYPE,1)));  FuncObj->SetParamInfo(3, 1, gclParameter("by", PortionSpec(porTEXT,1)));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "Filter", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Filter, 				       PortionSpec(porANYTYPE, 1), 2));  FuncObj->SetParamInfo(0, 0, gclParameter("x",                               PortionSpec(porANYTYPE, NLIST, porNULLSPEC )));  FuncObj->SetParamInfo(0, 1, gclParameter("y", 					    PortionSpec(porBOOLEAN, NLIST)));  gsm->AddFunction(FuncObj);  FuncObj = new gclFunction(*gsm, "Flatten", 1);  FuncObj->SetFuncInfo(0, gclSignature(GSM_Flatten, 				       PortionSpec(porANYTYPE, 1), 2));  FuncObj->SetParamInfo(0, 0, gclParameter("x",                               PortionSpec(porANYTYPE, NLIST, porNULLSPEC )));  FuncObj->SetParamInfo(0, 1, gclParameter("levels", porINTEGER,					    new NumberPortion(0)));  gsm->AddFunction(FuncObj);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?