Skip to content

Instantly share code, notes, and snippets.

View tom-tan's full-sized avatar
🌴
(:3 」∠ )_

Tomoya Tanjo tom-tan

🌴
(:3 」∠ )_
View GitHub Profile
@tom-tan
tom-tan / ref.d
Last active December 19, 2015 10:19
Library implementation of T& in C++
#!/usr/bin/env rdmd
/**
* Library implementation of T& in C++
* i.e. Its value can be modified
* but its address cannot be modified.
**/
struct Ref(T)
{
@system this(ref T ptr) pure nothrow
@tom-tan
tom-tan / gist:6026626
Created July 18, 2013 03:55
randomCover 前後で乱数進まないのはおかしい気がする.
#!/usr/bin/env rdmd
import std.stdio;
import std.random;
import std.range;
void main(string[] args)
{
writeRandomCovered!Random(10);
}
@tom-tan
tom-tan / cartesian.d
Last active December 20, 2015 02:59
Another implementation of std.algorithm.cartesianProduct
/**
* Another implementation of std.algorithm.cartesianProduct
* See_Also: $(LINK2 http://d.puremagic.com/issues/show_bug.cgi?id=10693, Issue 10693)
*/
@safe auto cartesianProduct(RR...)(RR Rs)
{
return CartesionProduct!RR(Rs);
}
@safe struct CartesionProduct(RR...)
@tom-tan
tom-tan / integer_partition.d
Created July 25, 2013 06:25
D implementation of ZS1 algorithm to enumerate integer partitions. See: http://en.wikipedia.org/wiki/Partition_(number_theory) (in English http://ja.wikipedia.org/wiki/自然数の分割 (日本語)
import std.traits;
/**
* Returns: InputRange which returns integer partitions of $(D_PARAM n) in lexicographic order.
* See_Also: ZS1 in "Fast Algorithms for Generating Integer Partitions"
*/
@safe pure nothrow
auto integerPartitions(UInt)(UInt n)
if (isIntegral!UInt && isUnsigned!UInt)
{
@tom-tan
tom-tan / randomcover.d
Last active December 20, 2015 13:19
Another implementation of std.random.RandomCover. It is faster than std.random.RandomCover in average time.
/**
* Another implementation of std.random.RandomCover
*
* Execution time for popFront (n: length):
* std.random: O(n)
* this : O(1)
*
* Memory consumption:
* std.random: O(n)
* this : O(n)
@tom-tan
tom-tan / gist:6432208
Created September 4, 2013 02:40
Ruby の Object#tap をD言語で書いてみた.デバッグ用途にどうぞ.
auto tap(alias fun, Range)(Range r)
{
alias fun_ = unaryFun!fun;
struct ResultType
{
this(Range r) { this.r = r; }
@property auto front()
{
auto ret = r.front;
fun_(ret);
@tom-tan
tom-tan / gist:6634316
Last active December 23, 2015 12:19
memmove in D
void* memmove(void* dest_, const void* src_, size_t n)
{
auto dest = (cast(byte*)dest_)[0..n];
auto src = (cast(byte*)src_)[0..n];
if (dest.ptr < src.ptr || (src.ptr < dest.ptr && src.ptr+n < dest.ptr))
{
// move forward order
foreach(i; 0..n)
{
dest[i] = src[i];
#!/usr/bin/env rdmd
import std.conv;
import std.exception;
version(unittest) {}
else
void main()
{
// Error: static assert "The error handler's return value(void) does not have a common type with the expression(int)."
int x = "x".to!int.ifThrown!ConvException((e) {
// Written in the D programming language.
/**
This module includes helper functions to anotate unsafe operations as trusted operations.
See: http://dlang.org/function.html
Here is the list of unsafe operations described in the spec.
- No casting from a pointer type to any type other than void*.
- No casting from any non-pointer type to a pointer type.
- No modification of pointer values.
--- configure 2014-10-27 13:56:10.000000000 +0900
+++ ../Bridger_r2014-12-01/configure 2017-05-18 18:22:22.000000000 +0900
@@ -15813,8 +15813,8 @@ cat >>conftest.$ac_ext <<_ACEOF
#include <boost/version.hpp>
boost-lib-version = BOOST_LIB_VERSION
_ACEOF
-if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
- tr -d '\r' |
+if (eval "$ac_cpp conftest.$ac_ext") 2>&5 | sed '/^#/d' | sed -e 's/^[ ]+//' |
+ tr -d '\n' |