
proc CORE.Array.Create stdcall uses ebx, size:DWORD
	invoke	mem_alloc, sizeof.CORE.Array
	mov	ebx, eax
	mov	eax, [size]
	mov	[ebx + CORE.Array.count], eax
	shl	eax, 2
	jnz	@f
	mov	eax, 1
    @@: add	eax, 4095
	and	eax, not 4095
	mov	[ebx + CORE.Array.size], eax
	invoke	mem_alloc, eax
	mov	[ebx + CORE.Array.items], eax
	mov	eax, ebx
	ret
endp

proc CORE.Array.Destroy stdcall, obj:DWORD
	mov	eax, [obj]
	invoke	mem_free, [eax + CORE.Array.items]
	invoke	mem_free, [obj]
	ret
endp

proc CORE.Array.Append stdcall, obj:DWORD, item:DWORD
	mov	eax, [obj]
	stdcall CORE.Array.Insert, eax, [eax + CORE.Array.count], [item]
	ret
endp

proc CORE.Array.Prepend stdcall, obj:DWORD, item:DWORD
	stdcall CORE.Array.Insert, [obj], 0, [item]
	ret
endp

proc CORE.Array.Insert stdcall uses ebx esi edi, obj:DWORD, idx:DWORD, item:DWORD
	mov	ebx, [obj]
	mov	ecx, [ebx + CORE.Array.count]
	cmp	[idx], ecx
	jbe	@f
	mov	[idx], ecx
    @@: inc	ecx
	shl	ecx, 2
	invoke	mem_realloc, [ebx + CORE.Array.items], ecx
	mov	[ebx + CORE.Array.items], eax
	mov	ecx, [ebx + CORE.Array.count]
	cmp	ecx, [idx]
	je	@f
	lea	edi, [eax + ecx * 4]
	lea	esi, [edi - 4]
	sub	ecx, [idx]
	std
	rep	movsd
	cld
    @@: pushd	[item]
	mov	ecx, [idx]
	popd	[eax + ecx * 4]
	mov	eax, [ebx + CORE.Array.count]
	inc	eax
	mov	[ebx + CORE.Array.count], eax
	ret
endp

proc CORE.Array.Remove stdcall uses ebx esi edi, obj:DWORD, idx:DWORD
	mov	ebx, [obj]
	mov	ecx, [idx]
	cmp	ecx, [ebx + CORE.Array.count]
	jae	@f
	mov	edi, [ebx + CORE.Array.items]
	lea	edi, [edi + ecx * 4]
	lea	esi, [edi + 4]
	cld
	neg	ecx
	add	ecx, [ebx + CORE.Array.count]
	dec	ecx
	rep	movsd
	dec	[ebx + CORE.Array.count]
	mov	eax, [ebx + CORE.Array.count]
	shl	eax, 2
	invoke	mem_realloc, [ebx + CORE.Array.items], eax
	mov	[ebx + CORE.Array.items], eax
    @@: ret
endp

proc CORE.Array.Find stdcall uses ebx, obj:DWORD, item:DWORD
	mov	ebx, [obj]
	mov	ecx, [item]
	mov	eax, [ebx + CORE.Array.count]
	mov	ebx, [ebx + CORE.Array.items]
    @@: dec	eax
	js	@f
	cmp	[ebx + eax * 4], ecx
	jne	@b
    @@: ret
endp
