"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "Common/ListUtils.cpp" between
getdp-3.4.0-source.tgz and getdp-3.5.0-source.tgz

About: GetDP is a general finite element solver using mixed elements to discretize de Rham-type complexes in one, two and three dimensions.

ListUtils.cpp  (getdp-3.4.0-source.tgz):ListUtils.cpp  (getdp-3.5.0-source.tgz)
// GetDP - Copyright (C) 1997-2021 P. Dular and C. Geuzaine, University of Liege // GetDP - Copyright (C) 1997-2022 P. Dular and C. Geuzaine, University of Liege
// //
// See the LICENSE.txt file for license information. Please report all // See the LICENSE.txt file for license information. Please report all
// issues on https://gitlab.onelab.info/getdp/getdp/issues. // issues on https://gitlab.onelab.info/getdp/getdp/issues.
// //
// Contributor(s): // Contributor(s):
// Marc Ume // Marc Ume
// //
#include "GetDPConfig.h" #include "GetDPConfig.h"
#if !defined(HAVE_NO_STDINT_H) #if !defined(HAVE_NO_STDINT_H)
skipping to change at line 28 skipping to change at line 28
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
#include "MallocUtils.h" #include "MallocUtils.h"
#include "ListUtils.h" #include "ListUtils.h"
#include "TreeUtils.h" #include "TreeUtils.h"
#include "Message.h" #include "Message.h"
#if !defined(HAVE_GMSH) #if !defined(HAVE_GMSH)
int fcmp_int(const void *a, const void *b) int fcmp_int(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
{
return (*(int *)a - *(int *)b);
}
int fcmp_absint(const void *a, const void *b) int fcmp_absint(const void *a, const void *b)
{ {
return (abs(*(int *)a) - abs(*(int *)b)); return (abs(*(int *)a) - abs(*(int *)b));
} }
int fcmp_double(const void *a, const void *b) int fcmp_double(const void *a, const void *b)
{ {
double cmp; double cmp;
skipping to change at line 55 skipping to change at line 52
else if(cmp < -1.e-16) else if(cmp < -1.e-16)
return -1; return -1;
else else
return 0; return 0;
} }
List_T *List_Create(int n, int incr, int size) List_T *List_Create(int n, int incr, int size)
{ {
List_T *liste; List_T *liste;
if(n <= 0) if(n <= 0) n = 1;
n = 1; if(incr <= 0) incr = 1;
if(incr <= 0)
incr = 1;
liste = (List_T *) Malloc(sizeof(List_T)); liste = (List_T *)Malloc(sizeof(List_T));
liste->nmax = 0; liste->nmax = 0;
liste->incr = incr; liste->incr = incr;
liste->size = size; liste->size = size;
liste->n = 0; liste->n = 0;
liste->isorder = 0; liste->isorder = 0;
liste->array = NULL; liste->array = NULL;
List_Realloc(liste, n); List_Realloc(liste, n);
return (liste); return (liste);
} }
void List_Delete(List_T * liste) void List_Delete(List_T *liste)
{ {
if(!liste) if(!liste) return;
return;
Free(liste->array); Free(liste->array);
Free(liste); Free(liste);
} }
void List_Realloc(List_T * liste, int n) void List_Realloc(List_T *liste, int n)
{ {
if(n <= 0) if(n <= 0) return;
return;
if(liste->array == NULL) { if(liste->array == NULL) {
// This does not permit to allocate lists smaller that liste->incr: // This does not permit to allocate lists smaller that liste->incr:
//liste->nmax = ((n - 1) / liste->incr + 1) * liste->incr; // liste->nmax = ((n - 1) / liste->incr + 1) * liste->incr;
// So this is much better // So this is much better
liste->nmax = n; liste->nmax = n;
liste->array = (char *)Malloc(liste->nmax * liste->size); liste->array = (char *)Malloc(liste->nmax * liste->size);
} }
else if(n > liste->nmax) { else if(n > liste->nmax) {
liste->nmax = ((n - 1) / liste->incr + 1) * liste->incr; liste->nmax = ((n - 1) / liste->incr + 1) * liste->incr;
liste->array = (char *)Realloc(liste->array, liste->nmax * liste->size); liste->array = (char *)Realloc(liste->array, liste->nmax * liste->size);
} }
} }
void List_Add(List_T * liste, void *data) void List_Add(List_T *liste, void *data)
{ {
liste->n++; liste->n++;
List_Realloc(liste, liste->n); List_Realloc(liste, liste->n);
liste->isorder = 0; liste->isorder = 0;
memcpy(&liste->array[(liste->n - 1) * liste->size], data, liste->size); memcpy(&liste->array[(liste->n - 1) * liste->size], data, liste->size);
} }
int List_Nbr(List_T * liste) int List_Nbr(List_T *liste) { return (liste) ? liste->n : 0; }
{
return (liste) ? liste->n : 0;
}
void List_Read(List_T * liste, int index, void *data) void List_Read(List_T *liste, int index, void *data)
{ {
if((index < 0) || (index >= liste->n)) if((index < 0) || (index >= liste->n))
Message::Fatal("Wrong list index (read)"); Message::Fatal("Wrong list index (read)");
memcpy(data, &liste->array[index * liste->size], liste->size); memcpy(data, &liste->array[index * liste->size], liste->size);
} }
void List_Write(List_T * liste, int index, void *data) void List_Write(List_T *liste, int index, void *data)
{ {
if((index < 0) || (index >= liste->n)) if((index < 0) || (index >= liste->n))
Message::Error("Wrong list index (write)"); Message::Error("Wrong list index (write)");
else { else {
liste->isorder = 0; liste->isorder = 0;
memcpy(&liste->array[index * liste->size], data, liste->size); memcpy(&liste->array[index * liste->size], data, liste->size);
} }
} }
void List_Put(List_T * liste, int index, void *data) void List_Put(List_T *liste, int index, void *data)
{ {
if(index < 0) if(index < 0)
Message::Error("Wrong list index (put)"); Message::Error("Wrong list index (put)");
else { else {
if(index >= liste->n) { if(index >= liste->n) {
liste->n = index + 1; liste->n = index + 1;
List_Realloc(liste, liste->n); List_Realloc(liste, liste->n);
List_Write(liste, index, data); List_Write(liste, index, data);
} }
else { else {
List_Write(liste, index, data); List_Write(liste, index, data);
} }
} }
} }
void List_Pop(List_T * liste) void List_Pop(List_T *liste)
{ {
if(liste->n > 0) if(liste->n > 0) liste->n--;
liste->n--;
} }
void *List_Pointer(List_T * liste, int index) void *List_Pointer(List_T *liste, int index)
{ {
if((index < 0) || (index >= liste->n)) if((index < 0) || (index >= liste->n))
Message::Fatal("Wrong list index (pointer)"); Message::Fatal("Wrong list index (pointer)");
liste->isorder = 0; liste->isorder = 0;
return (&liste->array[index * liste->size]); return (&liste->array[index * liste->size]);
} }
void *List_Pointer_NoChange(List_T * liste, int index) void *List_Pointer_NoChange(List_T *liste, int index)
{ {
if((index < 0) || (index >= liste->n)) if((index < 0) || (index >= liste->n))
Message::Fatal("Wrong list index (pointer)"); Message::Fatal("Wrong list index (pointer)");
return (&liste->array[index * liste->size]); return (&liste->array[index * liste->size]);
} }
void *List_Pointer_Fast(List_T * liste, int index) void *List_Pointer_Fast(List_T *liste, int index)
{ {
return (&liste->array[index * liste->size]); return (&liste->array[index * liste->size]);
} }
void *List_Pointer_Test(List_T * liste, int index) void *List_Pointer_Test(List_T *liste, int index)
{ {
if(!liste || (index < 0) || (index >= liste->n)) if(!liste || (index < 0) || (index >= liste->n)) return NULL;
return NULL;
liste->isorder = 0; liste->isorder = 0;
return (&liste->array[index * liste->size]); return (&liste->array[index * liste->size]);
} }
void List_Sort(List_T * liste, int (*fcmp) (const void *a, const void *b)) void List_Sort(List_T *liste, int (*fcmp)(const void *a, const void *b))
{ {
if(!liste) return; if(!liste) return;
qsort(liste->array, liste->n, liste->size, fcmp); qsort(liste->array, liste->n, liste->size, fcmp);
} }
int List_Search(List_T * liste, void *data, int List_Search(List_T *liste, void *data,
int (*fcmp) (const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
void *ptr; void *ptr;
if(liste->isorder != 1) { if(liste->isorder != 1) {
List_Sort(liste, fcmp); List_Sort(liste, fcmp);
liste->isorder = 1; liste->isorder = 1;
} }
ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp); ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp);
if(ptr == NULL) if(ptr == NULL) return (0);
return (0);
return (1); return (1);
} }
int List_ISearchSeq(List_T * liste, void *data, int List_ISearchSeq(List_T *liste, void *data,
int (*fcmp) (const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
int i; int i;
if(!liste) if(!liste) return -1;
return -1;
i = 0; i = 0;
while((i < List_Nbr(liste)) && fcmp(data, (void *)List_Pointer(liste, i))) while((i < List_Nbr(liste)) && fcmp(data, (void *)List_Pointer(liste, i)))
i++; i++;
if(i == List_Nbr(liste)) if(i == List_Nbr(liste)) i = -1;
i = -1;
return i; return i;
} }
void *List_PQuery(List_T * liste, void *data, void *List_PQuery(List_T *liste, void *data,
int (*fcmp) (const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
void *ptr; void *ptr;
if(liste->isorder != 1) if(liste->isorder != 1) List_Sort(liste, fcmp);
List_Sort(liste, fcmp);
liste->isorder = 1; liste->isorder = 1;
ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp); ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp);
return (ptr); return (ptr);
} }
int List_PSuppress(List_T * liste, int index) int List_PSuppress(List_T *liste, int index)
{ {
char *ptr; char *ptr;
int len; int len;
ptr = (char *)List_Pointer_NoChange(liste, index); ptr = (char *)List_Pointer_NoChange(liste, index);
if(ptr == NULL) if(ptr == NULL) return (0);
return (0);
liste->n--; liste->n--;
len = liste->n - (((intptr_t)ptr - (intptr_t)liste->array) / liste->size); len = liste->n - (((intptr_t)ptr - (intptr_t)liste->array) / liste->size);
if(len > 0) if(len > 0) memmove(ptr, ptr + liste->size, len * liste->size);
memmove(ptr, ptr + liste->size, len * liste->size);
return (1); return (1);
} }
void List_Invert(List_T * a, List_T * b) void List_Invert(List_T *a, List_T *b)
{ {
int i, N; int i, N;
N = List_Nbr(a); N = List_Nbr(a);
for(i = 0; i < N; i++) { for(i = 0; i < N; i++) { List_Add(b, List_Pointer(a, N - i - 1)); }
List_Add(b, List_Pointer(a, N - i - 1));
}
} }
void List_Reset(List_T * liste) void List_Reset(List_T *liste)
{ {
if(!liste) if(!liste) return;
return;
liste->n = 0; liste->n = 0;
} }
void List_Action(List_T * liste, void (*action) (void *data, void *dummy)) void List_Action(List_T *liste, void (*action)(void *data, void *dummy))
{ {
int i, dummy; int i, dummy;
for(i = 0; i < List_Nbr(liste); i++) for(i = 0; i < List_Nbr(liste); i++)
(*action) (List_Pointer_NoChange(liste, i), &dummy); (*action)(List_Pointer_NoChange(liste, i), &dummy);
} }
void List_Copy(List_T * a, List_T * b) void List_Copy(List_T *a, List_T *b)
{ {
int i, N; int i, N;
N = List_Nbr(a); N = List_Nbr(a);
for(i = 0; i < N; i++) { for(i = 0; i < N; i++) { List_Add(b, List_Pointer(a, i)); }
List_Add(b, List_Pointer(a, i));
}
} }
List_T *ListOfDouble2ListOfInt(List_T *dList) List_T *ListOfDouble2ListOfInt(List_T *dList)
{ {
int n = List_Nbr(dList); int n = List_Nbr(dList);
List_T *iList = List_Create(n, n, sizeof(int)); List_T *iList = List_Create(n, n, sizeof(int));
for(int i = 0; i < n; i++){ for(int i = 0; i < n; i++) {
double d; double d;
List_Read(dList, i, &d); List_Read(dList, i, &d);
int j = (int)d; int j = (int)d;
List_Add(iList, &j); List_Add(iList, &j);
} }
return iList; return iList;
} }
int List_Suppress(List_T *liste, void *data, int List_Suppress(List_T *liste, void *data,
int (*fcmp)(const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
char *ptr; char *ptr;
int len; int len;
ptr = (char*)List_PQuery(liste,data,fcmp) ; ptr = (char *)List_PQuery(liste, data, fcmp);
if (ptr == NULL) return(0); if(ptr == NULL) return (0);
liste->n--; liste->n--;
len = liste->n - (((intptr_t)ptr - (intptr_t)liste->array) / liste->size); len = liste->n - (((intptr_t)ptr - (intptr_t)liste->array) / liste->size);
if (len > 0) memmove(ptr, ptr + liste->size, len * liste->size); if(len > 0) memmove(ptr, ptr + liste->size, len * liste->size);
return(1); return (1);
} }
#endif #endif
// These are not defined in Gmsh: // These are not defined in Gmsh:
static int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE * stream ) static int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{ {
size_t result = fwrite(ptr, size, nmemb, stream); size_t result = fwrite(ptr, size, nmemb, stream);
if(result < nmemb) { if(result < nmemb) {
Message::Error(strerror(errno)); Message::Error(strerror(errno));
if(fflush(stream) < 0) if(fflush(stream) < 0) Message::Error("EOF reached");
Message::Error("EOF reached"); if(fclose(stream) < 0) Message::Error(strerror(errno));
if(fclose(stream) < 0)
Message::Error(strerror(errno));
return 1; return 1;
} }
return 0; return 0;
} }
void List_WriteToFile(List_T * liste, FILE * file, int format) void List_WriteToFile(List_T *liste, FILE *file, int format)
{ {
int i, n; int i, n;
if(!(n = List_Nbr(liste))) if(!(n = List_Nbr(liste))) return;
return;
switch (format) { switch(format) {
case LIST_FORMAT_ASCII: case LIST_FORMAT_ASCII:
if(liste->size == sizeof(double)) if(liste->size == sizeof(double))
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
fprintf(file, " %.16g", *((double *)&liste->array[i * liste->size])); fprintf(file, " %.16g", *((double *)&liste->array[i * liste->size]));
else if(liste->size == sizeof(float)) else if(liste->size == sizeof(float))
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
fprintf(file, " %.16g", *((float *)&liste->array[i * liste->size])); fprintf(file, " %.16g", *((float *)&liste->array[i * liste->size]));
else if(liste->size == sizeof(int)) else if(liste->size == sizeof(int))
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
fprintf(file, " %d", *((int *)&liste->array[i * liste->size])); fprintf(file, " %d", *((int *)&liste->array[i * liste->size]));
else if(liste->size == sizeof(char)) else if(liste->size == sizeof(char))
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
fputc(*((char *)&liste->array[i * liste->size]), file); fputc(*((char *)&liste->array[i * liste->size]), file);
else else
Message::Error("Bad type of data to write list to file (size = %d)", Message::Error("Bad type of data to write list to file (size = %d)",
liste->size); liste->size);
break; break;
case LIST_FORMAT_BINARY: case LIST_FORMAT_BINARY:
safe_fwrite(liste->array, liste->size, n, file); safe_fwrite(liste->array, liste->size, n, file);
break; break;
default: default: Message::Error("Unknown list format"); break;
Message::Error("Unknown list format");
break;
} }
} }
int List_Query(List_T *liste, void *data, int List_Query(List_T *liste, void *data,
int (*fcmp)(const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
void *ptr; void *ptr;
if (liste->isorder != 1) List_Sort(liste,fcmp); if(liste->isorder != 1) List_Sort(liste, fcmp);
liste->isorder = 1; liste->isorder = 1;
ptr = (void *) bsearch(data,liste->array,liste->n,liste->size,fcmp); ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp);
if (ptr == NULL) return(0); if(ptr == NULL) return (0);
memcpy(data,ptr,liste->size); memcpy(data, ptr, liste->size);
return (1); return (1);
} }
void List_Insert(List_T *liste, void *data, void List_Insert(List_T *liste, void *data,
int (*fcmp)(const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
if (List_Search(liste,data,fcmp) == 0) if(List_Search(liste, data, fcmp) == 0) List_Add(liste, data);
List_Add(liste,data);
} }
List_T *List_Copy(List_T *src) List_T *List_Copy(List_T *src)
{ {
List_T *dest = (List_T *)Malloc(sizeof(List_T)); List_T *dest = (List_T *)Malloc(sizeof(List_T));
dest->nmax = src->nmax; dest->nmax = src->nmax;
dest->incr = src->incr; dest->incr = src->incr;
dest->size = src->size; dest->size = src->size;
dest->n = src->n; dest->n = src->n;
dest->isorder = src->isorder; dest->isorder = src->isorder;
dest->array = (char *)Malloc(src->nmax * src->size); dest->array = (char *)Malloc(src->nmax * src->size);
memcpy(dest->array, src->array, src->nmax * src->size); memcpy(dest->array, src->array, src->nmax * src->size);
return dest; return dest;
} }
int List_ISearch(List_T * liste, void *data, int List_ISearch(List_T *liste, void *data,
int (*fcmp) (const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
void *ptr; void *ptr;
if (!liste) return -1 ; if(!liste) return -1;
if(liste->isorder != 1) if(liste->isorder != 1) List_Sort(liste, fcmp);
List_Sort(liste, fcmp);
liste->isorder = 1; liste->isorder = 1;
ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp); ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp);
if(ptr == NULL) if(ptr == NULL) return (-1);
return (-1);
return (((intptr_t)ptr - (intptr_t)liste->array) / liste->size); return (((intptr_t)ptr - (intptr_t)liste->array) / liste->size);
} }
int List_ISearchSeqPartial(List_T *liste, void * data, int i_Start, int List_ISearchSeqPartial(List_T *liste, void *data, int i_Start,
int (*fcmp)(const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
int i ; int i;
if (!liste) return -1 ; if(!liste) return -1;
i = i_Start ; i = i_Start;
while ((i < List_Nbr(liste)) && while((i < List_Nbr(liste)) && fcmp(data, (void *)List_Pointer(liste, i)))
fcmp(data, (void *)List_Pointer(liste, i)) ) i++ ; i++;
if (i == List_Nbr(liste)) i = -1 ; if(i == List_Nbr(liste)) i = -1;
return i ; return i;
} }
int List_Replace(List_T *liste, void *data, int List_Replace(List_T *liste, void *data,
int (*fcmp)(const void *a, const void *b)) int (*fcmp)(const void *a, const void *b))
{ {
void *ptr; void *ptr;
if (liste->isorder != 1) List_Sort(liste,fcmp); if(liste->isorder != 1) List_Sort(liste, fcmp);
liste->isorder = 1; liste->isorder = 1;
ptr = (void *) bsearch(data,liste->array,liste->n,liste->size,fcmp); ptr = (void *)bsearch(data, liste->array, liste->n, liste->size, fcmp);
if (ptr == NULL) { if(ptr == NULL) {
List_Add(liste,data); List_Add(liste, data);
return(0); return (0);
} }
else { else {
memcpy(ptr,data,liste->size); memcpy(ptr, data, liste->size);
return (1); return (1);
} }
} }
static void *lolofind(void *data, void *array, int n, int size, static void *lolofind(void *data, void *array, int n, int size,
int (*fcmp)(const void *a, const void *b) ) int (*fcmp)(const void *a, const void *b))
{ {
char *ptr; char *ptr;
int i; int i;
ptr = (char*)array; ptr = (char *)array;
for (i = 0; i < n; i++) { for(i = 0; i < n; i++) {
if (fcmp(ptr,data) == 0) break; if(fcmp(ptr, data) == 0) break;
ptr += size; ptr += size;
} }
if (i < n) return(ptr); if(i < n) return (ptr);
return(NULL); return (NULL);
} }
static char *startptr = NULL; static char *startptr = NULL;
int List_LQuery(List_T *liste, void *data, int List_LQuery(List_T *liste, void *data,
int (*fcmp)(const void *a, const void *b), int first) int (*fcmp)(const void *a, const void *b), int first)
{ {
char *ptr; char *ptr;
if (first == 1) { if(first == 1) {
ptr = (char *) lolofind(data,liste->array,liste->n,liste->size,fcmp); ptr = (char *)lolofind(data, liste->array, liste->n, liste->size, fcmp);
} }
else { else {
if (startptr != NULL) if(startptr != NULL)
ptr = (char *) lolofind(data,startptr, ptr = (char *)lolofind(data, startptr,
liste->n - (startptr-liste->array)/liste->size, liste->n - (startptr - liste->array) / liste->size,
liste->size,fcmp); liste->size, fcmp);
else else
return(0); return (0);
} }
if (ptr == NULL) return(0); if(ptr == NULL) return (0);
startptr = ptr + liste->size; startptr = ptr + liste->size;
if ( startptr >= ( liste->array + liste->n * liste->size)) if(startptr >= (liste->array + liste->n * liste->size)) startptr = NULL;
startptr = NULL; memcpy(data, ptr, liste->size);
memcpy(data,ptr,liste->size);
return (1); return (1);
} }
 End of changes. 75 change blocks. 
134 lines changed or deleted 102 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)