Skip to content

Instantly share code, notes, and snippets.

@mzaks
mzaks / petri_net.c
Last active August 6, 2021 08:54
Simple Petri net implementation in C with a couple examples
#include <stdio.h>
#include <libc.h>
typedef struct {
int *marking;
int *takes;
int *puts;
int transition_count;
int place_count;
} PetriNet;
func barBellNet() -> PetriNet {
let totoalWeightInGram = Place(name: "tototalWeightInGram")
let barBellSelected = Place(name: "BarBellSelected")
let barBellNotSelected = Place(name: "BarBellNotSelected", initNumberOfTokens: 1)
let barBell10Kg = Place(name: "10KgBarBell", initNumberOfTokens: 1)
let barBell15Kg = Place(name: "15KgBarBell", initNumberOfTokens: 1)
let barBell17_5Kg = Place(name: "17.5_KgBarBell", initNumberOfTokens: 1)
let barBell20Kg = Place(name: "20_KgBarBell", initNumberOfTokens: 1)
let weight0_5kg = Place(name: "0.5_KgWeight", initNumberOfTokens: 4)
let weight1kg = Place(name: "1_KgWeight", initNumberOfTokens: 4)
func ticTacToeNet() -> PetriNet {
let xTurn = Place(name: "xTurn", initNumberOfTokens: 1)
let oTurn = Place(name: "oTurn")
let xWin = Place(name: "xWin")
let oWin = Place(name: "oWin")
let e1 = Place(name: "e1", initNumberOfTokens: 1)
let e2 = Place(name: "e2", initNumberOfTokens: 1)
let e3 = Place(name: "e3", initNumberOfTokens: 1)
let e4 = Place(name: "e4", initNumberOfTokens: 1)
@mzaks
mzaks / spec.md
Last active December 1, 2020 14:50
IndexedData white paper
1. Motivation
This format is designed to allow users pack data together for random access and with space efficiency in mind.
2. Internal structure
IndexedData (further referenced as idata) can be split up in two general regions: manifest and data.
Manifest region contains information needed to identify the number of elements in data region.
Plus it contains data neded to extract a signle element out of the data region.
Optionaly it can contain a validation key, which can be used to ensure that the data at hand is in fact idata.
The data region contains all data items linearly concatenaited to each other.
@mzaks
mzaks / search_comparison.rs
Created June 8, 2020 09:48
compare binary search with eytzinger
let now = Instant::now();
for (index, e) in vec_clone.iter().enumerate() {
assert_eq!(first_index_for_eytzinger(&vec_clone, e).expect(""), index)
}
let eytz_search = now.elapsed();
let now = Instant::now();
for (index, e) in vec_unstable.iter().enumerate() {
assert_eq!(vec_unstable.binary_search(e).expect(""), index)
}
@mzaks
mzaks / find.rs
Created June 8, 2020 09:35
Find first index for eytzinger
pub fn first_index_for_eytzinger<T>(arr: &[T], value: &T) -> Option<usize> where T: PartialOrd {
let mut index = 0;
let count = arr.len();
while index < count {
let candidate = unsafe{ arr.get_unchecked(index) };
if value == candidate {
return Some(index)
}
index = index * 2 + 1 + ((candidate < value) as usize);
}
@mzaks
mzaks / fix_value.py
Created December 30, 2019 08:38
FlexBuffers Python Decoder
import struct
import base64
import json
from .value_types import ValueType
class FlxValue:
def __init__(self, buffer, offset, parent_width, packed_type):
self._buffer = buffer
self._offset = offset
@mzaks
mzaks / ComponentAnalyzerWindow.cs
Created September 26, 2019 16:04
A small Unity3D editor window to explore component sizes and find issues in component field layout
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using Unity.Entities;
@mzaks
mzaks / find_index.htp
Last active August 10, 2019 10:44
Binary search in Happy Tree Frame programming language
frame {
items: [int] # implicit input
value: int # implicit input
min_index: int
max_index: int
mid_index: int
<- found_index: int # explicit output
}
# sel is selector
All I present in my blog post is just thin infrastructure (Helpers if you will) on top of ISystemStateComponentData.
You can use it in Jobs and it is not expensive as it does not introduce anything you would not do yourself if you would like to perform diff check on an entity.
For a more practical explanation how I envision this stuff to work please have a look at this response:
https://medium.com/@icex33/i-am-not-sure-if-you-were-able-to-see-the-gist-i-added-add298168546
Now why is it possible to use in Jobs?
Because if you call the `Diff<Position,PositionHistory>()` method on entity you are not mutating anything. You are just comparing two component values - current position and previous position. The return type is a struct which has 3 properties. The Type is an enum with following cases:
- NothingChanged, both values are equal
- Added, there is a current value set but there is no previous value
- Updated, the current and previous values are different