diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-24 15:23:20 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-24 15:23:20 -0700 |
| commit | f49b184a093ab5f653ac36ccc4939c45515a2f4b (patch) | |
| tree | 36b7b0b688a6b03ceb1412fc45f79f32e7540002 /chapter28b/src/main/smp/sys/ary.smp | |
| parent | a216a23bbd4d6e05a40d86ec8e76c038e945f3ba (diff) | |
| download | Simple-f49b184a093ab5f653ac36ccc4939c45515a2f4b.tar.gz Simple-f49b184a093ab5f653ac36ccc4939c45515a2f4b.zip | |
Move combined chapter 25 baseline to chapter 28b
Diffstat (limited to 'chapter28b/src/main/smp/sys/ary.smp')
| -rw-r--r-- | chapter28b/src/main/smp/sys/ary.smp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/chapter28b/src/main/smp/sys/ary.smp b/chapter28b/src/main/smp/sys/ary.smp new file mode 100644 index 00000000..8312e94c --- /dev/null +++ b/chapter28b/src/main/smp/sys/ary.smp @@ -0,0 +1,24 @@ + +// Array utilities + +// Copy byte array contents. Copies the shorter of the buffers and len +val copy = { u8[] dst, int doff, u8[~] src, int soff, int len -> + if( len > dst# - doff ) len = dst# - doff; + if( len > src# - soff ) len = src# - soff; + for( int i=0; i<len; i++ ) + dst[i+doff] = src[i+soff]; + return dst; +}; + +// Compare lexigraphically array contents. Compares up to shorter array or +// len. Returns <0 or >0 for first non-matching byte. Returns <0 or >0 for +// the larger array after that, or 0 if both arrays are fully equal. +val cmp = { u8[~] src1, u32 off1, u8[~] src2, u32 off2, u32 len -> + u32 len1 = len > src1# - off1 ? src1# - off1 : len; + u32 len2 = len > src2# - off2 ? src2# - off2 : len; + u32 max = len1 > len2 ? len2 : len1; + for( int i=0; i<max; i++ ) + if( src1[off1+i] != src2[off2+i] ) + return src1[off1+i] - src2[off2+i]; // Bytes determine compare + return len1 - len2; // Length determines compare +}; |
