#! @file ids.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains `ids`, a short list of indices, as a block's parameters, the edges #! into it, and the arguments an edge gives. Up to four are held in the list #! itself, so most lists take no memory of their own, and more are moved to #! memory of the list's own. module sakura::compiler; #! A list of indices, holding up to four itself and more in memory of its own. pub struct ids : owning , indexable , assignable , spannable , sliceable , iterable { var _len: u32; #! How many it holds. var _cap: u32; #! How many its memory holds, or 0 while it holds them itself. var _own: u32[4]; #! Those it holds itself. var _dat: *mut u32; #! Its memory, once it has any. } #! How many indices the list holds. #! @param this The list. #! @returns How many. pub val ids::length: u64 { get => this._len as u64; } #! Where the indices begin, in the list or in its memory. #! @param this The list. #! @returns The first index. pub def ids::data(&this): *u32 { return this._cap == 0 ? (&this._own) as *u32 : this._dat; } #! Makes a list holding the same indices as another, sharing no memory with it. #! @param other The list. #! @returns The copy. pub def ids::copy_of(other: &ids): ids { var made := ids {}; for &each in other.span { made.append(each); } return made; } #! Lends a view of some of the indices of a list, as `spannable` asks. #! @param other The list. #! @param start The place of the first index viewed. #! @param count How many are viewed, or `~0u` for every one after the start. #! @returns The view, which refers into the list. pub def ids::span_of(other: &ids, start: u64 = 0, count: u64 = ~0u): span { val whole := span::::over(other.data(), other._len as u64); return count == ~0u ? whole[start:] : whole[start:count]; } #! Lends a view of some of the indices of a list that writes them where they #! are, as `sliceable` asks. #! @param other The list, which can be written. #! @param start The place of the first index viewed. #! @param count How many are viewed, or `~0u` for every one after the start. #! @returns The view, which refers into the list. pub def ids::slice_of(other: &mut ids, start: u64 = 0, count: u64 = ~0u): slice { val data := other._cap == 0 ? (&mut other._own) as *mut u32 : other._dat; val whole := slice::::over(data, other._len as u64); return count == ~0u ? whole[start:] : whole[start:count]; } #! A view of the indices. #! @param this The list. #! @returns The view, which refers into the list. pub val ids::span: span { get => span::::over(this.data(), this._len as u64); } #! A view of the indices that writes them where they are, read of a list that #! can be written. #! @param this The list. #! @returns The view, which refers into the list. pub val ids::slice: slice { get mut => slice::::over(this._cap == 0 ? (&mut this._own) as *mut u32 : this._dat, this._len as u64); } #! Reads the index at a place in the list. #! @param this The list. #! @param index The place, below `length`, which stops the program otherwise. #! @returns The index. pub def ids::at(&this, index: u64): &u32 { return &span::::over(this.data(), this._len as u64)[index]; } #! Writes the index at a place in the list. #! @param this The list. #! @param index The place, below `length`, which stops the program otherwise. #! @returns A reference that writes it. pub def ids::at(&mut this, index: u64): &mut u32 { var whole := this.slice; return &mut whole[index]; } #! What `iter(&this)` lends, which reads the indices. pub type ids::iterator = span::iterator; #! Lends an iterator that reads the indices. #! @param this The list. #! @returns The iterator, which refers into the list. pub def ids::iter(&this): span::iterator { return this.span.iter(); } #! An iterator that reads the indices, as `iter` lends. #! @param this The list. #! @returns The iterator, which refers into the list. pub val ids::iterator: span::iterator { get => this.iter(); } #! Adds an index after the last. #! @param this The list. #! @param value The index. pub def ids::append(&mut this, value: u32): void { if this._cap == 0 && this._len < 4 { this._own[this._len as u64] = value; this._len++; return; } if this._len == this._cap || this._cap == 0 { this.spill(this._cap == 0 ? 8 : this._cap * 2); } this._dat[this._len as u64] = value; this._len++; } #! Removes every index, keeping the memory the list holds them in. #! @param this The list. pub def ids::clear(&mut this): void { this._len = 0; } #! A vector holding the same indices, which changing the list leaves as it is. #! @param this The list. #! @param alloc The optional allocator the vector takes its memory from. #! @returns The vector. pub def ids::copied(&this, alloc: maybe<&mut allocator>): vector { var made := vector::::new(this.length, alloc); made.extend(this.span); return made; } #! Whether two lists hold the same indices in the same order. #! @param this The list. #! @param other The list to compare it with. #! @returns Whether they do. pub def ids::same(&this, other: &ids): bool { return false if this._len != other._len; val mine := this.data(); val theirs := other.data(); for i in 0..this._len as u64 { return false if mine[i] != theirs[i]; } return true; } #! Gives back the memory the list holds, leaving it empty. #! @param this The list. pub def ids::close(&mut this): void { if this._cap != 0 { raw_release(this._dat as *mut u8); } this._dat = null; this._cap = 0; this._len = 0; } # Moves the indices to memory of the list's own, with room for a number of them. def ids::spill(&mut this, size: u32): void { if this._cap != 0 { this._dat = raw_resize(this._dat as *mut u8, size as u64 * 4) as *mut u32; this._cap = size; return; } val made := raw_allocate(size as u64 * 4) as *mut u32; for i in 0..this._len as u64 { made[i] = this._own[i]; } this._dat = made; this._cap = size; }