Skip to content

Instantly share code, notes, and snippets.

View baratgabor's full-sized avatar

Gabor Barat baratgabor

View GitHub Profile
@baratgabor
baratgabor / GetThreadStack0.cs
Last active November 8, 2022 19:04
32 bit ThreadStack0 implementation in C#
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
/// <summary>
/// Only for 32 bit processes. The methods of this static class can find the special symbol ThreadStack0,
/// and follow pointer chains to return the end result in the specified type T.
@baratgabor
baratgabor / GetLongestPalindromes.cs
Last active April 28, 2019 14:51
Compact & naive C# function to get the longest palindromic substrings of a string (i.e. all palindromes excluding overlaps). Time complexity is over quadratic.
// Naive, suboptimal
IEnumerable<string> GetLongestPalindromes(string s)
{
List<string> palindromes = new List<string>();
var len = s.Length;
for (int i = 0; i < len; i++)
{
for (int j = len - 1; j > i; j--)
{
@baratgabor
baratgabor / AcceleratedSpacePartition.cs
Last active April 7, 2019 14:26
Specialized space partitioning container to solve a specific problem. Just for my own future reference. Sorry, probably not very useful for others due to the dependencies.
/// <summary>
/// Simple specialized container for handling large amounts of linearly moving objects in large spaces.
/// Can be used when an Octree or a BVH is too expensive to maintain, if there is a known area of space where AABB queries are concentrated.
/// Partitions space by creating an accelerated zone around a target object where AABB queries are faster to execute.
/// Objects outside the accelerated zone are throttled by feeding them aggregate delta times to drastically reduce the workload of updating them.
/// - Returns accurate results for queries outside the zone as well, but at a cost of brute force query + overhead.
/// - Throttling is not load-balanced over multiple ticks, i.e. every X tick is more expensive to execute.
/// </summary>
public class AcceleratedSpacePartition
{
@baratgabor
baratgabor / SuffixTrie.cs
Last active April 13, 2019 17:11
Simple suffix trie for 26 char English alphabet with O(n^2) build time and linear (search phrase length) search time. Ascertains longest repeated substring automatically during build. Iterative traversal. No practical use due to the quadratic build time and immense memory use typical for tries.
class SuffixTrie
{
private TrieNode _root = new TrieNode();
private string _LRS = string.Empty; // Longest repeated substring
private SuffixTrie(string value)
=> AddWord(value);
public static SuffixTrie Build(string value)
=> new SuffixTrie(value);
@baratgabor
baratgabor / ManachersPalindrome.cs
Last active April 25, 2019 12:00
Manacher's algorithm for finding palindromes, in C#, refactored to separate the pre-processing, and to return IEnumerable<string> of palindromes of specified min and max length. Exposes static methods for instant queries, and instance methods for pre-processing a string only once and then querying it multiple times.
class ManachersPalindrome
{
private readonly char[] s2;
private readonly int[] p;
public static ManachersPalindrome Build(string s)
=> new ManachersPalindrome(s);
private ManachersPalindrome(string s)
{
@baratgabor
baratgabor / cube_v_______quads_absIndex.obj
Created July 10, 2019 08:31
Simple sample ASCII Obj mesh file variations for Obj importer testing.
# Vertices: 8
# Points: 0
# Lines: 0
# Faces: 6
# Materials: 1
o 1
# Vertex list