Browse Source

Added functions. Removed useless functions. Cleaned files.

master
frnmst/Franco Masotti 6 years ago
parent
commit
6d05a26f2b
  1. 101
      salibc.c
  2. 8
      salibc.h
  3. 99
      salibc_test.c

101
salibc.c

@ -49,6 +49,10 @@ element_null (void *element)
return (element == NULL);
}
/* Since memory is not always allocated consecutively, the following controls
* may fail because the only check memory addresses and not if that memory
* belongs to something else. */
#ifdef MEMORY_OVERLAP_CHECK
static bool
memory_overlaps (void *chunk1, void *chunk2, size_t fullsize)
{
@ -66,6 +70,16 @@ memory_overlaps (void *chunk1, void *chunk2, size_t fullsize)
return ((c1 <= c2) && (c1 + fullsize >= c2)) || ((c2 <= c1)
&& (c2 + fullsize >= c1));
}
#else
static bool
memory_overlaps (void *chunk1, void *chunk2, size_t fullsize)
{
(void) chunk1;
(void) chunk2;
(void) fullsize;
return false;
}
#endif
/*
******************
@ -130,7 +144,6 @@ array_memcopy (Array a, int index, void *element)
return false;
}
bool
array_null (Array a)
{
@ -263,52 +276,6 @@ array_get (Array a, int index)
return (array_indexpointer (a, index));
}
/* BUGGY PART--------------------------------------------- */
void
array_printelement (Array a, int index, const char *format)
{
char buf[128];
sprintf (buf, format, *(array_indexpointer (a, index)));
printf ("%s", buf);
/* printf (format, * (array_indexpointer (a, index) ) );*/
}
void
array_print (Array a, const char *format)
{
int i;
assert (!array_empty (a));
for (i = 0; i < array_length (a); i++)
array_printelement (a, i, format);
}
bool
array_orderedasc (Array a)
{
int i;
/* With 1 or 0 elements the array is surely ordered. */
if (array_length (a) <= 1)
return true;
for (i = 0; i < array_length (a) - 1; i++)
{
if (*((double *) array_indexpointer (a, i)) < *
((double *) array_indexpointer (a, i + 1)))
printf ("in--OK\n");
if (memcmp (array_indexpointer (a, i + i), array_indexpointer (a, i),
array_size (a)) < 0)
return false;
}
return true;
}
/* END OF BUGGY PART------------------------------------------ */
Array
array_copy (Array a1)
{
@ -381,6 +348,7 @@ array_resize (Array a, int new_length)
return true;
}
/* This alters the input. */
bool
array_append (Array a, void *element)
{
@ -410,12 +378,33 @@ array_trim (Array a)
return NULL;
}
/* Array array_merge (Array a1, Array a2) */
/* Array array_sum (Array a ) */
/* Array array_merge (Array a1, Array a2) */
/* The following two return the pointer to the elements. If referenced they
* return the value. */
/* char *array_maxelement (Array a ) */
/* char *array_minelement (Array a ) */
/* ? array_ascorder (Array a) */
/* ? array_dscorder (Array a) */
/* A new array is created: new_array={a1,a2} */
Array
array_merge (Array a1, Array a2)
{
Array new_array;
int i, j, total_length = array_length (a1) + array_length (a2);
/* Safety controls. */
if ((array_null (a1) && array_null (a2)) || (array_size (a1) !=
array_size (a2)))
return NULL;
new_array = array_new (total_length, array_size (a1));
if (array_null (new_array))
return NULL;
for (i = 0; i < array_length (a1); i++)
if (!array_memcopy (new_array, i, array_indexpointer (a1, i)))
return NULL;
j = 0;
for (i = array_length (a1); i < total_length; i++)
{
if (!array_memcopy (new_array, i, array_indexpointer (a1, j)))
return NULL;
j++;
}
return new_array;
}

8
salibc.h

@ -23,6 +23,13 @@
#ifndef SALIBC
#define SALIBC
/* Check C version. */
#define ISOC99_SOURCE
#define _POSIX_C_SOURCE 199309L
#if __STDC_VERSION__ != 199901L
#error "ANSI C99 not available"
#endif
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
@ -56,5 +63,6 @@ extern Array array_copy (Array a1);
extern bool array_resize (Array a, int new_length);
extern bool array_append (Array a, void *element);
extern char *array_trim (Array a);
extern Array array_merge (Array a1, Array a2);
#endif

