🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2150

Re: Iterators over slices and views and ranges analogs in C

#2150
From: Ross Finlayson
Date: Thu, 03 Dec 2026 20:04
323 lines
13894 bytes
On 12/03/2026 11:01 AM, Ross Finlayson wrote:
> On 12/03/2026 10:27 AM, Ross Finlayson wrote:
>> On 12/03/2026 10:00 AM, Ross Finlayson wrote:
>>> Hello, good day, been trying to figure out an account of generics in C,
>>> that are specialized to type according to _Generic breaking out function
>>> overloads according to type, and for example breaking out
>>> sizes of arrays to "underloads", which uses a macro to ## concatenate
>>> for example after # stringifying identifiers, and making the
>>> controlling-expression of the generic then as with regards to varioously
>>> declared pointers of the types with the sizes matching to
>>> functions overloads/underloads with given lengths or otherwise
>>> parameters like enums, then with __auto_type and using pointers to
>>> fixed-size arrays so that array-decay-to-pointer preserves the type
>>> under the point for function resolution, then about how to figure out a
>>> way to make for generic sorts of containers, yet mostly about an adapter
>>> to "iterator" and with regards "views/ranges", so then that usually
>>> account of do/while and while/do and for in looping-constructs has
>>> something to do when data is considered to be of particular types
>>> not-necessarily contiguous their memory (storage according to C) as
>>> contiguous and as about the notion of memory as super-scalar or
>>> unbounded vis-a-vis the randomly accessible, secondarily.
>>>
>>>
>>>
>>> Of course, one may suggest to simply use a higher-level language
>>> where these sorts concepts as they are ("interfaces" usually in
>>> sth. like Java, "concepts" for example in C++ or as for template-types,
>>> with regards typename and the like, or templates in C++ about
>>> specializing the generics with the combined yet distinguished
>>> language and idiom of both C++ and Java, primarily C++ for its
>>> derivation from C and primarily Java for its own account of innovations
>>> later folded into C++, like for accounts of CharSequence vis-a-vis
>>> views/ranges and the like, yet here just pointing out that C++'s
>>> terms are C++'s and Java's are Java's and C's are C's, and other
>>> languages like Rust or Python just stick out as making things
>>> different to be different and not compatible like "enum" in Rust
>>> or whatever's "Pythonic" these days, that Rust and Python are
>>> considered second-cousins while C -> C++ -> Java is considered
>>> more of a line. (The language not necessarily the runtime.)
>>>
>>>
>>>
>>> /* pseudo-code:  "overloading" */
>>>
>>> f_t _Generic( controlling_expression,
>>>    t1_t: f_t1(),
>>>    t2_t: f_t2(),
>>>    default: static_assert  /* omitting default is also static_assert */
>>> )
>>>
>>>
>>> /* pseudo-code: "underloading" */
>>>
>>> t1_t t1;
>>> t2_t t2;
>>>
>>>
>>> f_t _Generic( controlling_expression ## _t,
>>>    t1_t:  t(1),
>>>    t2_t:  t(2)
>>> )
>>>
>>>
>>>
>>> So, then what's sort of being figured is about how to go from array
>>> types, to variant, then while yet at compile-time, to dispatch over
>>> the introduced types, to result generics and their specialization,
>>> while resulting writing algorithms only to the "underloads", which
>>> for example C++ just generates many, many function-bodies, the idea
>>> being about how to generate 1 function-body "agnostic of type, yet,
>>> specialized to type", since the types are very simple and of only
>>> a few given types (the built-ins then tuples of fixed sizes).
>>>
>>>
>>>
>>> Then the account of iterators basically is about begin(), end(), next(),
>>> or for example since enumerations/vectors in Java like
>>> for next()/hasNext(), noticeably an opposite sort of account,
>>> then about what forever (rather, whatever) makes for being
>>> Iterable (interface in Java, concept in C++) about iterators and
>>> iterables, has that many, many times here have been written adapters
>>> to accessors to result generic access then about how to make that
>>> so for C in a nice sort of way, then also to result that a direct,
>>> natural reading surfaces the same code in C++ and for example
>>> idiomatically in C, C++, and Java, about the interfaces that
>>> the C libraries can provide, where the boundaries are according to C,
>>> since the process & thread they're according to the C runtime where
>>> it's hosted, usual accounts of code and loading & linking and so on,
>>> thus for the perceived requirement of iterators in C, according
>>> to functional signatures and a simple convention of name-spacing
>>> by function names, in C.
>>>
>>>
>>> Anyways I'd be curious if someone would point to something for
>>> example "oh forever since C++ was invented there was a an idiomatic
>>> C approach to iterators then later other standard sorts collections
>>> & containers, some national laboratory uses it for all the things
>>> and it's the same since forever", I'd be interested in that.
>>> Otherwise I'm looking at how to make one, figuring then
>>> it will be stable and standard and well-defined with the guarantees
>>> and so on, about many and various approaches basically to
>>> un-bounded, un-structured data as text, according to bounded structures,
>>> and iterators.
>>>
>>>
>>>
>>> Thanks for reading, it's invited your opinion.
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>> It's like when Josuttis says "... iterators are the key, glue, code
>> (to standard collections & containers, in C++) ...", it's like
>> "oh rly, Iterables and Single-Abstract-Methods the functional also
>> make a lot of sense in Java". ("Back to Basics: Iterators.")
>>
>> Then it's clear that without templates in C that's lots of duplicated
>> code and explosion of the namespace, yet, with the account of,
>> "underloading" now involved, can also reduce less, which can result
>> smaller code thus for more code re-use in the code-cache.
>>
>>
>> "As this is generic code, it works for all containers,
>> all data structures, and all the element types, ...,
>> you can use it for strings, ...."
>>
>> "... using the iterators as glue code, this works with
>> all abstractions, ..., you will not find the base class
>> for iterator, ..., everything that behaves like an iterator
>> is an iterator, ..., arrays or pointers, ...."
>>
>> Here the point is that the backing of the "array" or iterator
>> is a bunch of slices, then to result that functions are to
>> result making passes over iterators, for example whether or
>> not an iterator has a "remove()" element, here not as
>> it's a view not a mutator, then about the combinatorial
>> explosion of types to contain that with just these "buffers"
>> and "names" types of fixed-length and natural alignments.
>>
>>
>>
>>
>
>
> Sort of an, "iterview", ....
>
> viter: view-iter
>
> witer: write-iter (item-wise, ...)
> diter: dest-iter (vector-wise, ...)
>
> Maybe just "viter" and "witer",
> lining up with "const t* const" and "t* const",
> that those are "viter<t>" and "witer<t>".
>
> Then for something like
>
> viter -> view-iter
> witer -> write-iter
> xiter -> transform, product?
> yiter -> output?
> ziter -> vector destination?
>
>
> Then of course an iterator is just some state and some function
> pointers to usually make operator-overloaders what looks like
>
> while ( *p < len_p && *q < len_q) {
>    *q++ = *p++;
> }
>
>
> typedef void (*incr_fn)() iter_incr_fn_t;
> typedef void (*accr_fn)(size_t stride) iter_accr_fn_t;
>
> typedef p(*p_fn)(viter_p_t* v) p_fn_t;
>
> typedef struct {
>    void* p;
>    size_t size_p;
>
>    size_t len_p;
>    size_t off_p;
>
>    iter_incr_fn_t iter_incr_fn;
>    iter_accr_fn_t iter_accr_fn;
>
>    p_fn_t get_fn;
> } viter_p_t;
>
> typedef void (*fn_p)(witer_p_t* v) fn_p_t;
>
> typedef struct {
>    void* p;
>    size_t size_p;
>
>    size_t len_p;
>    size_t off_p;
>
>    iter_incr_fn_t iter_incr_fn;
>    iter_accr_fn_t iter_accr_fn;
>
>    p_fn_t get_fn;
>    fn_p_t set_fn;
> } witer_p_t;
>
>
>
> So, then "viter" and "witer" are working terms, then that
> for the account of vectorization that a "tuple-iterator"
> is suggested, a "titer",
>
> titer: tuple-iterator
> uiter: no, ..., perhaps a sort of tuple-interator "unsigned-iterator"
>
> Here the idea that tuples are of types yet unsigned's are of bits,
> then that in the vectorization account also, some vector operations
> work just fine on the vector-wide-scalar-word as bits, vis-a-vis,
> packed types as tuples of a type with regular stride, hmm...
>
>
> Hmm...
>
>
>


The loop-body above is agreeably not-well-formed or makes
a very unusual account that *p = p - p_0, whether p_len is
"SIZE_MAX".


Then about whether the iter (viter, witer, titer, uiter,
then for example about tivter & twiter or uviter & uwiter,
those being aspects of sorts or attributes or about
capabilities for things like types), about whether iter
has "state" or just that those are all functions, is figured
for example whether all the functions also accept a pointer
to the iter, or about separating the iter_state from the iter_change
the values from the functions, ..., when usually it's the idea
that there's a cursor on the iterator and it's stateful,
then that accesses are independent, usually vis-a-vis
accumulators and analytic functions and statistics & summaries
and these other sorts of accounts of iterator-passes or table-scans.


About C++, one may aver then to directly wrap the viter/witer
account in C++ classes, i.e., it's direct, makes for "iter"
and so on.

I got a lot (of inspiration and knowledge) from java.util.stream(s)
and java.util.functional, being quite familiar with all those,
vis-a-vis lambdas & transformers and the like, about for example
the characteristics of streams in Java, when for example it's
rather suprising the standard library doesn't provide one for
"arrays" that has all the "characteristics" of streams,
I wrote one called "trout", "trout" are fast in "streams",
for stuff like "exec::par" or whatever it is these days.


Then it's a usual idea that these "iters" of course have
naturally automatic implementations over arrays/pointers.


Then the point here or the question is about how to make
those as "natural" and "automatic" (or, "simple"), above
what basically are lists or "iterators-redux", about
stacking-views, when for example it's usual and common
to stack views on views on views, then for the algorithms
to be in effect on the non-contiguous, like what are
cache-lines and memory-pages already according to the
usual idea, while keeping it generic in C yet typed in C.


So, iters then suggest other usual accounts of data-structures,
basically the array or the list as is usual, and hiding those
their concepts/interfaces according to "contracts & characteristics"
beneath to implement them above, chicken-and-egg and catch-22
type things, ....


Then, the tuple- and unsigned-iters with regards to vectorizing,
of course these are usual about things like AMD & ARM (not
excluding MIPS/RISC-V), about the usual ISA's the op-codes
and the type-specifiers like "word" basically in Intel
and each of the data-types that are about same in both
AMD & ARM vector/packed operations about unsigneds/tuples,
figuring that the C compiler given what's compatible
or actually written in the instrinsics those the actual
things. Then of course that also involves things like
the 64-bit calling-conventions and that already they
make scratch some of the vector registers after spillage
instead of the stack, for example where here there's
that floating-point is not used for pretty much anything
and it's mostly entirely an integer account, yet though
that single and double floats are simple and common
and with sizeof and 4 and 8, vis-a-vis the 16-byte
tandem vector units on all the architectures, and
instructions about blocks of those and banks of those.
This is that mostly the arguments to these types functions
would be few (i.e. src/dst or dst/src if you'd rather
and a function pointer tuple -> tuple or unsigned -> unsigned).




Java and C++ really have a lot in common,
all the way up the libraries not just because
they're both "C-style" languages.









Technical Details
Message-ID: <jeucnZ8OQfeuVoz2nZ2dnZfqn_GdnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-3.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <-kGdnSzB4bWEM4z2nZ2dnZfqn_WdnZ2d@giganews.com> <y92cnSmcTMcBKYz2nZ2dnZfqn_SdnZ2d@giganews.com> <VEadnd4VkZjpIYz2nZ2dnZfqnPednZ2d@giganews.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-3.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-3.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 03 Dec 2026 20:04:03 +0000
Subject: Re: Iterators over slices and views and ranges analogs in C
Newsgroups: comp.lang.c
References: <-kGdnSzB4bWEM4z2nZ2dnZfqn_WdnZ2d@giganews.com>
 <y92cnSmcTMcBKYz2nZ2dnZfqn_SdnZ2d@giganews.com>
 <VEadnd4VkZjpIYz2nZ2dnZfqnPednZ2d@giganews.com>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Thu, 3 Dec 2026 12:04:22 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <VEadnd4VkZjpIYz2nZ2dnZfqnPednZ2d@giganews.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <jeucnZ8OQfeuVoz2nZ2dnZfqn_GdnZ2d@giganews.com>
Lines: 323
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-KgiZIlD9/f1Zn19GaAaYr5kV7U0bYIkXZYk5WLLZ0Ddn2O4QBXCIUd0tU4FY/83vN5Cdm+mcPS4GaWZ!/vZuGm+S0f2VXgDd7rxh8RuguoPSKtrCm3YTQLWjKauXnDVzt//0OaUDS5s/7nCtOKXBN6qt4xD/
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40