From 67a7a68746921f1422eb30ec6845fb11d6feaa4a Mon Sep 17 00:00:00 2001 From: Bruce Cowan <bruce@bcowan.me.uk> Date: Sat, 24 Aug 2019 11:08:59 +0100 Subject: [PATCH] Move down a directory --- lib/meson.build | 3 +- meson.build | 35 +-- pthread/meson.build | 4 - add.c => src/add.c | 0 add2.c => src/add2.c | 0 angle.c => src/angle.c | 0 {array => src/array}/meson.build | 0 {array => src/array}/src/array.c | 0 {array => src/array}/src/array.h | 0 {array => src/array}/src/mem.c | 0 {array => src/array}/src/mem.h | 0 {array => src/array}/src/meson.build | 0 {array => src/array}/test/average.c | 0 {array => src/array}/test/basic.c | 0 {array => src/array}/test/empty.c | 0 {array => src/array}/test/meson.build | 0 binary.c => src/binary.c | 0 complex.c => src/complex.c | 0 einstein.c => src/einstein.c | 0 fgets.c => src/fgets.c | 0 fib.c => src/fib.c | 0 fixed-sizeof.c => src/fixed-sizeof.c | 0 gcd.c => src/gcd.c | 0 getchar.c => src/getchar.c | 0 {hashtable => src/hashtable}/hashtable.c | 0 {hashtable => src/hashtable}/hashtable.h | 0 {hashtable => src/hashtable}/meson.build | 0 {hashtable => src/hashtable}/slist.c | 0 {hashtable => src/hashtable}/slist.h | 0 {hashtable => src/hashtable}/test.c | 0 kepler.c => src/kepler.c | 0 {list => src/list}/meson.build | 0 {list => src/list}/slist.c | 0 {list => src/list}/slist.h | 0 {list => src/list}/test.c | 0 src/meson.build | 33 +++ next.c => src/next.c | 0 {openmp => src/openmp}/meson.build | 0 {openmp => src/openmp}/pi.c | 0 {openmp => src/openmp}/simd.c | 0 {openmp => src/openmp}/taylor.c | 0 {openmp => src/openmp}/threads.c | 0 parrot.c => src/parrot.c | 0 src/pthread/meson.build | 2 + {pthread => src/pthread}/thread-incr.c | 0 sizeof.c => src/sizeof.c | 0 snprintf.c => src/snprintf.c | 0 {sockets => src/sockets}/meson.build | 0 {sockets => src/sockets}/udp-server-win.c | 0 {sockets => src/sockets}/udp-server.c | 0 strtol.c => src/strtol.c | 0 taylor.c => src/taylor.c | 0 src/winter/add.c | 26 ++ src/winter/add2.c | 12 + src/winter/angle.c | 31 +++ src/winter/array/meson.build | 5 + src/winter/array/src/array.c | 311 ++++++++++++++++++++++ src/winter/array/src/array.h | 54 ++++ src/winter/array/src/mem.c | 43 +++ src/winter/array/src/mem.h | 26 ++ src/winter/array/src/meson.build | 3 + src/winter/array/test/average.c | 110 ++++++++ src/winter/array/test/basic.c | 64 +++++ src/winter/array/test/empty.c | 92 +++++++ src/winter/array/test/meson.build | 21 ++ src/winter/binary.c | 60 +++++ src/winter/complex.c | 21 ++ src/winter/einstein.c | 30 +++ src/winter/fgets.c | 17 ++ src/winter/fib.c | 24 ++ src/winter/fixed-sizeof.c | 23 ++ src/winter/gcd.c | 43 +++ src/winter/getchar.c | 27 ++ src/winter/hashtable/hashtable.c | 95 +++++++ src/winter/hashtable/hashtable.h | 13 + src/winter/hashtable/meson.build | 5 + src/winter/hashtable/slist.c | 19 ++ src/winter/hashtable/slist.h | 13 + src/winter/hashtable/test.c | 33 +++ src/winter/kepler.c | 53 ++++ src/winter/lib/meson.build | 1 + src/winter/lib/utils.c | 27 ++ src/winter/lib/utils.h | 7 + src/winter/list/meson.build | 3 + src/winter/list/slist.c | 23 ++ src/winter/list/slist.h | 16 ++ src/winter/list/test.c | 44 +++ {winter => src/winter}/meson.build | 0 {winter => src/winter}/winter.c | 0 world.c => src/world.c | 0 90 files changed, 1433 insertions(+), 39 deletions(-) delete mode 100644 pthread/meson.build rename add.c => src/add.c (100%) rename add2.c => src/add2.c (100%) rename angle.c => src/angle.c (100%) rename {array => src/array}/meson.build (100%) rename {array => src/array}/src/array.c (100%) rename {array => src/array}/src/array.h (100%) rename {array => src/array}/src/mem.c (100%) rename {array => src/array}/src/mem.h (100%) rename {array => src/array}/src/meson.build (100%) rename {array => src/array}/test/average.c (100%) rename {array => src/array}/test/basic.c (100%) rename {array => src/array}/test/empty.c (100%) rename {array => src/array}/test/meson.build (100%) rename binary.c => src/binary.c (100%) rename complex.c => src/complex.c (100%) rename einstein.c => src/einstein.c (100%) rename fgets.c => src/fgets.c (100%) rename fib.c => src/fib.c (100%) rename fixed-sizeof.c => src/fixed-sizeof.c (100%) rename gcd.c => src/gcd.c (100%) rename getchar.c => src/getchar.c (100%) rename {hashtable => src/hashtable}/hashtable.c (100%) rename {hashtable => src/hashtable}/hashtable.h (100%) rename {hashtable => src/hashtable}/meson.build (100%) rename {hashtable => src/hashtable}/slist.c (100%) rename {hashtable => src/hashtable}/slist.h (100%) rename {hashtable => src/hashtable}/test.c (100%) rename kepler.c => src/kepler.c (100%) rename {list => src/list}/meson.build (100%) rename {list => src/list}/slist.c (100%) rename {list => src/list}/slist.h (100%) rename {list => src/list}/test.c (100%) create mode 100644 src/meson.build rename next.c => src/next.c (100%) rename {openmp => src/openmp}/meson.build (100%) rename {openmp => src/openmp}/pi.c (100%) rename {openmp => src/openmp}/simd.c (100%) rename {openmp => src/openmp}/taylor.c (100%) rename {openmp => src/openmp}/threads.c (100%) rename parrot.c => src/parrot.c (100%) create mode 100644 src/pthread/meson.build rename {pthread => src/pthread}/thread-incr.c (100%) rename sizeof.c => src/sizeof.c (100%) rename snprintf.c => src/snprintf.c (100%) rename {sockets => src/sockets}/meson.build (100%) rename {sockets => src/sockets}/udp-server-win.c (100%) rename {sockets => src/sockets}/udp-server.c (100%) rename strtol.c => src/strtol.c (100%) rename taylor.c => src/taylor.c (100%) create mode 100644 src/winter/add.c create mode 100644 src/winter/add2.c create mode 100644 src/winter/angle.c create mode 100644 src/winter/array/meson.build create mode 100644 src/winter/array/src/array.c create mode 100644 src/winter/array/src/array.h create mode 100644 src/winter/array/src/mem.c create mode 100644 src/winter/array/src/mem.h create mode 100644 src/winter/array/src/meson.build create mode 100644 src/winter/array/test/average.c create mode 100644 src/winter/array/test/basic.c create mode 100644 src/winter/array/test/empty.c create mode 100644 src/winter/array/test/meson.build create mode 100644 src/winter/binary.c create mode 100644 src/winter/complex.c create mode 100644 src/winter/einstein.c create mode 100644 src/winter/fgets.c create mode 100644 src/winter/fib.c create mode 100644 src/winter/fixed-sizeof.c create mode 100644 src/winter/gcd.c create mode 100644 src/winter/getchar.c create mode 100644 src/winter/hashtable/hashtable.c create mode 100644 src/winter/hashtable/hashtable.h create mode 100644 src/winter/hashtable/meson.build create mode 100644 src/winter/hashtable/slist.c create mode 100644 src/winter/hashtable/slist.h create mode 100644 src/winter/hashtable/test.c create mode 100644 src/winter/kepler.c create mode 100644 src/winter/lib/meson.build create mode 100644 src/winter/lib/utils.c create mode 100644 src/winter/lib/utils.h create mode 100644 src/winter/list/meson.build create mode 100644 src/winter/list/slist.c create mode 100644 src/winter/list/slist.h create mode 100644 src/winter/list/test.c rename {winter => src/winter}/meson.build (100%) rename {winter => src/winter}/winter.c (100%) rename world.c => src/world.c (100%) diff --git a/lib/meson.build b/lib/meson.build index 95c33f8..2609f83 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -1 +1,2 @@ -lib_utils = library('utils', 'utils.c') +utils_lib = library('utils', 'utils.c') +utils_dep = declare_dependency(link_with: utils_lib, include_directories: '.') diff --git a/meson.build b/meson.build index 9ba5689..fafc281 100644 --- a/meson.build +++ b/meson.build @@ -15,37 +15,4 @@ else endif subdir('lib') - -executable('add', 'add.c') -executable('add2', 'add2.c', link_with: lib_utils, include_directories: 'lib') -executable('angle', 'angle.c', dependencies: libm) -executable('binary', 'binary.c') -executable('complex', 'complex.c', dependencies: libm) -executable('einstein', 'einstein.c') -executable('fib', 'fib.c') -executable('fixed-sizeof', 'fixed-sizeof.c') -executable('fgets', 'fgets.c') -executable('gcd', 'gcd.c') -executable('getchar', 'getchar.c') -executable('kepler', 'kepler.c', dependencies: libm) -executable('next', 'next.c', dependencies: libm) -executable('parrot', 'parrot.c') -executable('sizeof', 'sizeof.c') -executable('snprintf', 'snprintf.c') -executable('strtol', 'strtol.c') -executable('taylor', 'taylor.c') -executable('world', 'world.c') - -subdir('array') -subdir('hashtable') -subdir('list') -subdir('sockets') -subdir('winter') - -if openmp_dep.found() - subdir('openmp') -endif - -if thread_dep.found() - subdir('pthread') -endif +subdir('src') diff --git a/pthread/meson.build b/pthread/meson.build deleted file mode 100644 index 78af5f2..0000000 --- a/pthread/meson.build +++ /dev/null @@ -1,4 +0,0 @@ -executable('thread-incr', 'thread-incr.c', - dependencies: thread_dep, - link_with: lib_utils, - include_directories: '../lib') diff --git a/add.c b/src/add.c similarity index 100% rename from add.c rename to src/add.c diff --git a/add2.c b/src/add2.c similarity index 100% rename from add2.c rename to src/add2.c diff --git a/angle.c b/src/angle.c similarity index 100% rename from angle.c rename to src/angle.c diff --git a/array/meson.build b/src/array/meson.build similarity index 100% rename from array/meson.build rename to src/array/meson.build diff --git a/array/src/array.c b/src/array/src/array.c similarity index 100% rename from array/src/array.c rename to src/array/src/array.c diff --git a/array/src/array.h b/src/array/src/array.h similarity index 100% rename from array/src/array.h rename to src/array/src/array.h diff --git a/array/src/mem.c b/src/array/src/mem.c similarity index 100% rename from array/src/mem.c rename to src/array/src/mem.c diff --git a/array/src/mem.h b/src/array/src/mem.h similarity index 100% rename from array/src/mem.h rename to src/array/src/mem.h diff --git a/array/src/meson.build b/src/array/src/meson.build similarity index 100% rename from array/src/meson.build rename to src/array/src/meson.build diff --git a/array/test/average.c b/src/array/test/average.c similarity index 100% rename from array/test/average.c rename to src/array/test/average.c diff --git a/array/test/basic.c b/src/array/test/basic.c similarity index 100% rename from array/test/basic.c rename to src/array/test/basic.c diff --git a/array/test/empty.c b/src/array/test/empty.c similarity index 100% rename from array/test/empty.c rename to src/array/test/empty.c diff --git a/array/test/meson.build b/src/array/test/meson.build similarity index 100% rename from array/test/meson.build rename to src/array/test/meson.build diff --git a/binary.c b/src/binary.c similarity index 100% rename from binary.c rename to src/binary.c diff --git a/complex.c b/src/complex.c similarity index 100% rename from complex.c rename to src/complex.c diff --git a/einstein.c b/src/einstein.c similarity index 100% rename from einstein.c rename to src/einstein.c diff --git a/fgets.c b/src/fgets.c similarity index 100% rename from fgets.c rename to src/fgets.c diff --git a/fib.c b/src/fib.c similarity index 100% rename from fib.c rename to src/fib.c diff --git a/fixed-sizeof.c b/src/fixed-sizeof.c similarity index 100% rename from fixed-sizeof.c rename to src/fixed-sizeof.c diff --git a/gcd.c b/src/gcd.c similarity index 100% rename from gcd.c rename to src/gcd.c diff --git a/getchar.c b/src/getchar.c similarity index 100% rename from getchar.c rename to src/getchar.c diff --git a/hashtable/hashtable.c b/src/hashtable/hashtable.c similarity index 100% rename from hashtable/hashtable.c rename to src/hashtable/hashtable.c diff --git a/hashtable/hashtable.h b/src/hashtable/hashtable.h similarity index 100% rename from hashtable/hashtable.h rename to src/hashtable/hashtable.h diff --git a/hashtable/meson.build b/src/hashtable/meson.build similarity index 100% rename from hashtable/meson.build rename to src/hashtable/meson.build diff --git a/hashtable/slist.c b/src/hashtable/slist.c similarity index 100% rename from hashtable/slist.c rename to src/hashtable/slist.c diff --git a/hashtable/slist.h b/src/hashtable/slist.h similarity index 100% rename from hashtable/slist.h rename to src/hashtable/slist.h diff --git a/hashtable/test.c b/src/hashtable/test.c similarity index 100% rename from hashtable/test.c rename to src/hashtable/test.c diff --git a/kepler.c b/src/kepler.c similarity index 100% rename from kepler.c rename to src/kepler.c diff --git a/list/meson.build b/src/list/meson.build similarity index 100% rename from list/meson.build rename to src/list/meson.build diff --git a/list/slist.c b/src/list/slist.c similarity index 100% rename from list/slist.c rename to src/list/slist.c diff --git a/list/slist.h b/src/list/slist.h similarity index 100% rename from list/slist.h rename to src/list/slist.h diff --git a/list/test.c b/src/list/test.c similarity index 100% rename from list/test.c rename to src/list/test.c diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 0000000..279be6f --- /dev/null +++ b/src/meson.build @@ -0,0 +1,33 @@ +executable('add', 'add.c') +executable('add2', 'add2.c', dependencies: utils_dep) +executable('angle', 'angle.c', dependencies: libm) +executable('binary', 'binary.c') +executable('complex', 'complex.c', dependencies: libm) +executable('einstein', 'einstein.c') +executable('fib', 'fib.c') +executable('fixed-sizeof', 'fixed-sizeof.c') +executable('fgets', 'fgets.c') +executable('gcd', 'gcd.c') +executable('getchar', 'getchar.c') +executable('kepler', 'kepler.c', dependencies: libm) +executable('next', 'next.c', dependencies: libm) +executable('parrot', 'parrot.c') +executable('sizeof', 'sizeof.c') +executable('snprintf', 'snprintf.c') +executable('strtol', 'strtol.c') +executable('taylor', 'taylor.c') +executable('world', 'world.c') + +subdir('array') +subdir('hashtable') +subdir('list') +subdir('sockets') +subdir('winter') + +if openmp_dep.found() + subdir('openmp') +endif + +if thread_dep.found() + subdir('pthread') +endif diff --git a/next.c b/src/next.c similarity index 100% rename from next.c rename to src/next.c diff --git a/openmp/meson.build b/src/openmp/meson.build similarity index 100% rename from openmp/meson.build rename to src/openmp/meson.build diff --git a/openmp/pi.c b/src/openmp/pi.c similarity index 100% rename from openmp/pi.c rename to src/openmp/pi.c diff --git a/openmp/simd.c b/src/openmp/simd.c similarity index 100% rename from openmp/simd.c rename to src/openmp/simd.c diff --git a/openmp/taylor.c b/src/openmp/taylor.c similarity index 100% rename from openmp/taylor.c rename to src/openmp/taylor.c diff --git a/openmp/threads.c b/src/openmp/threads.c similarity index 100% rename from openmp/threads.c rename to src/openmp/threads.c diff --git a/parrot.c b/src/parrot.c similarity index 100% rename from parrot.c rename to src/parrot.c diff --git a/src/pthread/meson.build b/src/pthread/meson.build new file mode 100644 index 0000000..bc22c6f --- /dev/null +++ b/src/pthread/meson.build @@ -0,0 +1,2 @@ +executable('thread-incr', 'thread-incr.c', + dependencies: [thread_dep, utils_dep]) diff --git a/pthread/thread-incr.c b/src/pthread/thread-incr.c similarity index 100% rename from pthread/thread-incr.c rename to src/pthread/thread-incr.c diff --git a/sizeof.c b/src/sizeof.c similarity index 100% rename from sizeof.c rename to src/sizeof.c diff --git a/snprintf.c b/src/snprintf.c similarity index 100% rename from snprintf.c rename to src/snprintf.c diff --git a/sockets/meson.build b/src/sockets/meson.build similarity index 100% rename from sockets/meson.build rename to src/sockets/meson.build diff --git a/sockets/udp-server-win.c b/src/sockets/udp-server-win.c similarity index 100% rename from sockets/udp-server-win.c rename to src/sockets/udp-server-win.c diff --git a/sockets/udp-server.c b/src/sockets/udp-server.c similarity index 100% rename from sockets/udp-server.c rename to src/sockets/udp-server.c diff --git a/strtol.c b/src/strtol.c similarity index 100% rename from strtol.c rename to src/strtol.c diff --git a/taylor.c b/src/taylor.c similarity index 100% rename from taylor.c rename to src/taylor.c diff --git a/src/winter/add.c b/src/winter/add.c new file mode 100644 index 0000000..3bebb8b --- /dev/null +++ b/src/winter/add.c @@ -0,0 +1,26 @@ +/* Adds up 2 integers, not the most exciting thing in the world */ +#include <stdio.h> + +int +main (int argc, char **argv) +{ + int a, b; + + if (argc != 3) + { + printf ("Please input an integer value: "); + scanf ("%d", &a); + + printf ("Please input an integer value: "); + scanf ("%d", &b); + } + else + { + sscanf (argv[1], "%d", &a); + sscanf (argv[2], "%d", &b); + } + + printf ("%d + %d = %d\n", a, b, a + b); + + return 0; +} diff --git a/src/winter/add2.c b/src/winter/add2.c new file mode 100644 index 0000000..55e1935 --- /dev/null +++ b/src/winter/add2.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +#include "utils.h" + +int +main (int argc, + char **argv) +{ + int *vals = get_ints (argc, argv, 2, "Please input an integer value"); + printf ("%d + %d = %d\n", vals[0], vals[1], vals[0] + vals[1]); + return 0; +} diff --git a/src/winter/angle.c b/src/winter/angle.c new file mode 100644 index 0000000..f06b968 --- /dev/null +++ b/src/winter/angle.c @@ -0,0 +1,31 @@ +#include <stdio.h> +#include <tgmath.h> + +#define DEG_TO_RAD(x) ((x) * M_PI / 180.0) + +int +main (int argc, char **argv) +{ + double angle, angle_rad; + double sin_a, cos_a, tan_a; + + if (argc != 2) + { + printf ("Input the angle: "); + scanf ("%lf", &angle); + } + else + sscanf (argv[1], "%lf", &angle); + + angle_rad = DEG_TO_RAD (angle); + + sin_a = sin (angle_rad); + cos_a = cos (angle_rad); + tan_a = tan (angle_rad); + + printf ("sin (%f) = %f\n", angle, sin_a); + printf ("cos (%f) = %f\n", angle, cos_a); + printf ("tan (%f) = %f\n", angle, tan_a); + + return 0; +} diff --git a/src/winter/array/meson.build b/src/winter/array/meson.build new file mode 100644 index 0000000..d9e53c5 --- /dev/null +++ b/src/winter/array/meson.build @@ -0,0 +1,5 @@ +subdir('src') + +if glib_dep.found() + subdir('test') +endif diff --git a/src/winter/array/src/array.c b/src/winter/array/src/array.c new file mode 100644 index 0000000..ed4c91f --- /dev/null +++ b/src/winter/array/src/array.c @@ -0,0 +1,311 @@ +/* array.c + * + * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include <errno.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "array.h" +#include "mem.h" + +#define MIN_SIZE 2 + +struct _Array +{ + void **data; + size_t length; + size_t capacity; + FreeFunc free_func; + + int ref_count; +}; + +static void +resize_array (Array *array, + size_t required) +{ + size_t size = MIN_SIZE; + + while (size < required) + { + size = (size_t) ceil (size * 1.5); + } + + array->data = check_realloc (array->data, sizeof (void *) * size); + array->capacity = size; +} + +static bool +index_in_array (const Array *array, + size_t index) +{ + if (index < array->length) + return true; + else + { + errno = EINVAL; + return false; + } +} + +static void +maybe_shrink (Array *array) +{ + if (array->length <= (size_t) ceil (array->capacity * 0.3)) + resize_array (array, array->length); +} + +/** + * array_new: + * @func: (nullable): A #FreeFunc which is used to free deleted items + * + * Creates a new #Array. + * + * Returns: The new #Array, free with array_free() + */ +Array * +array_new (FreeFunc func) +{ + Array *array = check_malloc (sizeof (Array)); + + array->data = check_malloc (sizeof (void *) * MIN_SIZE); + array->length = 0; + array->capacity = MIN_SIZE; + array->free_func = func; + array->ref_count = 1; + + return array; +} + +/** + * array_ref: + * @array: The array to increase the reference count of + * + * Increase the reference account of the array. + * + * Returns: The array + */ +Array * +array_ref (Array *array) +{ + array->ref_count++; + + return array; +} + +/** + * array_unref: + * @array: The array to decrease the reference count of + * + * Decrease the reference account of the array. If it becomes 0, it will be + * freed. + */ +void +array_unref (Array *array) +{ + array->ref_count--; + + if (array->ref_count == 0) + { + if (array->free_func) + { + for (size_t i = 0; i < array->length; i++) + array->free_func (array->data[i]); + } + + free (array->data); + free (array); + } +} + +/** + * array_add: + * @array: An array + * @val: The value to add to the array + * + * Adds a value to an array. + */ +void +array_add (Array *array, + const void *val) +{ + if (array->length == array->capacity) + resize_array (array, array->length + 1); + + array->data[array->length] = (void *) val; + array->length += 1; +} + +/** + * array_remove_fast: + * @array: An array + * @index: The index of the item to remove from @array + * + * Removes an item from an array. This function copies the last member of the + * array to the location of the item to be removed, which means that ordering + * is not preserved. + * + * Returns: %true if successful + */ +bool +array_remove_fast (Array *array, + size_t index) +{ + if (!index_in_array (array, index)) + { + perror ("array_remove_fast"); + return false; + } + + if (array->free_func) + array->free_func (array->data[index]); + + void *last = array->data[array->length - 1]; + array->data[index] = last; + + array->length -= 1; + maybe_shrink (array); + + return true; +} + +/** + * array_remove: + * @array: An array + * @index: The index of the item to remove from @array + * + * Removes an item from an array. This function moves the items following the + * removed item down one space. This preserves the ordering of the items, but is + * slower than array_remove_fast(). + * + * Returns: %true if successful + */ +bool +array_remove (Array *array, + size_t index) +{ + if (!index_in_array (array, index)) + { + perror ("array_remove"); + return false; + } + + if (array->free_func) + array->free_func (array->data[index]); + + memmove (array->data + index, + array->data + index + 1, + sizeof (void *) * (array->length - index - 1)); + + array->length -= 1; + maybe_shrink (array); + + return true; +} + +/** + * array_set: + * @array: An array + * @index: The index of the item to set a value for + * @val: The value to set the item to + * + * Sets an element of @array to @val, using the @index. + * + * Returns: %true if successful + */ +bool +array_set (Array *array, + size_t index, + const void *val) +{ + if (!index_in_array (array, index)) + { + perror ("array_set"); + return false; + } + + array->data[index] = (void *) val; + return true; +} + +/** + * array_get: + * @array: An array + * @index: The index of @array to get a value from + * + * Get a value of @array, using @index. + * + * Returns: (transfer none): The value at @index of @array + */ +const void * +array_get (const Array *array, + size_t index) +{ + if (index_in_array (array, index)) + return array->data[index]; + else + { + perror ("array_get"); + return NULL; + } +} + +/** + * array_get_length: + * @array: An array + * + * Gets the number of elements in an array. + * + * Returns: The length of the array + */ +size_t +array_get_length (const Array *array) +{ + return array->length; +} + +/** + * array_get_capacity: + * @array: An array + * + * Get the capacity (proportional to memory usage) of @array. + * + * Returns: The capacity, in items. + */ +size_t +array_get_capacity (const Array *array) +{ + return array->capacity; +} + +/** + * array_sort: + * @array: An array + * @func: A function which returns -1 for less than, 0 for equal, and 1 for more than + * + * Sorts the array + */ +void +array_sort (Array *array, + CompareFunc func) +{ + qsort (*array->data, array->length, sizeof (void *), func); +} diff --git a/src/winter/array/src/array.h b/src/winter/array/src/array.h new file mode 100644 index 0000000..44f0a7b --- /dev/null +++ b/src/winter/array/src/array.h @@ -0,0 +1,54 @@ +/* array.h + * + * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include <stdbool.h> +#include <stddef.h> + +typedef struct _Array Array; + +typedef void (*FreeFunc) (void *element); +typedef double (*ValueFunc) (const void *element); +typedef int (*CompareFunc) (const void *a, const void *b); + +// Fundamental functions +Array * array_new (FreeFunc func); +Array * array_ref (Array *array); +void array_unref (Array *array); + +void array_add (Array *array, + const void *val); +bool array_remove_fast (Array *array, + size_t index); +bool array_remove (Array *array, + size_t index); +bool array_set (Array *array, + size_t index, + const void *val); + +// Getters +const void * array_get (const Array *array, + size_t index); +size_t array_get_length (const Array *array); +size_t array_get_capacity (const Array *array); + +// Misc +void array_sort (Array *array, + CompareFunc func); diff --git a/src/winter/array/src/mem.c b/src/winter/array/src/mem.c new file mode 100644 index 0000000..168c1e6 --- /dev/null +++ b/src/winter/array/src/mem.c @@ -0,0 +1,43 @@ +/* mem.c + * + * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "mem.h" + +#include <stdlib.h> + +void * +check_malloc (size_t size) +{ + void *ptr = malloc (size); + if (!ptr) + exit (EXIT_FAILURE); + + return ptr; +} + +void * +check_realloc (void *ptr, + size_t size) +{ + void *new = realloc (ptr, size); + if (!new) + exit (EXIT_FAILURE); + + return new; +} diff --git a/src/winter/array/src/mem.h b/src/winter/array/src/mem.h new file mode 100644 index 0000000..c73b025 --- /dev/null +++ b/src/winter/array/src/mem.h @@ -0,0 +1,26 @@ +/* mem.h + * + * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include <stddef.h> + +void * check_malloc (size_t size); +void * check_realloc (void *ptr, + size_t size); diff --git a/src/winter/array/src/meson.build b/src/winter/array/src/meson.build new file mode 100644 index 0000000..ad7d9e3 --- /dev/null +++ b/src/winter/array/src/meson.build @@ -0,0 +1,3 @@ +src = files('array.c', 'mem.c') +lib = shared_library('array', src, dependencies: libm) +lib_inc = include_directories('.') diff --git a/src/winter/array/test/average.c b/src/winter/array/test/average.c new file mode 100644 index 0000000..e81bb27 --- /dev/null +++ b/src/winter/array/test/average.c @@ -0,0 +1,110 @@ +/* average.c + * + * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include <stdlib.h> +#include <math.h> + +#include <glib.h> + +#include <array.h> + +static gdouble +get_total (const Array *array) +{ + double total = 0; + size_t length = array_get_length (array); + + for (size_t i = 0; i < length; i++) + total += (double) GPOINTER_TO_INT (array_get (array, i)); + + return total; +} + +static gdouble +get_average (const Array *array) +{ + return get_total (array) / array_get_length (array); +} + +static Array * +create_array (size_t length) +{ + Array *array = array_new (NULL); + + for (size_t i = 1; i < length; i++) + array_add (array, GINT_TO_POINTER (i)); + + return array; +} + +static void +expected_values (Array *array, + double *total, + double *average) +{ + size_t n = array_get_length (array); + gdouble sum = (n * (n+1)) / 2; + + if (total) + *total = sum; + + if (average) + *average = sum / n; +} + +static void +test_total (void) +{ + Array *array = create_array (100); + + gdouble expected_total; + gdouble total = get_total (array); + expected_values (array, &expected_total, NULL); + + g_assert_cmpfloat (total, ==, expected_total); + + array_unref (array); +} + +static void +test_average (void) +{ + Array *array = create_array (100); + + gdouble expected_average; + gdouble average = get_average (array); + expected_values (array, NULL, &expected_average); + + g_assert_cmpfloat (average, ==, expected_average); + + array_unref (array); +} + +int +main (int argc, + char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/array/total", test_total); + g_test_add_func ("/array/average", test_average); + + return g_test_run (); +} + diff --git a/src/winter/array/test/basic.c b/src/winter/array/test/basic.c new file mode 100644 index 0000000..aede24f --- /dev/null +++ b/src/winter/array/test/basic.c @@ -0,0 +1,64 @@ +/* basic.c + * + * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include <glib.h> + +#include "array.h" + +#define LENGTH 100 + +static Array * +create_array (size_t length) +{ + Array *array = array_new (g_free); + + for (size_t i = 0; i < length; i++) + { + gchar *str = g_strdup_printf ("String #%zu", i); + array_add (array, str); + } + + return array; +} + +static void +test_get (void) +{ + Array *array = create_array (LENGTH); + + for (size_t i = 0; i < LENGTH; i++) + { + const gchar *element = (const gchar *) array_get (array, i); + g_autofree gchar *compare = g_strdup_printf ("String #%zu", i); + g_assert_cmpstr (element, ==, compare); + } + + array_unref (array); +} + +int +main (int argc, + char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/array/get", test_get); + + return g_test_run (); +} diff --git a/src/winter/array/test/empty.c b/src/winter/array/test/empty.c new file mode 100644 index 0000000..d4d590a --- /dev/null +++ b/src/winter/array/test/empty.c @@ -0,0 +1,92 @@ +/* empty.c + * + * Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include <glib.h> + +#include "array.h" + +static void +test_length (void) +{ + Array *array = array_new (NULL); + + size_t length = array_get_length (array); + g_assert_cmpint (length, ==, 0); + + array_unref (array); +} + +static void +test_remove_fast (void) +{ + Array *array = array_new (NULL); + + gboolean success = array_remove_fast (array, 1); + g_assert_false (success); + + array_unref (array); +} + +static void +test_remove (void) +{ + Array *array = array_new (NULL); + + gboolean success = array_remove (array, 1); + g_assert_false (success); + + array_unref (array); +} + +static void +test_get (void) +{ + Array *array = array_new (NULL); + + const void *element = array_get (array, 1); + g_assert_null (element); + + array_unref (array); +} + +static void +test_set (void) +{ + Array *array = array_new (NULL); + + gboolean success = array_set (array, 1, NULL); + g_assert_false (success); + + array_unref (array); +} + +int +main (int argc, + char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/array/length", test_length); + g_test_add_func ("/array/remove_fast", test_remove_fast); + g_test_add_func ("/array/remove", test_remove); + g_test_add_func ("/array/get", test_get); + g_test_add_func ("/array/set", test_set); + + return g_test_run (); +} diff --git a/src/winter/array/test/meson.build b/src/winter/array/test/meson.build new file mode 100644 index 0000000..c9fd3b9 --- /dev/null +++ b/src/winter/array/test/meson.build @@ -0,0 +1,21 @@ +avg = executable('average', + 'average.c', + link_with: lib, + include_directories: lib_inc, + dependencies: [glib_dep]) + +basic = executable('basic', + 'basic.c', + link_with: lib, + include_directories: lib_inc, + dependencies: [glib_dep]) + +empty = executable('empty', + 'empty.c', + link_with: lib, + include_directories: lib_inc, + dependencies: [glib_dep]) + +test('average', avg) +test('basic', basic) +test('empty', empty) diff --git a/src/winter/binary.c b/src/winter/binary.c new file mode 100644 index 0000000..d3d9862 --- /dev/null +++ b/src/winter/binary.c @@ -0,0 +1,60 @@ +#include <stdio.h> + +static int list[] = { 8, 11, 17, 22, 29, 38, 51, 58, 65, 67, 74, 81, 87, 90, 96, 97 }; + +#define N_ELEMENTS(arr) (sizeof(arr) / sizeof(arr[0])) + +static void +binary_search (int key) +{ + int passes = 0; + int *low = &list[0]; + int *high = &list[N_ELEMENTS (list) - 1]; + int *mid; + + printf ("key = %d\n", key); + + while (low <= high) + { + passes += 1; + printf ("\n----------Pass #%d----------\n", passes); + + mid = low + (high - low) / 2; + printf ("low, mid, high = %d, %d, %d\n", *low, *mid, *high); + + if (key < *mid) + { + high = mid - 1; + printf ("key < mid\n"); + } + else if (key > *mid) + { + low = mid + 1; + printf ("key > mid\n"); + } + else + { + printf ("key = %d\n", *mid); + printf ("%d passes\n", passes); + break; + } + } +} + +int +main (int argc, char **argv) +{ + int key; + + if (argc != 2) + { + printf ("What is the number to search for? "); + scanf ("%d", &key); + } + else + sscanf (argv[1], "%d", &key); + + binary_search (key); + + return 0; +} diff --git a/src/winter/complex.c b/src/winter/complex.c new file mode 100644 index 0000000..8582c07 --- /dev/null +++ b/src/winter/complex.c @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <tgmath.h> + +int +main (void) +{ + double complex z1 = I*I; + printf("j * j = %.1f + j%.1f\n", creal (z1), cimag (z1)); + + double complex z2 = pow (I, 2); + printf("j^2 = %.1f + j%.1f\n", creal (z2), cimag (z2)); + + double complex z3 = exp (I * M_PI); + printf("exp(j * pi) = %.1f + j%.1f\n", creal (z3), cimag (z3)); + + double complex z4 = 1 + 2*I; + double complex z5 = 1 - 2*I; + printf ("(1+j2)(1-j2) = %.1f + j%.1f\n", creal (z4*z5), cimag (z4*z5)); + + return 0; +} diff --git a/src/winter/einstein.c b/src/winter/einstein.c new file mode 100644 index 0000000..6f4dad4 --- /dev/null +++ b/src/winter/einstein.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <stdlib.h> + +#define C 299792458 +#define C2 (C)*(C) + +int +main (int argc, char **argv) +{ + double m; + + if (argc != 2) + { + printf ("Input the mass in kg: "); + scanf ("%lf", &m); + } + else + { + if (sscanf (argv[1], "%lf", &m) != 1) + { + fprintf (stderr, "Couldn't parse number\n"); + return EXIT_FAILURE; + } + } + + double e = m * C2; + printf ("E = %#.3G J\n", e); + + return 0; +} diff --git a/src/winter/fgets.c b/src/winter/fgets.c new file mode 100644 index 0000000..a60cab3 --- /dev/null +++ b/src/winter/fgets.c @@ -0,0 +1,17 @@ +#include <stdio.h> + +int +main (void) +{ + char array[80]; + + printf ("Enter string for fgets()\n"); + fgets (array, sizeof(array), stdin); + printf ("fgets returned: %s\n", array); + + printf ("Enter string for scanf()\n"); + scanf ("%80[^\n]", array); + printf ("scanf() returned: %s\n", array); + + return 0; +} diff --git a/src/winter/fib.c b/src/winter/fib.c new file mode 100644 index 0000000..61b078e --- /dev/null +++ b/src/winter/fib.c @@ -0,0 +1,24 @@ +#include <inttypes.h> +#include <stdio.h> + +#define N 45 + +static uint32_t +fib (int n) +{ + if (n < 2) + return 1; + + return fib (n - 2) + fib (n - 1); +} + +int +main (void) +{ + for (int i = 0; i < N; i++) + { + printf ("fib(%2d) = %" PRIu32 "\n", i, fib (i)); + } + + return 0; +} diff --git a/src/winter/fixed-sizeof.c b/src/winter/fixed-sizeof.c new file mode 100644 index 0000000..a0d0072 --- /dev/null +++ b/src/winter/fixed-sizeof.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stdint.h> + +int +main (void) +{ + printf ("int8_t is %zu bytes\n", sizeof (int8_t)); + printf ("int16_t is %zu bytes\n", sizeof (int16_t)); + printf ("int32_t is %zu bytes\n", sizeof (int32_t)); + printf ("int64_t is %zu bytes\n", sizeof (int64_t)); + + printf ("int_fast8_t is %zu bytes\n", sizeof (int_fast8_t)); + printf ("int_fast16_t is %zu bytes\n", sizeof (int_fast16_t)); + printf ("int_fast32_t is %zu bytes\n", sizeof (int_fast32_t)); + printf ("int_fast64_t is %zu bytes\n", sizeof (int_fast64_t)); + + printf ("int_least8_t is %zu bytes\n", sizeof (int_least8_t)); + printf ("int_least16_t is %zu bytes\n", sizeof (int_least16_t)); + printf ("int_least32_t is %zu bytes\n", sizeof (int_least32_t)); + printf ("int_least64_t is %zu bytes\n", sizeof (int_least64_t)); + + return 0; +} diff --git a/src/winter/gcd.c b/src/winter/gcd.c new file mode 100644 index 0000000..c30bed3 --- /dev/null +++ b/src/winter/gcd.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> + +static int +gcd (int a, int b) +{ + static int pass = 0; + ++pass; + + printf ("\n---Pass #%d---\n", pass); + printf ("a = %d, b = %d\n", a, b); + + if (b == 0) + return a; + else + return gcd (b, a % b); +} + +int +main (int argc, char **argv) +{ + int a, b, solution; + + if (argc != 3) + { + printf ("Input the first number "); + scanf ("%d", &a); + printf ("Input the second number "); + scanf ("%d", &b); + } + else + { + a = strtol (argv[1], NULL, 0); + b = strtol (argv[2], NULL, 0); + } + + solution = gcd (a, b); + + printf ("The GCD of (%d, %d) is %d\n", a, b, solution); + printf ("For example, it could be (%d, %d)\n", a / solution, b / solution); + + return 0; +} diff --git a/src/winter/getchar.c b/src/winter/getchar.c new file mode 100644 index 0000000..86e49d9 --- /dev/null +++ b/src/winter/getchar.c @@ -0,0 +1,27 @@ +#include <inttypes.h> +#include <stdint.h> +#include <stdio.h> + +#define LEN_DIGITS 18 // Safe digits of INT64_MAX + +int +main (void) +{ + int64_t value = 0; + + printf ("Put the number in: "); + + for (int i = 0; i < LEN_DIGITS; i++) + { + int ch; + if ((ch = getchar ()) == '\n') + break; + + int digit = ch - '0'; + value = 10 * value + digit; + } + + printf ("Value is %" PRId64 "\n", value); + + return 0; +} diff --git a/src/winter/hashtable/hashtable.c b/src/winter/hashtable/hashtable.c new file mode 100644 index 0000000..f529754 --- /dev/null +++ b/src/winter/hashtable/hashtable.c @@ -0,0 +1,95 @@ +#include "hashtable.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/** + * HashTable: + * + * The #HashTable struct is an opaque data structure to represent a + * Hash Table. It should only be accessed via the following functions. + */ +struct _HashTable +{ + SList **array; + size_t size; +}; + +/** + * hash_table_new: + * @size: Number of buckets to use. + * + * Creates a new #HashTable. + */ +HashTable * +hash_table_new (size_t size) +{ + HashTable *new = malloc (sizeof (HashTable)); + new->array = calloc (size, sizeof (SList *)); + new->size = size; + + return new; +} + +/** + * strhash: + * @string: the string + * + * Converts a string into a hash value. + * + * Returns: the hash + */ +static unsigned +strhash (const char *string) +{ + unsigned h = 5381; + char c; + + while ((c = *string++)) + h = (h << 5) + h + c; + + return h; +} + +/** + * hash_table_insert: + * @table: a #HashTable. + * @key: the key to insert. + * @value: the value to associate with the key. + * + * Inserts a new key and value into a #HashTable. + * + * Duplicates are ignored, and @key and @value are copied. + */ +void +hash_table_insert (HashTable *table, + const char *key, + const char *value) +{ + unsigned index = strhash (key) % table->size; + + SList *list = slist_prepend (table->array[index], key, value); + table->array[index] = list; +} + +/** + * hash_table_print_all: + * @table: a #HashTable. + * + * Prints out the contents of the #HashTable, one key-value pair per line, + * in the format "key=value". + */ +void +hash_table_print_all (HashTable *table) +{ + for (size_t i = 0; i < table->size; i++) + { + SList *bucket = table->array[i]; + if (bucket == NULL) + continue; + + for (SList *list = bucket; list; list = list->next) + printf ("%s:%s\n", list->key, list->value); + } +} diff --git a/src/winter/hashtable/hashtable.h b/src/winter/hashtable/hashtable.h new file mode 100644 index 0000000..03d02f4 --- /dev/null +++ b/src/winter/hashtable/hashtable.h @@ -0,0 +1,13 @@ +#pragma once + +#include <stddef.h> + +#include "slist.h" + +typedef struct _HashTable HashTable; + +HashTable * hash_table_new (size_t size); +void hash_table_insert (HashTable *table, + const char *key, + const char *value); +void hash_table_print_all (HashTable *table); diff --git a/src/winter/hashtable/meson.build b/src/winter/hashtable/meson.build new file mode 100644 index 0000000..69d9385 --- /dev/null +++ b/src/winter/hashtable/meson.build @@ -0,0 +1,5 @@ +sources = files('hashtable.c', + 'slist.c', + 'test.c') + +executable('hash', sources) diff --git a/src/winter/hashtable/slist.c b/src/winter/hashtable/slist.c new file mode 100644 index 0000000..6b4bd0b --- /dev/null +++ b/src/winter/hashtable/slist.c @@ -0,0 +1,19 @@ +#include "slist.h" + +#include <stdlib.h> +#include <string.h> + +SList * +slist_prepend (SList *list, + const char *key, + const char *value) +{ + SList *new; + + new = malloc (sizeof (SList)); + new->key = strdup (key); + new->value = strdup (value); + new->next = list; + + return new; +} diff --git a/src/winter/hashtable/slist.h b/src/winter/hashtable/slist.h new file mode 100644 index 0000000..f1bc6a3 --- /dev/null +++ b/src/winter/hashtable/slist.h @@ -0,0 +1,13 @@ +#pragma once + +typedef struct _SList SList; +struct _SList +{ + char *key; + char *value; + SList *next; +}; + +SList * slist_prepend (SList *list, + const char *key, + const char *value); diff --git a/src/winter/hashtable/test.c b/src/winter/hashtable/test.c new file mode 100644 index 0000000..0ac02e7 --- /dev/null +++ b/src/winter/hashtable/test.c @@ -0,0 +1,33 @@ +#include "hashtable.h" + +#include <stdio.h> + +static void +add_data (HashTable *table) +{ + char key[20]; + char value[20]; + + printf ("Input the key "); + scanf ("%s", key); + printf ("Input the value "); + scanf ("%s", value); + + hash_table_insert (table, key, value); +} + +int +main (void) +{ + HashTable *table; + + table = hash_table_new (16); + + while (1) + { + add_data (table); + hash_table_print_all (table); + } + + return 0; +} diff --git a/src/winter/kepler.c b/src/winter/kepler.c new file mode 100644 index 0000000..7da4f95 --- /dev/null +++ b/src/winter/kepler.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <stdlib.h> +#include <tgmath.h> + +#define ACC 1e-9 + +static double +solve_keplers_equation (double m, double ecc) +{ + static int i = 1; + double e = m; // Initial guess + + while (1) + { + printf ("---Iteration #%d---\n", i); + + double delta = e - ecc * sin (e) - m; + printf ("E = %f, delta = %g\n", e, delta); + + if (fabs (delta) <= ACC) + return e; + + e -= delta / (1 - ecc * cos (e)); + i++; + } +} + +int +main (int argc, char **argv) +{ + double m; + double ecc; + + if (argc != 3) + { + printf ("Input M (degrees) "); + scanf ("%lf", &m); + printf ("Input e "); + scanf ("%lf", &ecc); + } + else + { + sscanf (argv[1], "%lf", &m); + sscanf (argv[2], "%lf", &ecc); + } + + m *= (M_PI / 180.0); // Convert m into radians + double e = solve_keplers_equation (m, ecc); + + printf ("\nResult is E = %.17g rad\n", e); + + return 0; +} diff --git a/src/winter/lib/meson.build b/src/winter/lib/meson.build new file mode 100644 index 0000000..95c33f8 --- /dev/null +++ b/src/winter/lib/meson.build @@ -0,0 +1 @@ +lib_utils = library('utils', 'utils.c') diff --git a/src/winter/lib/utils.c b/src/winter/lib/utils.c new file mode 100644 index 0000000..4ca6fa4 --- /dev/null +++ b/src/winter/lib/utils.c @@ -0,0 +1,27 @@ +#include <stdio.h> +#include <stdlib.h> + +int * +get_ints (int argc, + char **argv, + int n, + const char *prompt) +{ + int *ret = malloc (sizeof (int) * n); + + if (argc != (n + 1)) + { + for (int i = 0; i < n; i++) + { + printf ("%s: ", prompt); + scanf ("%d", &ret[i]); + } + } + else + { + for (int i = 0; i < n; i++) + sscanf (argv[i + 1], "%d", &ret[i]); + } + + return ret; +} diff --git a/src/winter/lib/utils.h b/src/winter/lib/utils.h new file mode 100644 index 0000000..4a21c4b --- /dev/null +++ b/src/winter/lib/utils.h @@ -0,0 +1,7 @@ +#pragma once + +int * +get_ints (int argc, + char **argv, + int n, + const char *prompt); diff --git a/src/winter/list/meson.build b/src/winter/list/meson.build new file mode 100644 index 0000000..2ae653a --- /dev/null +++ b/src/winter/list/meson.build @@ -0,0 +1,3 @@ +sources = files('slist.c', 'test.c') + +executable('slist', sources) diff --git a/src/winter/list/slist.c b/src/winter/list/slist.c new file mode 100644 index 0000000..2bcbef4 --- /dev/null +++ b/src/winter/list/slist.c @@ -0,0 +1,23 @@ +#include "slist.h" + +#include <stdlib.h> +#include <string.h> + +SList * +slist_prepend (SList *list, + void *data) +{ + SList *new = malloc (sizeof (SList)); + new->data = data; + new->next = list; + + return new; +} + +void +slist_free_all (SList *list, + FreeFunc func) +{ + for (SList *l = list; l; l = l->next) + func (l); +} diff --git a/src/winter/list/slist.h b/src/winter/list/slist.h new file mode 100644 index 0000000..ae55f13 --- /dev/null +++ b/src/winter/list/slist.h @@ -0,0 +1,16 @@ +#pragma once + +typedef void (*FreeFunc) (void *value); + +typedef struct _SList SList; +struct _SList +{ + void *data; + SList *next; +}; + +SList * slist_prepend (SList *list, + void *data); + +void slist_free_all (SList *list, + FreeFunc func); diff --git a/src/winter/list/test.c b/src/winter/list/test.c new file mode 100644 index 0000000..98aad53 --- /dev/null +++ b/src/winter/list/test.c @@ -0,0 +1,44 @@ +#include "slist.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static SList * +add_data (SList *list) +{ + char buffer[128]; + + printf ("Input the data "); + scanf ("%s", buffer); + + char *str = strdup (buffer); + return slist_prepend (list, str); +} + +static void +print_data (SList *list) +{ + SList *l; + + for (l = list; l != NULL; l = l->next) + { + printf ("%s\n", (char *) l->data); + } +} + +int +main (void) +{ + SList *list = NULL; + + while (1) + { + list = add_data (list); + print_data (list); + } + + slist_free_all (list, free); + + return 0; +} diff --git a/winter/meson.build b/src/winter/meson.build similarity index 100% rename from winter/meson.build rename to src/winter/meson.build diff --git a/winter/winter.c b/src/winter/winter.c similarity index 100% rename from winter/winter.c rename to src/winter/winter.c diff --git a/world.c b/src/world.c similarity index 100% rename from world.c rename to src/world.c -- GitLab