Skip to content

Instantly share code, notes, and snippets.

View momvart's full-sized avatar

Mohammad Omidvar momvart

  • Simon Fraser University
  • BC, Canada
  • LinkedIn in/momvart
View GitHub Profile
@momvart
momvart / buggy_vec.rs
Last active September 14, 2023 18:31
An example of how unsafe Rust can cause memory deallocation bugs. A case of late `len` update and unsoundness.
#![feature(maybe_uninit_uninit_array)]
#![feature(extend_one)]
use std::mem::MaybeUninit;
fn main() {
let mut v = MyVec::new();
v.push(Box::new(DropReporter));
let mut dest = BadExtend(Default::default());
@momvart
momvart / visit.rs
Last active January 5, 2023 04:19
Inspired by the default generic rustc visitors, here are a set of visitors written for StatementKind, TerminatorKind, and Rvalue
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece, Mutability};
use rustc_middle::{
mir::{
AggregateKind, AssertMessage, BasicBlock, BinOp, BorrowKind, CastKind, Coverage,
FakeReadCause, InlineAsmOperand, Local, NonDivergingIntrinsic, NullOp, Operand, Place,
RetagKind, Rvalue, StatementKind, SwitchTargets, TerminatorKind, UnOp, UserTypeProjection,
},
ty::{Const, Region, Ty, Variance},
};
use rustc_span::Span;
@momvart
momvart / ISO8601Parser.cs
Last active April 10, 2022 16:39
A set of functions to parse some data and time data expressed in ISO 8601 format for C#.
using System;
using System.Text.RegularExpressions;
namespace RBC.WorkflowEngine.Core.Utilities;
/// <summary>
/// Provides a set of functions for parsing some of date- and time-related
/// data represented in ISO 8601 formats.
/// </summary>
/// <see href="https://en.wikipedia.org/wiki/ISO_8601">
@momvart
momvart / ThousandSeparatorVisualTransformation.kt
Last active April 9, 2022 09:41
A VisualTransformation for inserting thousand-separator commas in TextFields inputting numbers. It also has support for fraction parts and can put limit on the number of fraction digits. #android #compose
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.input.VisualTransformation
import java.text.DecimalFormat
import kotlin.math.min
class ThousandSeparatorVisualTransformation(
var maxFractionDigits: Int = Int.MAX_VALUE,
var minFractionDigits: Int = 0
@momvart
momvart / KotlinObjectExtensions.cs
Created November 24, 2021 16:04
A set of extensions functions written on object, by default available in Kotlin, ported for c#.
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Utilities
{
public static class KotlinObjectExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Also<T>(this T callee, Action<T> action)
@momvart
momvart / CircularProgressBar.kt
Last active August 28, 2021 14:07
My simple implementation of a circular progress bar with support of animation in Android
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AnimationUtils
import android.view.animation.Interpolator
import org.jetbrains.anko.displayMetrics
import kotlin.math.absoluteValue
@momvart
momvart / ButtonHoldClickTriggerTouchListener.kt
Last active August 28, 2021 14:06
A touch listener that makes a touch-and-hold click trigger functionality for you view in Android.
import android.view.MotionEvent
import android.view.View
import androidx.core.view.postDelayed
import kotlin.math.max
class ButtonHoldClickTriggerTouchListener(
private val startDelay: Long = STANDARD_DELAY,
private val minDelay: Long = STANDARD_MIN_DELAY_BETWEEN_TRIGGERS,
private val delayReduce: Long = STANDARD_DELAY_REDUCE
) : View.OnTouchListener {
@momvart
momvart / BLOSUM62.py
Created April 8, 2020 17:48
Simple BLOSUM62 implementation in python. Used for sequence alignment of proteins. Based on https://en.wikipedia.org/wiki/BLOSUM
BLOSUM62_MATRIX = [[4, 0, -2, -1, -2, 0, -2, -1, -1, -1, -1, -2, -1, -1, -1, 1, 0, 0, -3, -2],
[0, 9, -3, -4, -2, -3, -3, -1, -3, -1, -1, -3, -3, -3, -3, -1, -1, -1, -2, -2],
[-2, -3, 6, 2, -3, -1, -1, -3, -1, -4, -3, 1, -1, 0, -2, 0, -1, -3, -4, -3],
[-1, -4, 2, 5, -3, -2, 0, -3, 1, -3, -2, 0, -1, 2, 0, 0, -1, -2, -3, -2],
[-2, -2, -3, -3, 6, -3, -1, 0, -3, 0, 0, -3, -4, -3, -3, -2, -2, -1, 1, 3],
[0, -3, -1, -2, -3, 6, -2, -4, -2, -4, -3, 0, -2, -2, -2, 0, -2, -3, -2, -3],
[-2, -3, -1, 0, -1, -2, 8, -3, -1, -3, -2, 1, -2, 0, 0, -1, -2, -3, -2, 2],
[-1, -1, -3, -3, 0, -4, -3, 4, -3, 2, 1, -3, -3, -3, -3, -2, -1, 3, -3, -1],
[-1, -3, -1, 1, -3, -2, -1, -3, 5, -2, -1, 0, -1, 1, 2, 0, -1, -2, -3, -2],
[-1, -1, -4, -3, 0, -4, -3, 2, -2, 4, 2, -3, -3, -2, -2, -2, -1, 1, -2, -1],
@momvart
momvart / ObjectToInferredTypesConverter.cs
Last active February 27, 2021 20:35
A converter for `System.Text.Json` that recursively parse converts json to the inferred types. Uses `ExpandoObject` for objects.
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Dynamic;
public class ObjectToInferredTypesConverter : JsonConverter<object>
{
public override object Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) =>
@momvart
momvart / GMM.py
Created January 20, 2021 18:37
Implementation of Gaussian Mixture Models clustering using numpy and scipy.
import numpy as np
from numpy.random import choice
from numpy.linalg import norm
from scipy.stats import multivariate_normal
def train_gmm(data, k, convergence_threshold):
raw_data = data
data = pd.DataFrame(data) # Should be removed
feature_columns = data.columns
pis = np.ones((k, 1)) * 1 / k