📄 int_listv.c
字号:
return(&extractInfo);}/* * 'length/size' field */static char *lengthDoc = "{[= <length>]} {Sets/Gets the length/size of a listv}";static void * GetLengthListvV(VALUE val, void **arg){ /* Documentation */ if (val == NULL) return(lengthDoc); return(GetIntField(((LISTV) val)->length,arg));}/* Set the length of a listv (perform allocation) */#define LISTVINCRLENGTH 10 /* If n element are required, we allocate n+LISTVINCRLENGTH */#define LISTVMINLENGTH 10 /* The minimum number of elements of a listv */void SetLengthListv(LISTV lv, int length){ int i,l; if (length <= lv->nAlloc) { for (i=lv->length; i<length;i++) { if (lv->values[i] != NULL) { DeleteValue(lv->values[i]); lv->values[i] = NULL; } lv->f[i] = 0; } } else if (lv->nAlloc != 0) { l = length+LISTVINCRLENGTH; lv->f = Realloc(lv->f,sizeof(LWFLOAT)*l); lv->values = Realloc(lv->values,sizeof(VALUE)*l); for (i=lv->length; i<l;i++) { lv->values[i] = NULL; lv->f[i] = 0; } lv->nAlloc = l; } else { if (length < LISTVMINLENGTH) l = LISTVMINLENGTH; else l = length + LISTVINCRLENGTH; lv->f = Calloc(l,sizeof(LWFLOAT)); lv->values = Calloc(l,sizeof(VALUE)); lv->nAlloc = l; } lv->length = length;}static void * SetLengthListvV(VALUE val, void **arg){ LISTV lv; int length; /* doc */ if (val == NULL) return(lengthDoc); lv = (LISTV) val; length = lv->length; if (SetIntField(&length,arg,FieldPositive)==NULL) return(NULL); SetLengthListv(lv,length);}/* * 'nAlloc' field */static char *nallocDoc = "{} {Gets the allocation size of a listv}";static void * GetNAllocListvV(VALUE val, void **arg){ /* Documentation */ if (val == NULL) return(nallocDoc); return(GetIntField(((LISTV) val)->nAlloc,arg));}/* * (Des)Allocation of a Listv *//* Allocate a new listv */LISTV NewListv(void){ LISTV lv; #ifdef DEBUGALLOC DebugType = "Listv";#endif lv = Malloc(sizeof(struct listvValue)); InitValue(lv,&tsListv); lv->length = 0; lv->nAlloc = 0; lv->values = NULL; lv->f = NULL; return(lv);}LISTV TNewListv(void){ LISTV lv; lv = NewListv(); TempValue( lv); return(lv);}/* Delete a listv */void DeleteListv(LISTV lv){ int i; RemoveRefValue( lv); if (lv->nRef>0) return; if (lv->f != NULL) Free(lv->f); if (lv->values != NULL) { for (i=0;i<lv->length;i++) { if(lv->values[i] != NULL) DeleteValue(lv->values[i]); } Free(lv->values); } #ifdef DEBUGALLOC DebugType = "Listv";#endif Free(lv);}/* Get the nth element wich should be a number */LWFLOAT GetListvNthFloat(LISTV lv,int n){ if (n>=lv->length || n< 0) Errorf("GetListvNthFloat() : index out of range (%d)",n); if (lv->values[n] == NULL) return(lv->f[n]); if (GetTypeValue(lv->values[n]) != numType) Errorf("GetListvNthFloat() : Bad element of type '%s'",GetTypeValue(lv->values[n])); return(CastValue(lv->values[n],NUMVALUE)->f);}char *GetListvNth(LISTV lv,int n,VALUE *val,LWFLOAT *f){ char *type; if (n>=lv->length || n< 0) Errorf("GetListvNth() : index out of range (%d)",n); if (lv->values[n] == NULL) { *val = NULL; *f = lv->f[n]; return(numType); } *val = lv->values[n]; type = GetTypeValue(*val); if (type == numType) *f = ((NUMVALUE) (*val))->f; return(type);}char *GetListvNthStr(LISTV lv,int n){ if (n>=lv->length || n< 0) Errorf("GetListvNthStr() : index out of range (%d)",n); if (lv->values[n] == NULL) Errorf("GetListvNthStr() : expect a string, not a number"); if (GetTypeValue(lv->values[n]) != strType) Errorf("GetListvNthStr() : expect a string, not a '%s'",GetTypeValue(lv->values[n])); return(CastValue(lv->values[n],STRVALUE)->str);}void SetListvNthFloat(LISTV lv,int n,LWFLOAT f){ if (n>=lv->length || n < 0) Errorf("GetListvNth() : index out of range (%d)",n); if (lv->values[n] != NULL) { DeleteValue(lv->values[n]); lv->values[n] = NULL; } lv->f[n] = f; return;}void SetListvNthValue(LISTV lv,int n,VALUE val){ extern char TestRecursiveListv(LISTV lv, LISTV lv1); if (n>=lv->length || n < 0) Errorf("GetListvNth() : index out of range (%d)",n); if (GetTypeValue(val) == listvType && TestRecursiveListv(lv,CastValue(val,LISTV))) Errorf("Sorry, recursive list are not allowed"); if (lv->values[n] != NULL) { DeleteValue(lv->values[n]); lv->values[n] = NULL; } lv->values[n] = val; AddRefValue(val); return;}int AreEqualListv(LISTV lv1,LISTV lv2){ int i; if (lv1->length != lv2->length) return(NO); for (i=0;i<lv1->length;i++) { if (lv1->values[i] != lv2->values[i]) return(NO); if (lv1->values[i] == NULL) { if (lv1->f[i] != lv2->f[i]) return(NO); } else { if (strType == GetTypeValue((VALUE) lv1->values[i])) { if (strcmp(CastValue(lv1->values[i],STRVALUE)->str,CastValue(lv2->values[i],STRVALUE)->str)) return(NO); } } } return(YES);}/* Copy a listv */LISTV CopyListv(LISTV in,LISTV out){ int i; if (in == out) return(out); if (out == NULL) out = NewListv(); SetLengthListv(out,in->length); for (i=0;i<in->length;i++) { if (out->values[i] != NULL) DeleteValue(out->values[i]); out->values[i] = in->values[i]; if (in->values[i]) AddRefValue(in->values[i]); else out->f[i] = in->f[i]; } return(out);}/* Clear a listv */void ClearListv(LISTV lv){ int i; if (lv->f != NULL) Free(lv->f); if (lv->values != NULL) { for (i=0;i<lv->length;i++) { if(lv->values[i] != NULL) DeleteValue(lv->values[i]); } Free(lv->values); } lv->length = 0; lv->nAlloc = 0; lv->values = NULL; lv->f = NULL;}/* Concat 2 listv's */void ConcatListv(LISTV lv1,LISTV lv2,LISTV lv3){ int i; int l1 = lv1->length; int l2 = lv2->length; int l3 = l1+l2; SetLengthListv(lv3,l3); /* We first copy l2 */ for (i=l1;i<l3;i++) { if (lv2->values[i-l1]) AddRefValue(lv2->values[i-l1]); else lv3->f[i] = lv2->f[i-l1]; if (lv3->values[i] != NULL) DeleteValue(lv3->values[i]); lv3->values[i] = lv2->values[i-l1]; } /* Then if necessary we copy l1 */ if (lv1 != lv3) { for (i=0;i<l1;i++) { if (lv1->values[i]) AddRefValue(lv1->values[i]); else lv3->f[i] = lv1->f[i]; if (lv3->values[i] != NULL) DeleteValue(lv3->values[i]); lv3->values[i] = lv1->values[i]; } } }/* Multiply a listv with an integer */void MultListv(LISTV lv1, int n, LISTV lv3){ int i,j,k,j0; int l1 = lv1->length; int l3 = l1*n; /* if (lv1 == lv3) Errorf("MultListv() : the two listv should be different"); */ SetLengthListv(lv3,l3); if (n == 0) return; if (lv1 == lv3) j0 = 1; else j0 = 0; for (j=j0,k=j0*l1;j<n;j++) { for (i=0;i<l1;i++,k++) { if (lv1->values[i]) AddRefValue(lv1->values[i]); else lv3->f[k] = lv1->f[i]; if (lv3->values[k] != NULL) DeleteValue(lv3->values[k]); lv3->values[k] = lv1->values[i]; } }}/* Test if lv1 can be added as an element of lv */char TestRecursiveListv(LISTV lv, LISTV lv1){ int i; if (lv == lv1) return(YES); for (i=0;i<lv1->length;i++) { if (lv1->values[i]==NULL || GetTypeValue(lv1->values[i]) != listvType) continue; if (TestRecursiveListv(lv,CastValue(lv1->values[i],LISTV))) return(YES); } return(NO);}/* Append a Variable Content at the end of a listv */void AppendValue2Listv (LISTV lv, VALUE val){ if (GetTypeValue(val) == listvType && TestRecursiveListv(lv,CastValue(val,LISTV))) Errorf("Sorry, recursive list are not allowed"); SetLengthListv(lv,lv->length+1); val = ValueOf(val); lv->values[lv->length-1] = val; AddRefValue(val);}/* Append a LWFLOAT at the end of a listv */void AppendFloat2Listv (LISTV lv, LWFLOAT f){ SetLengthListv(lv,lv->length+1); lv->values[lv->length-1] = NULL; lv->f[lv->length-1] = f;}/* Append a LWFLOAT array at the end of a listv */void AppendFloatArray2Listv (LISTV lv, LWFLOAT *f, int n){ int i,j; if (n <= 0) return; j=lv->length; SetLengthListv(lv,lv->length+n); for (i=0;i<n;i++,j++) { lv->values[j] = NULL; lv->f[j] = f[i]; }}/* Append an int the end of a listv */void AppendInt2Listv (LISTV lv, int i){ SetLengthListv(lv,lv->length+1); lv->values[lv->length-1] = NULL; lv->f[lv->length-1] = i;}/* Append a LWFLOAT array at the end of a listv */void AppendIntArray2Listv (LISTV lv, int *i, int n){ int i1,j1; j1=lv->length; SetLengthListv(lv,lv->length+n); for (i1=0;i1<n;i1++,j1++) { lv->values[j1] = NULL; lv->f[j1] = i[i1]; }}/* Append a string to the end of a listv */void AppendStr2Listv (LISTV lv, char *str){ STRVALUE sc; sc = NewStrValue(); SetStrValue(sc,str); SetLengthListv(lv,lv->length+1); lv->values[lv->length-1] = (VALUE) sc;}/* The result is supposed to contain a list and we just want to append a string */void AppendStr2Listvf(LISTV lv, char *format,...){ Errorf("AppendStr2Listvf pas fait");}/* Append a listv to the end of a listv */void AppendListv2Listv (LISTV lv, LISTV lv1){ int i,j; int l1=lv1->length; j = lv->length; SetLengthListv(lv,lv->length+lv1->length); for (i=0;i<l1;i++) { if (lv1->values[i] == NULL) { lv->values[j] = NULL; lv->f[j] = lv1->f[i]; } else { if (GetTypeValue(lv1->values[i]) == listvType && TestRecursiveListv(lv,CastValue(lv1->values[i],LISTV))) Errorf("Sorry, recursive list are not allowed"); lv->values[j] = lv1->values[i]; AddRefValue(lv1->values[i]); } j++; }}extern void ApplyProc2Listv(LWPROC proc, LISTV lv);static char _theFlagInverse=0;static int qsortcmpstr(const void *s1,const void *s2){ switch(_theFlagInverse) { case 0: return(strcmp((*((STRVALUE *) s1))->str,(*((STRVALUE *) s2))->str)); default : return(strcmp((*((STRVALUE *) s2))->str,(*((STRVALUE *) s1))->str)); }}static int qsortcmpnum(const void *s1,const void *s2){ switch(_theFlagInverse) { case 0: return((*((LWFLOAT *) s1)) >= (*((LWFLOAT *) s2)) ? 1 : -1); default : return((*((LWFLOAT *) s2)) >= (*((LWFLOAT *) s1)) ? 1 : -1); }}static LWPROC _theProc=NULL;static LISTV _theLv= NULL;static int qsortcmpvc(const void *s1,const void *s2){ _theLv->values[0] = (*((VALUE *) s1)); _theLv->values[1] = (*((VALUE *) s2)); ApplyProc2Listv(_theProc,_theLv); switch(_theFlagInverse) { case 0: return(GetResultInt()); default : return(-GetResultInt()); }}LISTV SortListv(LISTV lv, LWPROC proc,char flagInverse){ VALUE *vcarray; LWFLOAT *farray; int i; LISTV lv1; if (lv->length == 0) return(lv); _theFlagInverse = flagInverse; lv1 = TNewListv(); CopyListv(lv,lv1); lv = lv1; if (proc != NULL) { vcarray = TMalloc(sizeof(VALUE)*lv->length); for (i=0;i<lv->length;i++) { vcarray[i] = lv->values[i]; if (lv->values[i] != NULL && GetTrueTypeValue(lv->values[i]) == numType) { lv->f[i] = ((NUMVALUE) (lv->values[i]))->f; DeleteValue(lv->values[i]); lv->values[i] = NULL; } if (lv->values[i] == NULL) { vcarray[i] = (VALUE) TNewNumValue(); ((NUMVALUE) (vcarray[i]))->f = lv->f[i]; } else { vcarray[i] = lv->values[i]; } } _theProc = proc; if (_theLv == NULL) { _theLv = NewListv(); SetLengthListv(_theLv,2); } qsort(vcarray,lv->length,sizeof(VALUE),&qsortcmpvc); for (i=0;i<lv->length;i++) { if (GetTypeValue(vcarray[i]) == numType) { lv->f[i] = CastValue(vcarray[i],NUMVALUE)->f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -