Click here to Skip to main content
15,891,033 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: Apple new amazing feature Pin
Moo v This2-Nov-21 3:17
Moo v This2-Nov-21 3:17 
GeneralRe: Apple new amazing feature Pin
Paul Sanders (the other one)28-Oct-21 4:31
Paul Sanders (the other one)28-Oct-21 4:31 
GeneralRe: Apple new amazing feature Pin
Gary Wheeler28-Oct-21 6:06
Gary Wheeler28-Oct-21 6:06 
GeneralRe: Apple new amazing feature Pin
Paul Sanders (the other one)28-Oct-21 6:28
Paul Sanders (the other one)28-Oct-21 6:28 
GeneralRe: Apple new amazing feature Pin
Gary Wheeler28-Oct-21 6:42
Gary Wheeler28-Oct-21 6:42 
GeneralRe: Apple new amazing feature Pin
Gilles Plante28-Oct-21 6:59
Gilles Plante28-Oct-21 6:59 
GeneralRe: Apple new amazing feature Pin
Luc Pattyn6-Nov-21 9:31
sitebuilderLuc Pattyn6-Nov-21 9:31 
GeneralProbably the most elegant little bit of code I've written in awhile Pin
honey the codewitch26-Oct-21 17:53
mvahoney the codewitch26-Oct-21 17:53 
This bit of code will take an array int[] entries and some text (string or char array or whatever) and tell you whether the text matches the state machine stored in entries

I've effectively packed a DFA state machine into an array of ints, and very efficiently unpack it while matching/walking it because of the way it's encoded. Only downside is encoding the array takes two passes, but the passes are fast. Big Grin | :-D

Here _FetchNextInput(cursor); simply fetches the next UTF32 codepoint off the text input (doing surrogate pair encoding in the process when necessary)

C#
var cursor = text.GetEnumerator();
var ch = _FetchNextInput(cursor);
if (ch == -1) return -1!=entries[0];
var state = 0;
while (ch != -1)
{
    // state starts with accept symbol
    var acc = entries[state++];
    // next is the num of transitions
    var tlen = entries[state++];
    var m = false;
    for (var i = 0; i < tlen; ++i)
    {
        // each transition starts with the destination index
        var tto = entries[state++];
        // next with a packed range length
        var prlen = entries[state++];
        for (var j = 0; j < prlen; ++j)
        {
            // next each packed range
            var tmin = entries[state++];
            var tmax = entries[state++];
            if (ch >= tmin && ch <= tmax)
            {
                ch = _FetchNextInput(cursor);
                if (ch == -1) return -1!=entries[tto];
                state = tto;
                i = tlen;
                m = true;
                break;
            }
        }
    }
    if (!m) return -1!=acc;
}
return false;

Real programmers use butterflies

GeneralRe: Probably the most elegant little bit of code I've written in awhile Pin
Peter_in_278026-Oct-21 20:53
professionalPeter_in_278026-Oct-21 20:53 
GeneralRIP Mark Sanborn Pin
Cp-Coder26-Oct-21 6:39
Cp-Coder26-Oct-21 6:39 
GeneralRe: RIP Mark Sanborn Pin
dandy7226-Oct-21 8:27
dandy7226-Oct-21 8:27 
GeneralRe: RIP Mark Sanborn Pin
Jörgen Andersson26-Oct-21 9:29
professionalJörgen Andersson26-Oct-21 9:29 
GeneralRe: RIP Mark Sanborn Pin
  Forogar  26-Oct-21 9:32
professional  Forogar  26-Oct-21 9:32 
AnswerRe: RIP Mark Sanborn Pin
Member 1532961326-Oct-21 9:51
Member 1532961326-Oct-21 9:51 
GeneralRe: RIP Mark Sanborn Pin
englebart26-Oct-21 10:26
professionalenglebart26-Oct-21 10:26 
QuestionRe: RIP Mark Sanborn Pin
Eddy Vluggen26-Oct-21 12:14
professionalEddy Vluggen26-Oct-21 12:14 
GeneralRe: RIP Mark Sanborn Pin
den2k8826-Oct-21 23:34
professionalden2k8826-Oct-21 23:34 
PraiseI just figured out how to do code synthesis (i think)! Pin
honey the codewitch26-Oct-21 6:03
mvahoney the codewitch26-Oct-21 6:03 
GeneralRe: I just figured out how to do code synthesis (i think)! Pin
PIEBALDconsult26-Oct-21 6:07
mvePIEBALDconsult26-Oct-21 6:07 
GeneralRe: I just figured out how to do code synthesis (i think)! Pin
honey the codewitch26-Oct-21 6:14
mvahoney the codewitch26-Oct-21 6:14 
GeneralRe: I just figured out how to do code synthesis (i think)! Pin
PIEBALDconsult26-Oct-21 6:27
mvePIEBALDconsult26-Oct-21 6:27 
GeneralRe: I just figured out how to do code synthesis (i think)! Pin
Greg Utas26-Oct-21 6:35
professionalGreg Utas26-Oct-21 6:35 
GeneralRe: I just figured out how to do code synthesis (i think)! Pin
honey the codewitch26-Oct-21 7:01
mvahoney the codewitch26-Oct-21 7:01 
GeneralRe: I just figured out how to do code synthesis (i think)! Pin
Jörgen Andersson26-Oct-21 9:32
professionalJörgen Andersson26-Oct-21 9:32 
GeneralRe: I just figured out how to do code synthesis (i think)! Pin
honey the codewitch26-Oct-21 9:34
mvahoney the codewitch26-Oct-21 9:34 

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.