/// <summary> /// Push element to the top of the stack /// </summary> /// <param name="newString">content</param> public void Push(string newString) { lock (_lock) { // First create new StackString object and save it to DB to get a new ID var stackString = new StackString(newString); var record = _db.StackStrings.Add(stackString); _db.SaveChanges(); // Connect it via 2way binding to the top element of the stack according to the order if (_stackDirection == StackDirection.IN_ORDER) { var left = _db.StackStrings .SingleOrDefault(s => s.LeftId == StackString.LEFT_ID); if (left != null) { left.LeftId = stackString.StackStringId; stackString.RightId = left.StackStringId; stackString.LeftId = StackString.LEFT_ID; } } else { var right = _db.StackStrings .SingleOrDefault(s => s.RightId == StackString.RIGHT_ID); if (right != null) { right.RightId = stackString.StackStringId; stackString.LeftId = right.StackStringId; stackString.RightId = StackString.RIGHT_ID; } } // If one of the sides is not connected - this is the first element if (stackString.LeftId == StackString.INVALID_ID || stackString.RightId == StackString.INVALID_ID) { stackString.LeftId = StackString.LEFT_ID; stackString.RightId = StackString.RIGHT_ID; } _db.SaveChanges(); } }
public Token[] Tokenize(string str) { _symbolsStack = new StackString(str); var tokens = new List <Token>(); while (_symbolsStack.TryPeek(out var ch)) { if (char.IsWhiteSpace(ch)) { _symbolsStack.Pop(); continue; } tokens.Add(ReadToken()); } _symbolsStack = null; return(tokens.ToArray()); }