Click here to Skip to main content
15,908,931 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I might need to optimize this XD Pin
Greg Utas2-Feb-20 11:29
professionalGreg Utas2-Feb-20 11:29 
GeneralRe: I might need to optimize this XD Pin
honey the codewitch2-Feb-20 11:46
mvahoney the codewitch2-Feb-20 11:46 
GeneralRe: I might need to optimize this XD Pin
Jörgen Andersson2-Feb-20 20:35
professionalJörgen Andersson2-Feb-20 20:35 
GeneralRe: I might need to optimize this XD Pin
honey the codewitch2-Feb-20 23:08
mvahoney the codewitch2-Feb-20 23:08 
GeneralRe: I might need to optimize this XD Pin
Jörgen Andersson2-Feb-20 23:09
professionalJörgen Andersson2-Feb-20 23:09 
GeneralRe: I might need to optimize this XD Pin
honey the codewitch2-Feb-20 23:12
mvahoney the codewitch2-Feb-20 23:12 
GeneralRe: I might need to optimize this XD Pin
Jörgen Andersson2-Feb-20 23:17
professionalJörgen Andersson2-Feb-20 23:17 
GeneralRe: I might need to optimize this XD Pin
honey the codewitch2-Feb-20 23:24
mvahoney the codewitch2-Feb-20 23:24 
Look away. Here's almost all of it. The stuff you don't see is very thin

C#
public static int Run(int[][] prog,LexContext input)
{
	input.EnsureStarted();
	int i,match=-1;
	_Fiber[] currentFibers, nextFibers, tmp;
	int currentFiberCount=0, nextFiberCount=0;
	int[] pc;
	// position in input
	int sp=0;
	// stores our captured input
	var sb = new StringBuilder(64);
	int[] saved, matched;
	saved = new int[2];
	currentFibers = new _Fiber[prog.Length];
	nextFibers = new _Fiber[prog.Length];
	_EnqueueFiber(ref currentFiberCount, ref currentFibers, new _Fiber(prog,0, saved), 0);
	matched = null;
	var cur = -1;
	if (LexContext.EndOfInput != input.Current)
	{
		var ch1 = unchecked((char)input.Current);
		if (char.IsHighSurrogate(ch1))
		{
			if (-1 == input.Advance())
				throw new ExpectingException("Expecting low surrogate in unicode stream. The input source is corrupt or not valid Unicode", input.Line, input.Column, input.Position, input.FileOrUrl);
			var ch2 = unchecked((char)input.Current);
			cur = char.ConvertToUtf32(ch1, ch2);
		}
		else
			cur = ch1;

	}
			
	while(0<currentFiberCount)
	{
		bool passed = false;
				
		for (i = 0; i < currentFiberCount; ++i)
		{
			var t = currentFibers[i];
			pc = t.Program[t.Index];
			saved = t.Saved;
			switch (pc[0])
			{
				case Compiler.Switch:
					var idx = 1;
					while(idx<pc.Length && -2<pc[idx])
					{
						if (_InRanges(pc, ref idx, cur))
						{
							while (-1!=pc[idx])
								++idx;

							++idx;
							passed = true;
							_EnqueueFiber(ref nextFiberCount, ref nextFibers, new _Fiber(t, pc[idx], saved), sp + 1);
							idx = pc.Length;
							break;
						}
						else
						{
							while (-1!=pc[idx])
								++idx;
							++idx;
						}
						++idx;
					}
					if(idx<pc.Length&&-2==pc[idx])
					{
						++idx;
						while(idx<pc.Length)
						{
							_EnqueueFiber(ref currentFiberCount, ref currentFibers, new _Fiber(t, pc[idx], saved), sp);
							++idx;
						}
					}
					break;
				case Compiler.Char:
					if (cur!= pc[1])
					{
						break;
					}
					goto case Compiler.Any;
				case Compiler.Set:
					idx = 1;
					if (!_InRanges(pc,ref idx, cur))
					{
						break;
					}
					goto case Compiler.Any;
				case Compiler.NSet:
					idx = 1;
					if (_InRanges(pc, ref idx,cur))
					{
						break;
					}
					goto case Compiler.Any;
				case Compiler.UCode:
					var str = char.ConvertFromUtf32(cur);
					if (unchecked((int)char.GetUnicodeCategory(str,0) != pc[1]))
					{
						break;
					}
					goto case Compiler.Any;
				case Compiler.NUCode:
					str = char.ConvertFromUtf32(cur);
					if (unchecked((int)char.GetUnicodeCategory(str,0)) == pc[1])
					{
						break;
					}
					goto case Compiler.Any;

				case Compiler.Any:
					if (LexContext.EndOfInput==input.Current)
					{
						break;
					}
					passed = true;
					_EnqueueFiber(ref nextFiberCount, ref nextFibers, new _Fiber(t, t.Index+1, saved), sp+1);

					break;
				case Compiler.Match:
					matched = saved;
					match = pc[1];
							
					// break the for loop:
					i = currentFiberCount;
					break;
							
			}
		}
		if (passed)
		{
			sb.Append(char.ConvertFromUtf32(cur));
			input.Advance();
			if (LexContext.EndOfInput != input.Current)
			{
				var ch1 = unchecked((char)input.Current);
				if (char.IsHighSurrogate(ch1))
				{
					input.Advance();
					if (-1 == input.Advance())
						throw new ExpectingException("Expecting low surrogate in unicode stream. The input source is corrupt or not valid Unicode", input.Line, input.Column, input.Position, input.FileOrUrl);
					++sp;
					var ch2 = unchecked((char)input.Current);
					cur = char.ConvertToUtf32(ch1, ch2);
				}
				else
					cur = ch1;
			}
			else
				cur = -1;
			++sp;
		}
		tmp = currentFibers;
		currentFibers = nextFibers;
		nextFibers = tmp;
		currentFiberCount = nextFiberCount;
		nextFiberCount = 0;
				
	}

	if (null!=matched)
	{
		var start = matched[0];
		// this is actually the point just past the end
		// of the match, but we can treat it as the length
		var len = matched[1];
		input.CaptureBuffer.Append(sb.ToString(start, len - start));
		return match;
	};
	return -1; // error symbol
}
static void _EnqueueFiber(ref int lcount,ref _Fiber[] l, _Fiber t, int sp)
{
	// really shouldn't happen, but maybe it might
	if(l.Length<=lcount)
	{
		var newarr = new _Fiber[l.Length * 2];
		Array.Copy(l, 0, newarr, 0, l.Length);
		l = newarr;
	}
	l[lcount] = t;
	++lcount;
	var pc = t.Program[t.Index];
	switch (pc[0])
	{
		case Compiler.Jmp:
			for (var j = 1; j < pc.Length; j++)
				_EnqueueFiber(ref lcount,ref l, new _Fiber(t.Program, pc[j],t.Saved),sp);
			break;
		case Compiler.Save:
			var slot = pc[1];
			var max = slot > t.Saved.Length ? slot : t.Saved.Length;
			var saved = new int[max];
			for (var i = 0;i<t.Saved.Length;++i)
				saved[i]=t.Saved[i];
			saved[slot] = sp;
			_EnqueueFiber(ref lcount,ref l, new _Fiber(t,t.Index+1, saved), sp);
			break;
	}
}
		
private struct _Fiber
{
	public readonly int[][] Program;
	public readonly int Index;
	public int[] Saved;
	public _Fiber(int[][] program, int index,int[] saved)
	{
		Program = program;
		Index = index;
		Saved = saved;
	}
	public _Fiber(_Fiber fiber, int index,int[] saved)
	{
		Program = fiber.Program;
		Index = index;
		Saved = saved;
	}
}

Real programmers use butterflies

GeneralRe: I might need to optimize this XD Pin
Jörgen Andersson2-Feb-20 23:36
professionalJörgen Andersson2-Feb-20 23:36 
GeneralRe: I might need to optimize this XD Pin
honey the codewitch2-Feb-20 23:57
mvahoney the codewitch2-Feb-20 23:57 
GeneralRe: I might need to optimize this XD Pin
Jörgen Andersson2-Feb-20 23:58
professionalJörgen Andersson2-Feb-20 23:58 
GeneralRe: I might need to optimize this XD Pin
honey the codewitch3-Feb-20 0:12
mvahoney the codewitch3-Feb-20 0:12 
GeneralRe: I might need to optimize this XD Pin
ZTransform2-Feb-20 23:16
ZTransform2-Feb-20 23:16 
GeneralRe: I might need to optimize this XD Pin
honey the codewitch2-Feb-20 23:22
mvahoney the codewitch2-Feb-20 23:22 
GeneralRe: I might need to optimize this XD Pin
Jörgen Andersson2-Feb-20 23:35
professionalJörgen Andersson2-Feb-20 23:35 
NewsThe Rotating Lepton Model Pin
Stylianos Polychroniadis2-Feb-20 4:19
Stylianos Polychroniadis2-Feb-20 4:19 
GeneralRe: The Rotating Lepton Model Pin
BillWoodruff2-Feb-20 4:49
professionalBillWoodruff2-Feb-20 4:49 
GeneralRe: The Rotating Lepton Model Pin
phil.o2-Feb-20 4:54
professionalphil.o2-Feb-20 4:54 
GeneralRe: The Rotating Lepton Model Pin
OriginalGriff2-Feb-20 5:14
mveOriginalGriff2-Feb-20 5:14 
GeneralRe: The Rotating Lepton Model Pin
Mark_Wallace2-Feb-20 6:20
Mark_Wallace2-Feb-20 6:20 
GeneralRe: The Rotating Lepton Model Pin
OriginalGriff2-Feb-20 6:23
mveOriginalGriff2-Feb-20 6:23 
GeneralRe: The Rotating Lepton Model Pin
Mark_Wallace2-Feb-20 6:51
Mark_Wallace2-Feb-20 6:51 
GeneralRe: The Rotating Lepton Model Pin
lopatir2-Feb-20 6:28
lopatir2-Feb-20 6:28 
GeneralRe: The Rotating Lepton Model Pin
Stylianos Polychroniadis2-Feb-20 7:52
Stylianos Polychroniadis2-Feb-20 7:52 
GeneralRe: The Rotating Lepton Model Pin
Mark_Wallace2-Feb-20 9:54
Mark_Wallace2-Feb-20 9:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.