#! @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, 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.view() { made.append(each); } return made; } #! Lends a view of the indices. #! @param this The list. #! @returns The view, which refers into the list. pub def ids::view(&this): span { return span::::over(this.data(), this._len as u64); } #! Lends a view of the indices that writes them where they are. #! @param this The list. #! @returns The view, which refers into the list. pub def ids::slice(&mut this): slice { val data := this._cap == 0 ? (&mut this._own) as *mut u32 : this._dat; return slice::::over(data, 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::cursor = span::cursor; #! Lends a cursor that reads the indices. #! @param this The list. #! @returns The cursor, which refers into the list. pub def ids::iter(&this): span::cursor { return this.view().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.view()); 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; }