mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
"Public by default"
This commit is contained in:
committed by
Christoffer Lerno
parent
13e95553bb
commit
97ac957cb7
@@ -1,54 +1,299 @@
|
||||
module std::array;
|
||||
import std::mem;
|
||||
|
||||
public macro make($Type, usize elements)
|
||||
macro make($Type, usize elements)
|
||||
{
|
||||
assert(elements > 0);
|
||||
$Type* ptr = mem::alloc($Type.sizeof, elements);
|
||||
return ptr[0..(elements - 1)];
|
||||
}
|
||||
|
||||
public macro make_zero($Type, usize elements)
|
||||
macro make_zero($Type, usize elements)
|
||||
{
|
||||
assert(elements > 0);
|
||||
$Type* ptr = mem::calloc($Type.sizeof, elements);
|
||||
return ptr[0..(elements - 1)];
|
||||
}
|
||||
|
||||
public template vararray <Type>
|
||||
template vararray <Type>
|
||||
{
|
||||
public struct VarArray
|
||||
struct List
|
||||
{
|
||||
usize size;
|
||||
usize capacity;
|
||||
Type *entries;
|
||||
}
|
||||
|
||||
public func void VarArray.append(VarArray *array, Type element)
|
||||
static func void List.ensureCapacity(List *list) @inline
|
||||
{
|
||||
if (array.capacity == array.size)
|
||||
if (list.capacity == list.size)
|
||||
{
|
||||
array.capacity = array.capacity ? 2 * array.capacity : 16;
|
||||
array.entries = mem::realloc(array.entries, Type.sizeof * array.capacity);
|
||||
list.capacity = list.capacity ? 2 * list.capacity : 16;
|
||||
list.entries = mem::realloc(list.entries, Type.sizeof * list.capacity);
|
||||
}
|
||||
array.entries[array.size++] = element;
|
||||
}
|
||||
|
||||
public func usize VarArray.len(VarArray *array)
|
||||
func void List.push(List *list, Type element) @inline
|
||||
{
|
||||
return array.size;
|
||||
list.append(element);
|
||||
}
|
||||
|
||||
public func Type VarArray.get(VarArray *array, usize index)
|
||||
func void List.append(List *list, Type element)
|
||||
{
|
||||
return array.entries[index];
|
||||
list.ensureCapacity();
|
||||
list.entries[list.size++] = element;
|
||||
}
|
||||
|
||||
public func void VarArray.free(VarArray *array)
|
||||
/**
|
||||
* @require list.size > 0
|
||||
*/
|
||||
func Type List.pop(List *list)
|
||||
{
|
||||
mem::free(array.entries);
|
||||
array.capacity = 0;
|
||||
array.size = 0;
|
||||
return list.entries[--list.size];
|
||||
}
|
||||
|
||||
/**
|
||||
* @require list.size > 0
|
||||
*/
|
||||
func Type List.popFirst(List *list)
|
||||
{
|
||||
Type value = list.entries[0];
|
||||
list.removeAt(0);
|
||||
return value;
|
||||
}
|
||||
|
||||
func void List.removeAt(List *list, usize index)
|
||||
{
|
||||
for (usize i = index + 1; i < list.size; i++)
|
||||
{
|
||||
list.entries[i - 1] = list.entries[i];
|
||||
}
|
||||
list.size--;
|
||||
}
|
||||
|
||||
func void List.pushFront(List *list, Type type) @inline
|
||||
{
|
||||
list.insertAt(0, type);
|
||||
}
|
||||
|
||||
func void List.insertAt(List *list, usize index, Type type)
|
||||
{
|
||||
list.ensureCapacity();
|
||||
for (usize i = list.size; i > index; i--)
|
||||
{
|
||||
list.entries[i] = list.entries[i - 1];
|
||||
}
|
||||
list.size++;
|
||||
list.entries[index] = type;
|
||||
}
|
||||
|
||||
func void List.removeLast(List *list)
|
||||
{
|
||||
list.size--;
|
||||
}
|
||||
|
||||
func void List.removeFirst(List *list)
|
||||
{
|
||||
list.removeAt(0);
|
||||
}
|
||||
|
||||
func Type* List.first(List *list)
|
||||
{
|
||||
return list.size ? &list.entries[0] : null;
|
||||
}
|
||||
|
||||
func Type* List.last(List *list)
|
||||
{
|
||||
return list.size ? &list.entries[list.size - 1] : null;
|
||||
}
|
||||
|
||||
func bool List.isEmpty(List *list)
|
||||
{
|
||||
return list.size;
|
||||
}
|
||||
|
||||
func usize List.len(List *list)
|
||||
{
|
||||
return list.size;
|
||||
}
|
||||
|
||||
func Type List.get(List *list, usize index)
|
||||
{
|
||||
return list.entries[index];
|
||||
}
|
||||
|
||||
func void List.free(List *list)
|
||||
{
|
||||
mem::free(list.entries);
|
||||
list.capacity = 0;
|
||||
list.size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template linklist <Type>
|
||||
{
|
||||
static struct Node
|
||||
{
|
||||
Node *next;
|
||||
Node *prev;
|
||||
Type value;
|
||||
}
|
||||
|
||||
struct LinkedList
|
||||
{
|
||||
usize size;
|
||||
Node *first;
|
||||
Node *last;
|
||||
}
|
||||
|
||||
func void LinkedList.push(LinkedList *list, Type value)
|
||||
{
|
||||
list.linkLast(value);
|
||||
}
|
||||
|
||||
static func void LinkedList.linkFirst(LinkedList *list, Type value)
|
||||
{
|
||||
Node *first = list.first;
|
||||
Node *new_node = @mem::malloc(Node);
|
||||
*new_node = { .next = first, .value = value };
|
||||
list.first = new_node;
|
||||
if (!first)
|
||||
{
|
||||
list.last = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
first.prev = new_node;
|
||||
}
|
||||
list.size++;
|
||||
}
|
||||
|
||||
static func void LinkedList.linkLast(LinkedList *list, Type value)
|
||||
{
|
||||
Node *last = list.last;
|
||||
Node *new_node = mem::alloc(Node.sizeof);
|
||||
*new_node = { .prev = last, .value = value };
|
||||
list.last = new_node;
|
||||
if (!last)
|
||||
{
|
||||
list.first = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
last.next = new_node;
|
||||
}
|
||||
list.size++;
|
||||
}
|
||||
|
||||
func void LinkedList.free(LinkedList *list)
|
||||
{
|
||||
for (Node* node = list.first; node != null;)
|
||||
{
|
||||
Node* next = node.next;
|
||||
mem::free(node);
|
||||
node = next;
|
||||
}
|
||||
list.first = null;
|
||||
list.last = null;
|
||||
list.size = 0;
|
||||
}
|
||||
|
||||
func usize LinkedList.len(LinkedList* list) @inline
|
||||
{
|
||||
return list.size;
|
||||
}
|
||||
|
||||
func Type LinkedList.get(LinkedList* list, usize index)
|
||||
{
|
||||
Node* node = list.first;
|
||||
while (index--)
|
||||
{
|
||||
node = node.next;
|
||||
}
|
||||
return node.value;
|
||||
}
|
||||
/**
|
||||
* @require succ != null
|
||||
**/
|
||||
static func void LinkedList.linkBefore(LinkedList *list, Node *succ, Type value)
|
||||
{
|
||||
Node* pred = succ.prev;
|
||||
Node* new_node = @mem::malloc(Node);
|
||||
*new_node = { .prev = pred, .next = succ, .value = value };
|
||||
succ.prev = new_node;
|
||||
if (!pred)
|
||||
{
|
||||
list.first = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
pred.next = new_node;
|
||||
}
|
||||
list.size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require f == list.first && f != null
|
||||
**/
|
||||
static func void unlinkFirst(LinkedList* list, Node* f)
|
||||
{
|
||||
Node* next = f.next;
|
||||
mem::free(f);
|
||||
list.first = next;
|
||||
if (!next)
|
||||
{
|
||||
list.last = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
next.prev = null;
|
||||
}
|
||||
list.size--;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require l == list.last && l != null
|
||||
**/
|
||||
static func void LinkedList.unlinkLast(LinkedList *list, Node* l)
|
||||
{
|
||||
Node* prev = l.prev;
|
||||
list.last = prev;
|
||||
mem::free(l);
|
||||
if (!prev)
|
||||
{
|
||||
list.first = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev.next = null;
|
||||
}
|
||||
list.size--;
|
||||
}
|
||||
|
||||
/**
|
||||
* @require x != null
|
||||
**/
|
||||
static func void LinkedList.unlink(LinkedList* list, Node* x)
|
||||
{
|
||||
Node* next = x.next;
|
||||
Node* prev = x.prev;
|
||||
if (!prev)
|
||||
{
|
||||
list.first = next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev.next = next;
|
||||
}
|
||||
if (!next)
|
||||
{
|
||||
list.last = prev;
|
||||
}
|
||||
else
|
||||
{
|
||||
next.prev = prev;
|
||||
}
|
||||
mem::free(x);
|
||||
list.size--;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,12 +29,12 @@ extern func int _putchar(char c) @cname("putchar");
|
||||
|
||||
extern File *__stdinp;
|
||||
|
||||
public func int putchar(char c) @inline
|
||||
func int putchar(char c) @inline
|
||||
{
|
||||
return _putchar(c);
|
||||
}
|
||||
|
||||
public func int print(char *message)
|
||||
func int print(char *message)
|
||||
{
|
||||
char* pointer = message;
|
||||
while (*pointer != '\0')
|
||||
@@ -45,7 +45,7 @@ public func int print(char *message)
|
||||
return 1;
|
||||
}
|
||||
|
||||
public func int println(char *message) @inline
|
||||
func int println(char *message) @inline
|
||||
{
|
||||
return _puts(message);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
module std::math;
|
||||
|
||||
union DoubleLong
|
||||
static union DoubleLong
|
||||
{
|
||||
double f;
|
||||
ulong i;
|
||||
}
|
||||
|
||||
public func double log10(double x)
|
||||
func double log10(double x)
|
||||
{
|
||||
const double IVLN10HI = 4.34294481878168880939e-01; /* 0x3fdbcb7b, 0x15200000 */
|
||||
const double IVLN10LO = 2.50829467116452752298e-11; /* 0x3dbb9438, 0xca9aadd5 */
|
||||
@@ -108,7 +108,7 @@ func double cos_limited(double x, double y)
|
||||
return w + (((1.0 - w) - hz) + (z*r - x*y));
|
||||
}
|
||||
|
||||
public func double sin_limited(double x, double y, bool iy)
|
||||
static func double sin_limited(double x, double y, bool iy)
|
||||
{
|
||||
const double S1 = -1.66666666666666324348e-01; // 0xBFC55555, 0x55555549
|
||||
const double S2 = 8.33333333332248946124e-03; // 0x3F811111, 0x1110F8A6
|
||||
|
||||
@@ -5,32 +5,32 @@ extern func void* _realloc(void* ptr, usize bytes) @cname("realloc");
|
||||
extern func void* _calloc(usize bytes, usize elements) @cname("calloc");
|
||||
extern func void _free(void* ptr) @cname("free");
|
||||
|
||||
public enum AllocationKind
|
||||
enum AllocationKind
|
||||
{
|
||||
ALLOC,
|
||||
REALLOC,
|
||||
FREE,
|
||||
}
|
||||
|
||||
public enum AllocationFailureKind
|
||||
enum AllocationFailureKind
|
||||
{
|
||||
OUT_OF_MEMORY
|
||||
}
|
||||
|
||||
public error AllocationFailure
|
||||
error AllocationFailure
|
||||
{
|
||||
AllocationFailureKind failureKind;
|
||||
}
|
||||
|
||||
public define AllocatorFunction = func void!(void *data, void** pointer, usize bytes, usize alignment, AllocationKind kind);
|
||||
define AllocatorFunction = func void!(void *data, void** pointer, usize bytes, usize alignment, AllocationKind kind);
|
||||
|
||||
public struct Allocator
|
||||
struct Allocator
|
||||
{
|
||||
AllocatorFunction allocation_function;
|
||||
void *data;
|
||||
}
|
||||
|
||||
public func void! system_malloc_function(void *unused, void** pointer, usize bytes, usize alignment, AllocationKind kind) @inline
|
||||
func void! system_malloc_function(void *unused, void** pointer, usize bytes, usize alignment, AllocationKind kind) @inline
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
@@ -52,7 +52,7 @@ public func void! system_malloc_function(void *unused, void** pointer, usize byt
|
||||
$unreachable;
|
||||
}
|
||||
|
||||
public struct RingAllocator
|
||||
struct RingAllocator
|
||||
{
|
||||
char *data;
|
||||
usize size;
|
||||
@@ -60,7 +60,7 @@ public struct RingAllocator
|
||||
}
|
||||
|
||||
|
||||
public func void* RingAllocator.alloc(RingAllocator *allocator, usize size)
|
||||
func void* RingAllocator.alloc(RingAllocator *allocator, usize size)
|
||||
{
|
||||
if (size > allocator.size) return null;
|
||||
// Wraparound? If so, start at the beginning.
|
||||
@@ -74,7 +74,7 @@ public func void* RingAllocator.alloc(RingAllocator *allocator, usize size)
|
||||
return data;
|
||||
}
|
||||
|
||||
public func void* RingAllocator.realloc(RingAllocator *allocator, void* ptr, usize size)
|
||||
func void* RingAllocator.realloc(RingAllocator *allocator, void* ptr, usize size)
|
||||
{
|
||||
if (size > allocator.size) return null;
|
||||
assert(allocator.data >= ptr && ptr < allocator.data + size, "Realloc on other allocator.");
|
||||
@@ -111,29 +111,29 @@ public func void* RingAllocator.realloc(RingAllocator *allocator, void* ptr, usi
|
||||
return allocator.data;
|
||||
}
|
||||
|
||||
public Allocator main_allocator = { &system_malloc_function, null };
|
||||
Allocator main_allocator = { &system_malloc_function, null };
|
||||
|
||||
public macro malloc($Type)
|
||||
macro malloc($Type)
|
||||
{
|
||||
return ($Type*)(mem::alloc($Type.sizeof));
|
||||
}
|
||||
|
||||
public func void* alloc(usize size, usize elements = 1) @inline
|
||||
func void* alloc(usize size, usize elements = 1) @inline
|
||||
{
|
||||
return _malloc(size * elements);
|
||||
}
|
||||
|
||||
public func void* calloc(usize size, usize elements = 1) @inline
|
||||
func void* calloc(usize size, usize elements = 1) @inline
|
||||
{
|
||||
return _calloc(size, elements);
|
||||
}
|
||||
|
||||
public func void* realloc(void *ptr, usize size) @inline
|
||||
func void* realloc(void *ptr, usize size) @inline
|
||||
{
|
||||
return _realloc(ptr, size);
|
||||
}
|
||||
|
||||
public func void free(void* ptr) @inline
|
||||
func void free(void* ptr) @inline
|
||||
{
|
||||
_free(ptr);
|
||||
}
|
||||
Reference in New Issue
Block a user