Skip to content

Instantly share code, notes, and snippets.

// Slice the string along the boundaries of text elements so that the size of each
// substring does not exceed chunk_size_max_chars characters (char).
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static IEnumerable<string> StringToTextElements(string source, int chunk_size_max_chars)
{
int chunk_start_index = 0; // first character index of chunk
int chunk_end_index = 0; // latest character index of chunk
int chunk_max_index = chunk_size_max_chars; // max index of next chunk
var enumerator = StringInfo.GetTextElementEnumerator(source);
@teheran
teheran / CpuUsageAlert.cs
Last active April 22, 2021 15:10
CPU usage alerting
using System;
using System.Linq;
using System.Diagnostics;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
namespace PerfCountersAlert
{
class Program
{
@teheran
teheran / fib.cs
Created September 25, 2020 20:56
static long Fib(int n)
{
return
Enumerable.Repeat(0, Math.Abs(n))
.Aggregate(
Tuple.Create(0L, 1L),
(tuple, _) => Tuple.Create(tuple.Item2, tuple.Item1 + tuple.Item2),
tuple => tuple.Item1
) * ((n > 0) ? 1 : ((n % 2) != 0) ? 1 : -1);
}
@teheran
teheran / d.cs
Last active September 20, 2020 03:58
Генерация скобочных последовательностей (https://contest.yandex.ru/contest/8458/problems/D)
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
private static IEnumerable<String> Generate(uint len) {
return GenerateImpl("", 0, 0, len);
}
private static IEnumerable<String> GenerateImpl(String head, int opn, int cls, uint len)
@teheran
teheran / sub-flow.json
Last active August 27, 2020 07:39
Node-RED: pulse counter controller (IN: falling OR rising edge message; OUT:counter value; velocity; on-off circle counter; on-off power). (контроллер импульсного расходомера)
[{"id":"8e490455.b9fa28","type":"subflow","name":"Velocity","info":"","category":"","in":[{"x":40.00005912780762,"y":127.00003242492676,"wires":[{"id":"e2fd1aa.88edfe8"}]}],"out":[{"x":1584.0000534057617,"y":122.00000381469727,"wires":[{"id":"3982accc.82d4b4","port":0}]},{"x":1585.000114440918,"y":408.0000190734863,"wires":[{"id":"f37a6c49.82af6","port":0}]},{"x":1580,"y":660,"wires":[{"id":"c57ef11f.dd02f","port":0}]},{"x":1580,"y":780,"wires":[{"id":"6ed28f42.c1c75","port":0}]}],"env":[{"name":"stop_factor","type":"num","value":"25"},{"name":"pulse_weight","type":"num","value":"0.01"},{"name":"Q_min","type":"num","value":"0.06"},{"name":"Q_max","type":"num","value":"10"},{"name":"digits","type":"num","value":"8"},{"name":"init","type":"num","value":"10690.420"},{"name":"unit","type":"str","value":"m³"},{"name":"update_rate","type":"num","value":"60"}],"color":"#A6BBCF","inputLabels":["increment message"],"outputLabels":["total value","velocity","cycle counter","consumed per cycle"],"icon":"node-red/function
@teheran
teheran / index.html
Created November 14, 2012 17:52
A CodePen by teheran. jQuery Draggable with out jQuery UI - It doesn't have all the fancy callbacks and options, but hey, it makes things draggable. From: http://reader-download.googlecode.com/svn/trunk/jquery-draggable/index.html
<div>
<svg id="svgRoot"
xmlns="http://www.w3.org/2000/svg" version="1.1"
width="100%" height="100%"
viewBox="0 0 300 300">
<symbol id='symbol'>
<image width="100" height="100"
xlink:href="http://upload.wikimedia.org/wikipedia/commons/c/cb/Footnote.svg"/>
</symbol>
@teheran
teheran / index.html
Created November 14, 2012 17:50
A CodePen by Chris Coyier. jQuery Draggable with out jQuery UI - It doesn't have all the fancy callbacks and options, but hey, it makes things draggable. From: http://reader-download.googlecode.com/svn/trunk/jquery-draggable/index.html
<div>Drag me</div>