summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-12 22:47:55 +0100
committerTyge Løvset <[email protected]>2023-02-12 23:20:18 +0100
commit7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d (patch)
tree681d1894d917bc2fe244375298ea40f736c18e18 /include/stc
parent9904a7ea36f9e4f45d7e41e409ed23ad22821e8a (diff)
downloadSTC-modified-7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d.tar.gz
STC-modified-7dc6fddc079f4f572c8fb7c0ffd5a27e03291a2d.zip
Fairly large update before release 4.1, cleaning up docs and some minor additions.
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/algo/csort.h36
-rw-r--r--include/stc/algo/filter.h6
-rw-r--r--include/stc/coption.h6
3 files changed, 17 insertions, 31 deletions
diff --git a/include/stc/algo/csort.h b/include/stc/algo/csort.h
index c8c41257..c452064f 100644
--- a/include/stc/algo/csort.h
+++ b/include/stc/algo/csort.h
@@ -20,8 +20,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
-#include <stc/priv/template.h>
#include <stc/ccommon.h>
+#include <stc/priv/template.h>
/* Generic Quicksort in C, performs as fast as c++ std::sort().
template params:
@@ -31,37 +31,21 @@ template params:
// test:
#include <stdio.h>
-#include <time.h>
-#include <stdlib.h>
#define i_val int
#include <stc/algo/csort.h>
-#include <stc/crandom.h>
-#ifdef __cplusplus
-#include <algorithm>
-#endif
-void testsort(csort_int_value *a, size_t size, const char *desc) {
- clock_t t = clock();
- csort_int(a, size);
- t = clock() - t;
+int main() {
+ int arr[] = {23, 321, 5434, 25, 245, 1, 654, 33, 543, 21};
+
+ csort_int(arr, c_arraylen(arr));
- printf("%s: %zu elements sorted in %.3fms\n",
- desc, size, t*1000.0/CLOCKS_PER_SEC);
+ for (int i = 0; i < c_arraylen(arr); i++)
+ printf(" %d", arr[i]);
+ puts("");
}
+*/
-int main() {
- size_t i, size = 10000000;
- csort_int_value *a = (csort_int_value*)malloc(sizeof(*a) * size);
- if (a != NULL) {
- for (i = 0; i < size; i++)
- a[i] = crandom() & (1U << 28) - 1;
- testsort(a, size, "random");
- for (i = 0; i < 20; i++) printf(" %d", a[i]);
- puts("");
- free(a);
- }
-}*/
-typedef i_val c_PASTE(c_PASTE(csort_, i_tag), _value);
+typedef i_val c_PASTE(c_CONCAT(csort_, i_tag), _value);
static inline void c_PASTE(cisort_, i_tag)(i_val arr[], intptr_t lo, intptr_t hi) {
for (intptr_t j = lo, i = lo + 1; i <= hi; j = i, ++i) {
diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h
index b2b59fa6..8357cb4d 100644
--- a/include/stc/algo/filter.h
+++ b/include/stc/algo/filter.h
@@ -57,6 +57,8 @@ int main()
#define c_flt_skip(i, n) (++(i).s1[(i).s1top++] > (n))
#define c_flt_skipwhile(i, pred) ((i).s2[(i).s2top++] |= !(pred))
#define c_flt_takewhile(i, pred) !c_flt_skipwhile(i, pred)
+#define c_flt_last(i) (i).s1[(i).s1top-1]
+#define c_flt_lastwhile(i) (i).s2[(i).s2top-1]
#define c_forfilter(...) c_MACRO_OVERLOAD(c_forfilter, __VA_ARGS__)
@@ -68,10 +70,10 @@ int main()
#define c_forfilter_B(i, C, start, filter) \
for (struct {C##_iter it; C##_value *ref; \
- uint32_t s1[c_NFILTERS], index, count; \
+ uint32_t s1[c_NFILTERS], index; \
bool s2[c_NFILTERS]; uint8_t s1top, s2top;} \
i = {.it=start, .ref=i.it.ref}; i.it.ref \
; C##_next(&i.it), i.ref = i.it.ref, ++i.index, i.s1top=0, i.s2top=0) \
- if (!((filter) && ++i.count)) ; else
+ if (!(filter)) ; else
#endif
diff --git a/include/stc/coption.h b/include/stc/coption.h
index 69d0796f..65dae444 100644
--- a/include/stc/coption.h
+++ b/include/stc/coption.h
@@ -107,7 +107,7 @@ static void coption_permute_(char *argv[], int j, int n) {
*/
static int coption_get(coption *opt, int argc, char *argv[],
const char *shortopts, const coption_long *longopts) {
- int optc = -1, i0, j, posixly_correct = (shortopts[0] == '+');
+ int optc = -1, i0, j, posixly_correct = (shortopts && shortopts[0] == '+');
if (!posixly_correct) {
while (opt->_i < argc && (argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0'))
++opt->_i, ++opt->_nargs;
@@ -149,12 +149,12 @@ static int coption_get(coption *opt, int argc, char *argv[],
}
}
}
- } else { /* a short option */
+ } else if (shortopts) { /* a short option */
const char *p;
if (opt->_pos == 0) opt->_pos = 1;
optc = opt->opt = argv[opt->_i][opt->_pos++];
opt->_optstr[1] = optc, opt->optstr = opt->_optstr;
- p = strchr((char *) shortopts, optc);
+ p = strchr(shortopts, optc);
if (p == 0) {
optc = '?'; /* unknown option */
} else if (p[1] == ':') {