Compile time ordered vectors

I would like to have support for ordering content of a constant array at compile time.

I would like to have optimized lookup function for such an ordered constant array (binary search, hash index, etc, – whatever fits the purpose) to check if a variable has a match in the ordered constant array. The code generator could inline the lookup function if so was desired.

I guess you can say that I am looking for something like the “in” keyword for sets, but extended to work with any constant array (string, array of enumerable, array of number, or array of other constants such as class type references).

A specification syntax could look something like:
   = {ordered {ascending | descending}} Vector

Implicit methods associated with the type:
  Vector.Contains(const element: T):Boolean;
  Vector.ContainsAny(const array of T):Boolean;
  Vector.ContainsAll(const array of T):Boolean;
  Vector.Include(const element: T);
  Vector.Exclude(const element: T);

Example declaration:
type
  TMyType = ordered ascending Vector;
const
  InvalidChars : TMyType = [‘\’, ‘:’, ‘.’, ‘(‘, ‘)’]; // I don’t want to worry about order here

Example of use:
  if InvalidChars.Contains(SomeCharacter) then …

Today, I can create singleton objects that are ordered as part of the initialization code, but it becomes a lot of micro management code which isn’t optimized for call performance, instead of easy to use code like “element in set” style code.

4 thoughts on “Compile time ordered vectors

  1. Your syntax is for sorting, but your use cases are all about set operations. Set operations do not require sorting – sets do require some manner of precomputing, but not display sequence ordering. Its best not to impose an implementation on the interface.If you want presorted data, change your use case to show why this would be useful and how you would deal with locale and localization issues.If you want set operations on arrays or vectors, lose the “ordered” syntax and just extend the existing “set of” syntax.

    Like

  2. Aye, it is a poor example. I'll provide a better one. This actually was an old draft post, and it appear to me that I haven't quite completed my train of thought here. My need at the time was related to creating jump tables for handling data streams, partly statically defined at compile time, but revised at run time.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.