99
salibc_test.c

@ -24,47 +24,68 @@
#ifdef SALIBC_TEST
int main (void )
int
main (void)
{
Array arr0, arr1, arr2, arr3;
char a = 'f';
int b = 421;
double c = 3223.554;
long double d=123423509.99999999;
arr0 = array_new ( 50, sizeof ( char ) );
arr1 = array_new ( 50, sizeof ( int ) );
arr2 = array_new ( 50, sizeof ( double ) );
arr3 = array_new ( 50, sizeof ( long double ) );
array_put ( arr0, 20, &a );
array_put ( arr1, 21, &b );
array_put ( arr2, 22, &c );
array_put ( arr3, 23, &d );
printf ( "%c\n", * ((char *) array_get ( arr0, 20 ) ) );
printf ( "%d\n", * ((int *) array_get ( arr1, 21 ) ) );
printf ( "%f\n", * ((double *) array_get ( arr2, 22 ) ) );
printf ( "%.9Lf\n", * ((long double *) array_get ( arr3, 23 ) ) );
array_append ( arr0, &a );
array_append ( arr0, &a );
array_append ( arr0, &a );
printf ( "%c\n", * ((char *) array_get ( arr0, 50 ) ) );
printf ( "%c\n", * ((char *) array_get ( arr0, 51 ) ) );
printf ( "%c\n", * ((char *) array_get ( arr0, 52 ) ) );
printf ( "%c\n", * ((char *) array_trim ( arr0 ) ) );
printf ( "%c\n", * ((char *) array_trim ( arr0 ) ) );
printf ( "%c\n", * ((char *) array_trim ( arr0 ) ) );
printf ( "null char: %c\n", * ((char *) array_trim ( arr0 ) ) );
array_resize ( arr2, 0 );
if ( array_empty ( arr2 ) )
printf ("arr2's length is now equal to zero\n");
return 0;
int i;
Array arr0, arr1, arr2, arr3, arr4, arr5;
char a = 'f';
int b = 421;
double c = 3223.554;
long double d = 123423509.99999999, e = 1435.654;
arr0 = array_new (50, sizeof (char));
arr1 = array_new (50, sizeof (int));
arr2 = array_new (50, sizeof (double));
arr3 = array_new (50, sizeof (long double));
array_put (arr0, 20, &a);
array_put (arr1, 21, &b);
array_put (arr2, 22, &c);
array_put (arr3, 23, &d);
printf ("%c\n", *((char *) array_get (arr0, 20)));
printf ("%d\n", *((int *) array_get (arr1, 21)));
printf ("%f\n", *((double *) array_get (arr2, 22)));
printf ("%.9Lf\n", *((long double *) array_get (arr3, 23)));
array_append (arr0, &a);
array_append (arr0, &a);
array_append (arr0, &a);
printf ("%c\n", *((char *) array_get (arr0, 50)));
printf ("%c\n", *((char *) array_get (arr0, 51)));
printf ("%c\n", *((char *) array_get (arr0, 52)));
printf ("%c\n", *((char *) array_trim (arr0)));
printf ("%c\n", *((char *) array_trim (arr0)));
printf ("%c\n", *((char *) array_trim (arr0)));
printf ("null char: %c\n", *((char *) array_trim (arr0)));
array_resize (arr2, 0);
if (array_empty (arr2))
printf ("arr2's length is now equal to zero\n");
arr4 = array_new (3, sizeof (long double));
array_put (arr4, 0, &d);
array_put (arr4, 1, &d);
array_put (arr4, 2, &e);
printf ("Length before merge = %d\n", array_length (arr3));
if (array_null (arr5 = array_merge (arr3, arr4)))
printf ("Merge failed.\n");
printf ("Length after merge = %d\n", array_length (arr5));
for (i = 0; i < array_length (arr4); i++)
printf ("%Lf\n", *((long double *) array_get (arr4, i)));
if (*((long double *) array_get (arr4, 1)) >
*((long double *) array_get (arr4, 2)))
printf ("%Lf is greater than %Lf\n",
*((long double *) array_get (arr4, 1)),
*((long double *) array_get (arr4, 2)));
return 0;
}
#endif