summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-06-01 18:31:41 +0200
committerTyge Løvset <[email protected]>2022-06-01 18:31:41 +0200
commit254e61e2774d6b8fa6064e61c054014bfc460d91 (patch)
tree1d350b9370a00cbc87f57ec484620d766788f8f7
parentde5b9db42473801e83e93b35968c7cfc4b782e3a (diff)
downloadSTC-modified-254e61e2774d6b8fa6064e61c054014bfc460d91.tar.gz
STC-modified-254e61e2774d6b8fa6064e61c054014bfc460d91.zip
Reverted to default static linkage of non-templated symbols (cstr, csview, ..), as it gives smaller executables and is convenient for small applications. (no need to define i_implement when functions is used in one translaton unit only).
-rw-r--r--README.md10
-rw-r--r--docs/carc_api.md1
-rw-r--r--examples/astar.c1
-rw-r--r--examples/birthday.c1
-rw-r--r--examples/bits.c1
-rw-r--r--examples/bits2.c1
-rw-r--r--examples/books.c1
-rw-r--r--examples/box.c1
-rw-r--r--examples/city.c1
-rw-r--r--examples/complex.c1
-rw-r--r--examples/convert.c1
-rw-r--r--examples/csmap_erase.c1
-rw-r--r--examples/csmap_find.c1
-rw-r--r--examples/csmap_insert.c1
-rw-r--r--examples/cstr_match.c1
-rw-r--r--examples/demos.c1
-rw-r--r--examples/hashmap.c1
-rw-r--r--examples/inits.c1
-rw-r--r--examples/mapmap.c1
-rw-r--r--examples/mmap.c1
-rw-r--r--examples/multimap.c1
-rw-r--r--examples/music_arc.c1
-rw-r--r--examples/new_arr.c1
-rw-r--r--examples/new_deq.c1
-rw-r--r--examples/new_list.c1
-rw-r--r--examples/new_map.c1
-rw-r--r--examples/new_queue.c1
-rw-r--r--examples/new_smap.c1
-rw-r--r--examples/new_sptr.c1
-rw-r--r--examples/new_vec.c1
-rw-r--r--examples/person_arc.c1
-rw-r--r--examples/phonebook.c1
-rw-r--r--examples/prime.c1
-rw-r--r--examples/priority.c1
-rw-r--r--examples/queue.c1
-rw-r--r--examples/random.c1
-rw-r--r--examples/rawptr_elements.c1
-rw-r--r--examples/read.c1
-rw-r--r--examples/regex1.c1
-rw-r--r--examples/regex2.c1
-rw-r--r--examples/regex_match.c1
-rw-r--r--examples/replace.c1
-rw-r--r--examples/splitstr.c2
-rw-r--r--examples/sso_map.c1
-rw-r--r--examples/utf8replace_c.c1
-rw-r--r--examples/vikings.c1
-rw-r--r--examples/words.c1
-rw-r--r--include/stc/cbits.h5
-rw-r--r--include/stc/crandom.h3
-rw-r--r--include/stc/cstr.h3
-rw-r--r--include/stc/csview.h3
51 files changed, 14 insertions, 57 deletions
diff --git a/README.md b/README.md
index f567e3b9..6d2f867b 100644
--- a/README.md
+++ b/README.md
@@ -252,8 +252,16 @@ If containers are used across several translation units with common instantiated
recommended to build as a "library" with external linking to minimize executable size. To enable this,
specify `-DSTC_HEADER` as compiler option in your build environment. Next, place all the instantiations
of the containers used inside a single C-source file as in the example below, and `#define STC_IMPLEMENT` at top.
-You may also cherry-pick external linking mode on individual containers by `#define i_header` and
+You may also cherry-pick shared linking mode on individual containers by `#define i_header` and
`#define i_implement`, or force static symbols by `#define i_static` before container includes.
+
+As a special case, there may be non-templated functions in templated containers that should be implemented only
+once if needed. Currently, for **clist**, define `i_extern` before including `clist.h` for sorting functionality
+(global `STC_EXTERN` may alternatively be defined).
+
+Conveniently, `src\libstc.c` implements non-templated functions as shared symbols for **cstr**, **csview**,
+**cbits** and **crandom**. When building in shared mode (-DSTC_HEADER), you may include this file in your project,
+or define your own as descibed above.
```c
// stc_libs.c
#define STC_IMPLEMENT
diff --git a/docs/carc_api.md b/docs/carc_api.md
index aef3d867..2ab61791 100644
--- a/docs/carc_api.md
+++ b/docs/carc_api.md
@@ -67,7 +67,6 @@ bool carc_X_value_eq(const i_val* x, const i_val* y); // carc_X_value_c
```c
// Create a stack and a list of shared pointers to maps,
// and demonstrate sharing and cloning of maps.
-#define i_implement
#include <stc/cstr.h>
#define i_type Map
diff --git a/examples/astar.c b/examples/astar.c
index db2d128c..4b86df9e 100644
--- a/examples/astar.c
+++ b/examples/astar.c
@@ -4,7 +4,6 @@
// This is a reimplementation of the CTL example to STC:
// https://github.com/glouw/ctl/blob/master/examples/astar.c
// https://www.redblobgames.com/pathfinding/a-star/introduction.html
-#define i_implement
#include <stc/cstr.h>
#include <stdio.h>
diff --git a/examples/birthday.c b/examples/birthday.c
index 50cd60dc..e09d84f1 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -1,7 +1,6 @@
#include <math.h>
#include <stdio.h>
#include <time.h>
-#define i_implement
#include <stc/crandom.h>
#define i_tag ic
diff --git a/examples/bits.c b/examples/bits.c
index 71fe1ee0..8cce573e 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -1,5 +1,4 @@
#include <stdio.h>
-#define i_implement
#include <stc/cbits.h>
int main()
diff --git a/examples/bits2.c b/examples/bits2.c
index 4b16ad76..bb3324e3 100644
--- a/examples/bits2.c
+++ b/examples/bits2.c
@@ -3,7 +3,6 @@
#define i_type Bits
#define i_len 80 // enable fixed bitset on the stack
-#define i_implement
#include <stc/cbits.h>
int main()
diff --git a/examples/books.c b/examples/books.c
index 5677ff3a..b2015208 100644
--- a/examples/books.c
+++ b/examples/books.c
@@ -1,5 +1,4 @@
// https://doc.rust-lang.org/std/collections/struct.HashMap.html
-#define i_implement
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
diff --git a/examples/box.c b/examples/box.c
index 7a984a21..70b0ab08 100644
--- a/examples/box.c
+++ b/examples/box.c
@@ -1,5 +1,4 @@
/* cbox: heap allocated boxed type */
-#define i_implement
#include <stc/cstr.h>
typedef struct { cstr name, last; } Person;
diff --git a/examples/city.c b/examples/city.c
index c359ebdf..d70edbf7 100644
--- a/examples/city.c
+++ b/examples/city.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
typedef struct {
diff --git a/examples/complex.c b/examples/complex.c
index 8db85bbd..79a34a6a 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
void check_drop(float* v) {printf("destroy %g\n", *v);}
diff --git a/examples/convert.c b/examples/convert.c
index 75299309..c1380c94 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#define i_key_str
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index b1dbaada..09d28d78 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -1,6 +1,5 @@
// map_erase.c
// From C++ example: https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-16
-#define i_implement
#include <stc/cstr.h>
#include <stdio.h>
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index 7fac5bc2..c3047110 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -1,6 +1,5 @@
// This implements the c++ std::map::find example at:
// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
-#define i_implement
#include <stc/cstr.h>
#define i_key int
diff --git a/examples/csmap_insert.c b/examples/csmap_insert.c
index 3d235877..3a739cd8 100644
--- a/examples/csmap_insert.c
+++ b/examples/csmap_insert.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
// This implements the std::map insert c++ example at:
diff --git a/examples/cstr_match.c b/examples/cstr_match.c
index 69c12ef2..8130b401 100644
--- a/examples/cstr_match.c
+++ b/examples/cstr_match.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/csview.h>
#include <stdio.h>
diff --git a/examples/demos.c b/examples/demos.c
index 780a4bff..a6b23bc4 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
void stringdemo1()
diff --git a/examples/hashmap.c b/examples/hashmap.c
index ab980045..ac4e29bc 100644
--- a/examples/hashmap.c
+++ b/examples/hashmap.c
@@ -1,5 +1,4 @@
// https://doc.rust-lang.org/rust-by-example/std/hash.html
-#define i_implement
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
diff --git a/examples/inits.c b/examples/inits.c
index 1013da36..d8114536 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#define i_key int
diff --git a/examples/mapmap.c b/examples/mapmap.c
index ab48ecc6..2ea8fa61 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -1,5 +1,4 @@
// unordered_map<string, unordered_map<string, string>>:
-#define i_implement
#include <stc/cstr.h>
#define i_type People
#define i_key_str
diff --git a/examples/mmap.c b/examples/mmap.c
index 5e3eda00..5ca2f92a 100644
--- a/examples/mmap.c
+++ b/examples/mmap.c
@@ -2,7 +2,6 @@
// https://en.cppreference.com/w/cpp/container/multimap/insert
// Multimap entries
-#define i_implement
#include <stc/cstr.h>
#define i_val_str
#define i_extern // define _clist_mergesort() once
diff --git a/examples/multimap.c b/examples/multimap.c
index a2d06a06..f580cf7e 100644
--- a/examples/multimap.c
+++ b/examples/multimap.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
// Olympics multimap example
diff --git a/examples/music_arc.c b/examples/music_arc.c
index 13075741..e1e715a1 100644
--- a/examples/music_arc.c
+++ b/examples/music_arc.c
@@ -1,6 +1,5 @@
// shared_ptr-examples.cpp
// based on https://docs.microsoft.com/en-us/cpp/cpp/how-to-create-and-use-shared-ptr-instances?view=msvc-160
-#define i_implement
#include <stc/cstr.h>
struct Song
diff --git a/examples/new_arr.c b/examples/new_arr.c
index d046c835..8ccc62fa 100644
--- a/examples/new_arr.c
+++ b/examples/new_arr.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#define i_val int
diff --git a/examples/new_deq.c b/examples/new_deq.c
index 5fe2403a..5f7c357f 100644
--- a/examples/new_deq.c
+++ b/examples/new_deq.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/new_list.c b/examples/new_list.c
index f45a7f30..9bbbd8ce 100644
--- a/examples/new_list.c
+++ b/examples/new_list.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
forward_clist(clist_i32, int);
diff --git a/examples/new_map.c b/examples/new_map.c
index 78c1a44d..b0752d53 100644
--- a/examples/new_map.c
+++ b/examples/new_map.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/new_queue.c b/examples/new_queue.c
index f0d8120f..f3051871 100644
--- a/examples/new_queue.c
+++ b/examples/new_queue.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/crandom.h>
#include <stc/forward.h>
#include <stdio.h>
diff --git a/examples/new_smap.c b/examples/new_smap.c
index 141934d3..368775dc 100644
--- a/examples/new_smap.c
+++ b/examples/new_smap.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/new_sptr.c b/examples/new_sptr.c
index e2d4da85..2af7317e 100644
--- a/examples/new_sptr.c
+++ b/examples/new_sptr.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
struct Person { cstr name, last; } typedef Person;
diff --git a/examples/new_vec.c b/examples/new_vec.c
index 73e4987f..9ae2ccf1 100644
--- a/examples/new_vec.c
+++ b/examples/new_vec.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/forward.h>
diff --git a/examples/person_arc.c b/examples/person_arc.c
index 685d1b3c..d0af690e 100644
--- a/examples/person_arc.c
+++ b/examples/person_arc.c
@@ -1,5 +1,4 @@
/* cbox: heap allocated boxed type */
-#define i_implement
#include <stc/cstr.h>
typedef struct { cstr name, last; } Person;
diff --git a/examples/phonebook.c b/examples/phonebook.c
index 2368480c..eebc8008 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -21,7 +21,6 @@
// Program to emulates the phone book.
-#define i_implement
#include <stc/cstr.h>
#define i_key_str
diff --git a/examples/prime.c b/examples/prime.c
index f235f845..01a6800b 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <math.h>
#include <time.h>
-#define i_implement
#include <stc/cbits.h>
cbits sieveOfEratosthenes(size_t n)
diff --git a/examples/priority.c b/examples/priority.c
index f4c45d76..7ba9e59b 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <time.h>
-#define i_implement
#include <stc/crandom.h>
#define i_val int64_t
diff --git a/examples/queue.c b/examples/queue.c
index 57e24fe6..cfc8d988 100644
--- a/examples/queue.c
+++ b/examples/queue.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/crandom.h>
#include <stdio.h>
diff --git a/examples/random.c b/examples/random.c
index fc7745e5..30751a6c 100644
--- a/examples/random.c
+++ b/examples/random.c
@@ -1,6 +1,5 @@
#include <stdio.h>
#include <time.h>
-#define i_implement
#include <stc/crandom.h>
int main()
diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c
index cfeb459d..c3e3188d 100644
--- a/examples/rawptr_elements.c
+++ b/examples/rawptr_elements.c
@@ -13,7 +13,6 @@ struct { double x, y; } typedef Point;
#define i_tag pnt
#include <stc/cset.h>
-#define i_implement
#include <stc/cstr.h>
// Map of int64 pointers: Define i_valraw as int64_t for easy emplace calls!
typedef int64_t inttype;
diff --git a/examples/read.c b/examples/read.c
index 74375a4f..60698789 100644
--- a/examples/read.c
+++ b/examples/read.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#define i_val_str
#include <stc/cvec.h>
diff --git a/examples/regex1.c b/examples/regex1.c
index 919bfd66..f2623f7a 100644
--- a/examples/regex1.c
+++ b/examples/regex1.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
diff --git a/examples/regex2.c b/examples/regex2.c
index c98c2e21..41266b65 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
#include <stc/csview.h>
diff --git a/examples/regex_match.c b/examples/regex_match.c
index 4bb8bee5..e60fd519 100644
--- a/examples/regex_match.c
+++ b/examples/regex_match.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/cregex.h>
diff --git a/examples/replace.c b/examples/replace.c
index 6084ede0..a70fcc8e 100644
--- a/examples/replace.c
+++ b/examples/replace.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
int main ()
diff --git a/examples/splitstr.c b/examples/splitstr.c
index a155830c..942d4eec 100644
--- a/examples/splitstr.c
+++ b/examples/splitstr.c
@@ -1,6 +1,4 @@
-#define i_implement
#include <stc/cstr.h>
-#define i_implement
#include <stc/csview.h>
#define i_val_str
#include <stc/cvec.h>
diff --git a/examples/sso_map.c b/examples/sso_map.c
index 823398e1..a32a9a3d 100644
--- a/examples/sso_map.c
+++ b/examples/sso_map.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#define i_key_str
#define i_val_str
diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c
index 733245e5..89d5375f 100644
--- a/examples/utf8replace_c.c
+++ b/examples/utf8replace_c.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
#include <stc/csview.h>
diff --git a/examples/vikings.c b/examples/vikings.c
index c63b7ffc..2cef1991 100644
--- a/examples/vikings.c
+++ b/examples/vikings.c
@@ -1,4 +1,3 @@
-#define i_implement
#include <stc/cstr.h>
typedef struct Viking {
diff --git a/examples/words.c b/examples/words.c
index ed800ae7..8a86ba7f 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -1,5 +1,4 @@
#include <math.h>
-#define i_implement
#include <stc/cstr.h>
#define i_val_str
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index bf5d5406..f7ce2368 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -51,7 +51,6 @@ int main() {
*/
#ifndef CBITS_H_INCLUDED
-#define i_header
#include "ccommon.h"
#include <stdlib.h>
#include <string.h>
@@ -246,8 +245,8 @@ STC_INLINE bool _i_memb(_disjoint)(const i_type* self, const i_type* other) {
return _cbits_disjoint(self->data64, other->data64, _i_memb(_size)(self));
}
-
-#if defined(i_implement)
+/* -------------------------- IMPLEMENTATION ------------------------- */
+#if defined(i_implement) || defined(i_extern)
#if !defined i_len
STC_DEF cbits* cbits_copy(cbits* self, const cbits* other) {
diff --git a/include/stc/crandom.h b/include/stc/crandom.h
index ea26eba3..1b94fd66 100644
--- a/include/stc/crandom.h
+++ b/include/stc/crandom.h
@@ -20,7 +20,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#define i_header
#include "ccommon.h"
#ifndef CRANDOM_H_INCLUDED
@@ -119,7 +118,7 @@ STC_INLINE stc64_normalf_t stc64_normalf_init(double mean, double stddev)
{ return stc64_normalf_new(mean, stddev); }
/* -------------------------- IMPLEMENTATION ------------------------- */
-#if defined(i_implement)
+#if defined(i_implement) || defined(i_extern)
/* Global random() */
static stc64_t stc64_global = {{
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index b33faf43..1bf58a18 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -30,7 +30,6 @@
#ifndef CSTR_H_INCLUDED
#define CSTR_H_INCLUDED
-#define i_header
#include "ccommon.h"
#include "forward.h"
#include "utf8.h"
@@ -355,7 +354,7 @@ STC_INLINE uint64_t cstr_hash(const cstr *self) {
}
/* -------------------------- IMPLEMENTATION ------------------------- */
-#if defined(i_implement)
+#if defined(i_implement) || defined(i_extern)
STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) {
cstr_buf r = cstr_buffer(self);
diff --git a/include/stc/csview.h b/include/stc/csview.h
index 90e1b10b..e55e547b 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -23,7 +23,6 @@
#ifndef CSVIEW_H_INCLUDED
#define CSVIEW_H_INCLUDED
-#define i_header
#include "ccommon.h"
#include "forward.h"
#include "utf8.h"
@@ -175,7 +174,7 @@ STC_INLINE uint64_t csview_hash(const csview *self)
{ return c_fasthash(self->str, self->size); }
/* -------------------------- IMPLEMENTATION ------------------------- */
-#if defined(i_implement)
+#if defined(i_implement) || defined(i_extern)
STC_DEF csview
csview_substr_ex(csview sv, intptr_t pos, size_t n) {