Skip to content

Instantly share code, notes, and snippets.

@ArtemAvramenko
ArtemAvramenko / LargeBitArray.cs
Created November 1, 2023 13:36
BitArray implementation that allows to store more than 2^32 values
public class LargeBitArray
{
private readonly int _bucketSize;
private readonly long _capacity;
public LargeBitArray(long capacity = int.MaxValue) // ~272 MB for 2^31 values
{
if (capacity < 1 || capacity > (long)2e18)
{
throw new ArgumentException("Capacity is out of range");
@ArtemAvramenko
ArtemAvramenko / node-script-with-dependencies.js
Last active October 11, 2023 11:47
A standalone single-file Node.js script that installs dependencies locally in dotfiles
/**
* This small function allows you to distribute your script as a single file, without
* requiring a separate package.json. When running a script through a node, the dependencies
* that are installed will not affect neighboring scripts or global packages.
* @summary Installs dependencies in the local dotfiles folder
*/
const installDependencies = dependencies => {
const fs = require('fs');
const dependenciesPath = './.npm-' + __filename.match(/[^\\/]+$/)[0];
fs.existsSync(dependenciesPath) || fs.mkdirSync(dependenciesPath);
@ArtemAvramenko
ArtemAvramenko / AsyncLazy.cs
Last active October 5, 2023 11:53
Lazy pattern implementation with asynchronous locks that release the thread for waiting time
// https://stackoverflow.com/questions/17975609/lazy-load-properties-with-async/41141728#41141728
#nullable enable
public class AsyncLazy<T>
{
private Func<Task<T>>? _valueFactory;
private volatile SemaphoreSlim? _semaphore = new(1, 1);
private volatile Boxed? _boxed;
@ArtemAvramenko
ArtemAvramenko / dark-mode.reg
Created September 8, 2023 13:52
Enable the dark theme through the registry
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize]
"AppsUseLightTheme"=dword:00000000
@ArtemAvramenko
ArtemAvramenko / toBasicLatin.ts
Last active July 24, 2024 19:01
Converts strings in national alphabets to Basic Latin identifiers
const toBasicLatin = (function() {
// see https://gist.github.com/ArtemAvramenko/ec3b5358221f8b6e9f3e9efe1d0a3066
const data =
'AαаAEæBβбCHчDđðδдDJђDZѕDZHџEεηеэFƒфFIfiFLflGγгґGJѓIıιиіIAяIEєIOёIUюJјKκкKHχхKJќLłλл'+
'LJљMμмNνнNJњOøοωоOEœPπпPHφPSψRρрSσςсSHшSHCHщSSßTτтTHþθTSцTSHћUµυуVвXξYыYIїZζзZHж';
let map = {};
for(const x of data.matchAll(/([A-Z]+)([^A-Z]+)/g)) {
const latValue = x[1].toLowerCase();
@ArtemAvramenko
ArtemAvramenko / GenerateBasicLatinMapping.cs
Last active July 10, 2024 13:45
Generator for mapping national alphabets to the Basic Latin
var unidecodeLines = File.ReadAllLines(@"C:\Users\username\Desktop\Unidecode\unidecode.tsv");
var outputFile = @"C:\Users\username\Desktop\Unidecode\onlyletters.tsv";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encodings = new[] {
10000, 10006, 10007, 10010, 10017, 10029, 10079, 10081, 10082,
1250, 1251, 1252, 1253, 1254, 1257, 1258
}.Select(i => Encoding.GetEncoding(i)).ToArray();
bool isSupportedEncoding(string s)
=> encodings.Any(e => e.GetString(e.GetBytes(s)) == s);
// In practical scenarios, such a transformation speeds up the search by dozens of times:
// Apple, Apricot, Banana => (?:Ap(?:ple|ricot)|Banana)
private static void GenerateRegex(StringBuilder result, IList<string> words, int level = 0)
{
const int minPrefix = 1;
const int maxLevel = 300;
void AppendLevel()
{
// sb.Append('\n');
var amount = 900m;
var currency = "μBTC";
// https://en.wikipedia.org/wiki/Micro-
int prefixIndex = currency.Length > 3 ? "mμnpf".IndexOf(currency[0]) : -1;
if (prefixIndex >= 0)
{
currency = currency[1..];
amount /= (decimal)Math.Pow(1000, prefixIndex + 1);
}
@ArtemAvramenko
ArtemAvramenko / money-formatting.js
Last active April 1, 2025 11:28
Script for determining regional number formats
// type numberFormat =
'dot_with_comma_grouping' | // en-US
'dot_with_indian_grouping' | // en-IN
'dot_with_apostrophe_grouping' | // de-CH
'comma_with_dot_grouping' | // it-IT
'comma_with_space_grouping'; // pt-PT
// 100,000.50 - DEFAULT FORMAT except for Europe (en-US)
["United Kingdom", "Malta"]
@ArtemAvramenko
ArtemAvramenko / fullTextSearch.js
Last active October 23, 2024 10:06
A script for a heuristic search for a similar name
const expect = actual => {
const toBe = expected => {
let [a, b] = [actual, expected].map(JSON.stringify);
if (a !== b) {
console.error(a + ' should be ' + b);
}
};
return { toBe, toEqual: toBe };
};