Home /Modules/collections/SLArray.sleela

SLArray.sleela

lib/collections/SLArray.sleela · 181 line(s) · sleela

<< lib/collections/SLArray.sleela
   SLeeLa Standard Library Definition.
   Definition: A concrete, fixed-capacity, bounds-safe String array wrapper
   built directly on the VM-managed native array primitive. Unlike SLList,
   the logical length equals the capacity: every slot is addressable from
   construction and defaults to null. This first implementation deliberately
   avoids Generics and mirrors the storage discipline of SLList. >>
#sleela 2.5
material;
< Unified SLeeLa standard-library front-end object. >
< Family: collections. Native VM/OS work crosses an explicit bridge. >
class SLArray {
  << ---- Descriptive metadata (version 1.11 nested-friendly accessors) ----
     Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject real origin where publicly known. >>
  String metaVersion = "1.1.0";
  int metaId = 152;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  String metaDate = "2026-11-04";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-09-28";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  << Backing storage is the VM-managed native String array. size is the fixed
     logical capacity; every index 0..size-1 is addressable and begins null. >>
  String[] values = new String[0];
  int size = 0;

  << Allocate (or re-allocate) backing storage of exactly n slots, all null.
     A negative request is clamped to zero. Call this once after new SLArray()
     before using get/set, or again to resize (contents are not preserved). >>
  void allocate(int n) {
    int want = n;
    if (want < 0) { want = 0; }
    values = new String[want];
    size = want;
  }

  < Number of addressable slots. >
  int length() { return size; }

  < True when there are no slots. >
  boolean empty() { return size == 0; }

  < True when index addresses a real slot. >
  boolean inBounds(int index) {
    return index >= 0 && index < size;
  }

  << Read the slot at index; returns null for an out-of-range index rather
     than trapping, matching the forgiving contract used across collections. >>
  String get(int index) {
    if (index < 0 || index >= size) { return null; }
    return arrayGet(values, index);
  }

  < Write the slot at index; returns false when the index is out of range. >
  boolean set(int index, String value) {
    if (index < 0 || index >= size) { return false; }
    arraySet(values, index, value);
    return true;
  }

  < Set every slot to value. >
  void fill(String value) {
    int i = 0;
    while (i < size) {
      arraySet(values, i, value);
      i = i + 1;
    }
  }

  < Set every slot back to null. >
  void clear() {
    int i = 0;
    while (i < size) {
      arraySet(values, i, null);
      i = i + 1;
    }
  }

  << First index whose value equals the target, or -1 when absent.
     null is matched by passing null. >>
  int indexOf(String value) {
    int i = 0;
    while (i < size) {
      String current = arrayGet(values, i);
      if (current == null) {
        if (value == null) { return i; }
      } else {
        if (current == value) { return i; }
      }
      i = i + 1;
    }
    return -1;
  }

  < True when value is present at any slot. >
  boolean contains(String value) {
    return indexOf(value) >= 0;
  }

  < Reverse the contents of the array in place. >
  void reverse() {
    int i = 0;
    int j = size - 1;
    while (i < j) {
      String a = arrayGet(values, i);
      String b = arrayGet(values, j);
      arraySet(values, i, b);
      arraySet(values, j, a);
      i = i + 1;
      j = j - 1;
    }
  }

  << Copy length slots starting at srcPos in this array into dest starting at
     destPos. Out-of-range requests are clamped so the call is always safe;
     returns the number of slots actually copied. >>
  int copyTo(SLArray dest, int srcPos, int destPos, int length) {
    if (dest == null) { return 0; }
    int copied = 0;
    int s = srcPos;
    int d = destPos;
    int remaining = length;
    while (remaining > 0) {
      if (s < 0 || s >= size) { return copied; }
      if (d < 0 || d >= dest.length()) { return copied; }
      dest.set(d, arrayGet(values, s));
      s = s + 1;
      d = d + 1;
      remaining = remaining - 1;
      copied = copied + 1;
    }
    return copied;
  }

  << Produce an SLList holding the same values in the same order. The list is
     an independent copy; later edits to either side do not affect the other. >>
  SLList toList() {
    SLList out = new SLList();
    int i = 0;
    while (i < size) {
      out.add(arrayGet(values, i));
      i = i + 1;
    }
    return out;
  }
}