Created
April 5, 2018 15:08
-
-
Save run-dlang/4db26c1730fc98fadc4701c5c6efcece to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import std.stdio; | |
| void main() | |
| { | |
| ivec4 v = ivec4([0, 1, 2, 3]); | |
| writeln(v); | |
| writeln(v.xyzw); | |
| writeln(v.w); | |
| writeln(v.stpq); | |
| writeln(v.rgba); | |
| v.xy = ivec2([7,8]); | |
| writeln(v); | |
| v.xy = v.yx; | |
| writeln(v); | |
| v.x = 1; | |
| writeln(v); | |
| writeln(v.x); | |
| writeln(swizzleIndicies!4("xvzw")); | |
| } | |
| /// last elem is 0 if invalid, 1 if valid | |
| ubyte[N+1] swizzleIndicies(size_t N)(string swizzle) | |
| { | |
| ubyte[N+1] result; | |
| swizzleIndicies(swizzle, result[]); | |
| return result; | |
| } | |
| void swizzleIndicies(string swizzle, ubyte[] result) | |
| { | |
| size_t N = swizzle.length; | |
| assert(N+1 == result.length); | |
| // 0 xyzw, 1 stpq, 2 rgba | |
| // [a b]c d e f[g]h i j k l m n o[p q r s t]u v[w x y z] | |
| ubyte[26] indicies = [3,2,4,4,4,4,1,4,4,4,4,4,4,4,4,2,3,0,0,1,4,4,3,0,1,2]; // 4 is invalid | |
| ubyte[26] groups = [2,2,3,3,3,3,2,3,3,3,3,3,3,3,3,1,1,2,1,1,3,3,0,0,0,0]; // 3 is invalid | |
| ubyte prevGroup = groups[swizzle[0]-'a']; | |
| foreach (i, char elem; swizzle) | |
| { | |
| if (elem >= 'a' && elem <= 'z') // xyzw | |
| { | |
| ubyte index = indicies[elem-'a']; | |
| ubyte group = groups[elem-'a']; | |
| bool hasInvalidElement = index == 4; | |
| bool hasMultipleGroups = group != prevGroup; | |
| if (hasInvalidElement || hasMultipleGroups) return; | |
| prevGroup = group; | |
| result[i] = index; | |
| } | |
| else | |
| { | |
| return; | |
| } | |
| } | |
| result[N] = 1; | |
| } | |
| alias ivec4 = Vector!(int, 4); | |
| alias ivec2 = Vector!(int, 2); | |
| struct Vector(T, int N) | |
| { | |
| T[N] data; | |
| template opDispatch(string swizzle) | |
| { | |
| enum len = swizzle.length; | |
| enum ubyte[len+1] indicies = swizzleIndicies!len(swizzle); | |
| static assert(indicies[len] == 1, "'" ~ swizzle ~ "' is invalid swizzling"); | |
| static if (len == 1) | |
| { | |
| @property auto opDispatch() @safe pure { | |
| return data[indicies[0]]; | |
| } | |
| @property auto opDispatch(T other) @safe pure { | |
| return data[indicies[0]] = other; | |
| } | |
| } | |
| else | |
| { | |
| @property auto opDispatch() @safe pure { | |
| Vector!(T, len) result; | |
| static foreach (i, elemIndex; indicies[0..$-1]) | |
| result.data[i] = data[elemIndex]; | |
| return result; | |
| } | |
| @property auto opDispatch(Vector!(T, len) other) @safe pure { | |
| Vector!(T, len) result; | |
| static foreach (i, elemIndex; indicies[0..$-1]) { | |
| result.data[i] = data[elemIndex] = other.data[i]; | |
| } | |
| return result; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment