<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Drmaciver's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 17:30:17 GMT</pubDate>
    <description>DZone Snippets: Drmaciver's Code Snippets</description>
    <item>
      <title>Statically enforced range types in scala</title>
      <link>http://snippets.dzone.com/posts/show/4876</link>
      <description>Cute piece of code for statically checking array index accesses. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package ranges;&lt;br /&gt;&lt;br /&gt;class Range(val start : Int, val end : Int){&lt;br /&gt;  if (start &gt;= end) throw new IndexOutOfBoundsException();&lt;br /&gt;&lt;br /&gt;  def checkRange(min : Int, max : Int){&lt;br /&gt;    if (min &lt; start || max &gt; end) throw new IndexOutOfBoundsException();&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  def inBounds(x : Int) : Index = unsafeFromInt(Math.min(end - 1, Math.max(x, start)));&lt;br /&gt;  def checkBounds (x : Int) : Index = if (x &lt; start || x &gt;= end) throw new IndexOutOfBoundsException() else unsafeFromInt(x);&lt;br /&gt;&lt;br /&gt;  private[Range] def unsafeFromInt (i : Int) = Index(i); // Separated into a method for refactoring and unsafe declaration.&lt;br /&gt;&lt;br /&gt;  val range = start until end;&lt;br /&gt;  val length = end - start;&lt;br /&gt;&lt;br /&gt;  val maxIndex = unsafeFromInt(end - 1);&lt;br /&gt;  val minIndex = unsafeFromInt(start);&lt;br /&gt;  val indices = range.map(unsafeFromInt(_));&lt;br /&gt;&lt;br /&gt;  trait Slice[T] extends Iterable[T] {&lt;br /&gt;    def apply(i : Index) : T = unsafeApply(i);     &lt;br /&gt;    def update(i : Index, t : T) = unsafeUpdate(i, t);&lt;br /&gt;&lt;br /&gt;    private[Range] def unsafeApply(i : Int) : T;&lt;br /&gt;    private[Range] def unsafeUpdate(i : Int, t : T) : Unit;&lt;br /&gt;&lt;br /&gt;    def elements = range.elements.map(unsafeApply(_)); &lt;br /&gt;  } &lt;br /&gt;&lt;br /&gt;  class ArraySlice[T](array : Array[T]) extends Slice[T]{&lt;br /&gt;    if (length &lt; end) throw new IndexOutOfBoundsException();&lt;br /&gt;    private[Range] def unsafeApply(i : Int) = array(i);&lt;br /&gt;    private[Range] def unsafeUpdate(i : Int, t : T) : Unit = array(i) = t;&lt;br /&gt;&lt;br /&gt;    override def foreach(f : T =&gt; Unit) = for (val i &lt;- range) array(i);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  class CheckedArray[T] extends Slice[T]{&lt;br /&gt;    private val array = new Array[T](length);&lt;br /&gt;    &lt;br /&gt;    private[Range] def unsafeApply(i : Int) = array(i - start);&lt;br /&gt;    private[Range] def unsafeUpdate(i : Int, t : T) : Unit = array(i - start) = t;&lt;br /&gt;    &lt;br /&gt;    override def foreach(f : T =&gt; Unit) = array.foreach(f);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  implicit def toInteger(i : Index) = i.toInt;&lt;br /&gt;&lt;br /&gt;  case class Index private[Range] (val toInt : Int){&lt;br /&gt;    def max(y : Index) = unsafeFromInt(Math.max(this, y));&lt;br /&gt;    def min(y : Index) = unsafeFromInt(Math.min(this, y));&lt;br /&gt;    def mid(y : Index) = unsafeFromInt((this + y) &gt;&gt;&gt; 1);&lt;br /&gt;    def opposite = unsafeFromInt(end + start - 1 - this);&lt;br /&gt;  }; &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 11 Dec 2007 22:42:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4876</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Creating a circular reference between two objects in Java</title>
      <link>http://snippets.dzone.com/posts/show/4445</link>
      <description>This is in some sense the 'right' way of doing it as far as I can figure. Nothing else I've been able to come up with works even close to as well.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class LazyModules &lt;br /&gt;{&lt;br /&gt;  static int i = 0;&lt;br /&gt;&lt;br /&gt;  static abstract class A{&lt;br /&gt;    abstract B getB();&lt;br /&gt;    int k = i++;&lt;br /&gt;    public String toString() { return "" + k; }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  static abstract class B{&lt;br /&gt;    abstract A getA();&lt;br /&gt;    int k = i++;&lt;br /&gt;    public String toString() { return "" + k; }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static void doStuff(A foo, B bar){&lt;br /&gt;    System.out.println("foo = " + foo);&lt;br /&gt;    System.out.println("foo.b = " + foo.getB());&lt;br /&gt;    System.out.println("bar = " + bar);&lt;br /&gt;    System.out.println("bar.a = " + bar.getA()); &lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static void main(String[] args){&lt;br /&gt;    new Object(){&lt;br /&gt;      final A foo = new A() { B getB() { return bar; }  };&lt;br /&gt;      final B bar = new B() { A getA() { return foo; }  };&lt;br /&gt;&lt;br /&gt;      { doStuff(foo, bar); }&lt;br /&gt;    };&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 22 Aug 2007 22:03:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4445</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Haskell Regular Expression Matcher</title>
      <link>http://snippets.dzone.com/posts/show/4434</link>
      <description>Basic implementation of Regular Expressions based on "Derivatives of Regular Expressions" by Janusz A. Brzozowski (Journal of Association for Computing Machinery, October 1964)&lt;br /&gt;&lt;br /&gt;Not really intended for serious use. Just a proof of concept.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Regexp&lt;br /&gt;where&lt;br /&gt;&lt;br /&gt;import Data.Set (Set)&lt;br /&gt;import Data.Map (Map)&lt;br /&gt;import Monad&lt;br /&gt;import List&lt;br /&gt;import Maybe&lt;br /&gt;import qualified Data.Set as Set&lt;br /&gt;import qualified Data.Map as Map&lt;br /&gt;&lt;br /&gt;data Regexp = &lt;br /&gt;    Zero&lt;br /&gt;  | Match Char       -- matches a single char&lt;br /&gt;  | Not Regexp       -- matches the negation of its argument&lt;br /&gt;  | Prod [Regexp]    -- matches a concatentation of its arguments&lt;br /&gt;  | Sum (Set Regexp) -- matches either of its arguments&lt;br /&gt;  | Star Regexp      -- matches repetitions of its argument (including 0 repetitions)&lt;br /&gt;  deriving (Eq, Ord)&lt;br /&gt;&lt;br /&gt;instance Show Regexp where&lt;br /&gt;  show Zero = "0"&lt;br /&gt;  show (Match c) = [c]&lt;br /&gt;  show (Not x)   = '~' : show x&lt;br /&gt;  show (Prod x) = join . (map show)  $ x&lt;br /&gt;  show (Sum x) = "(" ++ ( join . intersperse "|" . (map show) . Set.toList $ x ) ++ ")"&lt;br /&gt;  show (Star x) = "(" ++ show x ++ ")*" &lt;br /&gt;&lt;br /&gt;-- Flagrant abuse of type classes to allow implicit conversion of datatypes into regular&lt;br /&gt;-- expressions.&lt;br /&gt;class Match a where&lt;br /&gt;  match :: a -&gt; Regexp&lt;br /&gt;&lt;br /&gt;instance Match Char where&lt;br /&gt;  match c = Match c&lt;br /&gt;&lt;br /&gt;instance (Match a) =&gt; Match [a] where&lt;br /&gt;  match = con&lt;br /&gt;&lt;br /&gt;instance Match Regexp where&lt;br /&gt;  match = id&lt;br /&gt;&lt;br /&gt;-- "smart" versions of the constructors, which perform normalisation of the datatype.&lt;br /&gt;-- As long as all regular expressions are built up using these and the match instance&lt;br /&gt;-- for char we can guarantee that structural equality of terms == similarity.&lt;br /&gt;-- This is important to make sure we only generate a finite number of states.&lt;br /&gt;zero :: Regexp &lt;br /&gt;zero = Zero &lt;br /&gt;&lt;br /&gt;one :: Regexp&lt;br /&gt;one = Prod []&lt;br /&gt;&lt;br /&gt;(&lt;+&gt;) :: (Match a, Match b) =&gt; a -&gt; b -&gt; Regexp&lt;br /&gt;x &lt;+&gt; y = &lt;br /&gt;  case (match x, match y) of&lt;br /&gt;    (Zero, b)      -&gt; b&lt;br /&gt;    (a, Zero)      -&gt; a&lt;br /&gt;    (Sum a, Sum b) -&gt; Sum (Set.union a b)&lt;br /&gt;    (Sum a, b)     -&gt; Sum (Set.insert b a)&lt;br /&gt;    (a, Sum b)     -&gt; Sum (Set.insert a b)    &lt;br /&gt;    (a, b)         -&gt; Sum $ Set.fromList [a, b]&lt;br /&gt;  &lt;br /&gt;oneOf :: (Match a) =&gt; [a] -&gt; Regexp&lt;br /&gt;oneOf = foldr (&lt;+&gt;) zero&lt;br /&gt;&lt;br /&gt;(&lt;*&gt;) :: (Match a, Match b) =&gt; a -&gt; b -&gt; Regexp&lt;br /&gt;u &lt;*&gt; v = &lt;br /&gt;  case (match u, match v) of &lt;br /&gt;    (Zero, _)         -&gt; zero&lt;br /&gt;    (_, Zero)         -&gt; zero&lt;br /&gt;    (Prod x, Prod y)  -&gt; Prod (x ++ y)&lt;br /&gt;    (Prod x, y)       -&gt; Prod (x ++ [y])&lt;br /&gt;    (x, Prod y)       -&gt; Prod (x : y)&lt;br /&gt;    (x, y)            -&gt; Prod [x, y]&lt;br /&gt;&lt;br /&gt;con :: (Match a) =&gt; [a] -&gt; Regexp&lt;br /&gt;con = foldr (&lt;*&gt;) one&lt;br /&gt;&lt;br /&gt;neg :: (Match a) =&gt; a -&gt; Regexp&lt;br /&gt;neg x = &lt;br /&gt;  case (match x) of&lt;br /&gt;  (Not y) -&gt; y&lt;br /&gt;  y       -&gt; Not y&lt;br /&gt;&lt;br /&gt;star :: (Match a) =&gt; a -&gt; Regexp&lt;br /&gt;star x =&lt;br /&gt;  case (match x) of&lt;br /&gt;    (Zero)   -&gt; Zero&lt;br /&gt;    (Star y) -&gt; Star y&lt;br /&gt;    y        -&gt; Star y&lt;br /&gt;&lt;br /&gt;-- Returns if the regex matches the empty string.&lt;br /&gt;del :: Regexp -&gt; Bool&lt;br /&gt;del (Zero)    = False&lt;br /&gt;del (Sum x)   = or . map del $ Set.toList x&lt;br /&gt;del (Prod x)  = and . map del $ x&lt;br /&gt;del (Match _) = False&lt;br /&gt;del (Not x)   = not $ del x;&lt;br /&gt;del (Star _)  = True&lt;br /&gt;&lt;br /&gt;-- The derivative of a regular language A with respect to a character&lt;br /&gt;-- c is dA/dc = { s : cs \in A } &lt;br /&gt;diff :: Char -&gt; Regexp -&gt; Regexp&lt;br /&gt;diff _ (Zero)  = zero&lt;br /&gt;diff c (Match d) | (c == d) = one&lt;br /&gt;diff c (Match d) = zero&lt;br /&gt;diff c (Sum x) = oneOf $ (map $ diff c) (Set.toList x)&lt;br /&gt;diff c (Prod []) = zero&lt;br /&gt;diff c (Prod (x:xs)) | del x = (diff c x &lt;*&gt; xs) &lt;+&gt; diff c (Prod xs)&lt;br /&gt;diff c (Prod (x:xs)) = diff c x &lt;*&gt; xs&lt;br /&gt;diff c (Not x) = Not (diff c x)&lt;br /&gt;diff c (Star x) = diff c x &lt;*&gt; Star x&lt;br /&gt;&lt;br /&gt;flattenSet :: (Ord a) =&gt; Set (Set a) -&gt; Set a&lt;br /&gt;flattenSet = Set.fold Set.union Set.empty&lt;br /&gt;&lt;br /&gt;(/&gt;&gt;=) :: (Ord a, Ord b) =&gt; Set a -&gt; (a -&gt; Set b) -&gt; Set b&lt;br /&gt;x /&gt;&gt;= f = flattenSet (Set.map f x)&lt;br /&gt;&lt;br /&gt;-- The alphabet of all characters that appear in this regexp&lt;br /&gt;alphabet :: Regexp -&gt; Set Char&lt;br /&gt;alphabet (Zero) = Set.empty&lt;br /&gt;alphabet (Sum x) = flattenSet (Set.map alphabet x) &lt;br /&gt;alphabet (Prod x) = Set.unions $ map alphabet x&lt;br /&gt;alphabet (Not x) = alphabet x&lt;br /&gt;alphabet (Star x) = alphabet x&lt;br /&gt;alphabet (Match c) = Set.singleton c&lt;br /&gt;&lt;br /&gt;-- Set of all derivatives of a regular expression (including itself, and higher order derivatives).&lt;br /&gt;derivatives :: Regexp -&gt; [Regexp]&lt;br /&gt;derivatives exp = Set.toList $ enlarge (Set.singleton exp) (Set.singleton exp) &lt;br /&gt;  where&lt;br /&gt;    alpha = alphabet exp&lt;br /&gt;    firstDerivatives x = Set.map (`diff` x) alpha &lt;br /&gt;    enlarge :: Set Regexp -&gt; Set Regexp -&gt; Set Regexp&lt;br /&gt;    enlarge new found = &lt;br /&gt;      if Set.null new&lt;br /&gt;        then found&lt;br /&gt;        else&lt;br /&gt;          let nextNew   = (new /&gt;&gt;= firstDerivatives) Set.\\ found&lt;br /&gt;              nextFound = found `Set.union` nextNew&lt;br /&gt;          in enlarge nextNew nextFound&lt;br /&gt;&lt;br /&gt;-- A simple finite state machine type &lt;br /&gt;data FSM = State { transitions :: (Map Char FSM), isFinal :: Bool } &lt;br /&gt;&lt;br /&gt;-- Converts a Regexp into a finite state machine by using the derivatives&lt;br /&gt;-- with respect to specific characters as the transitions. Essentially at &lt;br /&gt;-- each stage we build up a regular expression that the remaining characters&lt;br /&gt;-- have to match. Due to Cunning Mathematics, only finitely many such regular&lt;br /&gt;-- expressions (up to similarity) result.&lt;br /&gt;compile :: Regexp -&gt; FSM&lt;br /&gt;compile x = fromJust $ Map.lookup x states&lt;br /&gt;  where&lt;br /&gt;    states :: Map Regexp FSM&lt;br /&gt;    states = Map.fromList $&lt;br /&gt;      do re &lt;- derivatives x -- Totally gratuitious use of list monad. :)&lt;br /&gt;         let trans = do c &lt;- Set.toList $ alphabet re&lt;br /&gt;                        let d = diff c re&lt;br /&gt;                        return (c, fromJust $ Map.lookup d states)&lt;br /&gt;         let state = State (Map.fromList trans) (del re) &lt;br /&gt;         return (re, state) &lt;br /&gt;&lt;br /&gt;runFSM :: FSM -&gt; String -&gt; Bool&lt;br /&gt;runFSM x []     = isFinal x&lt;br /&gt;runFSM x (c:cs) = case (Map.lookup c $ transitions x) of&lt;br /&gt;                    Nothing -&gt; False&lt;br /&gt;                    Just y  -&gt; runFSM y cs&lt;br /&gt;               &lt;br /&gt;matches :: (Match a) =&gt; String -&gt; a -&gt; Bool&lt;br /&gt;matches cs exp = runFSM (compile $  match exp) cs&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 19 Aug 2007 20:57:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4434</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Simple CharMap based on a Radix binary tree</title>
      <link>http://snippets.dzone.com/posts/show/4383</link>
      <description>Very simple Java implementation of a map from characters to objects based on a radix tree. &lt;br /&gt;&lt;br /&gt;It's not actually that fast, as I've not bothered to optimise it at all (a brief attempt didn't work very well and I lost interest after that). It seems to be a little slower than a HashMap using boxed Characters and a little faster than a TreeMap (when the JIT gets up to speed anyway. Before the JIT has done any optimisation the hashmap is 2 or 3 times faster). I might at some point implement a HashMap using unboxed characters for keys to see what kind of performance improvement it gives. &lt;br /&gt;&lt;br /&gt;Using a large array is of course faster than all of the options by an order of magnitude, but has the downside of being a bit too large to be useful. :-) &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class RadixCharMap&lt;T&gt;&lt;br /&gt;{&lt;br /&gt;    private RadixCharMap&lt;T&gt; map0; &lt;br /&gt;    private RadixCharMap&lt;T&gt; map1;&lt;br /&gt;    private T value;&lt;br /&gt;    &lt;br /&gt;    private RadixCharMap&lt;T&gt; getMap(int i){&lt;br /&gt;        if (i &gt; 0) return map1 == null ? (map1 = new RadixCharMap&lt;T&gt;()) : map1;&lt;br /&gt;        else return map0 == null ? (map0 = new RadixCharMap&lt;T&gt;()) : map0;}    &lt;br /&gt;    &lt;br /&gt;    public RadixCharMap&lt;T&gt; put(char key, T value){&lt;br /&gt;        if (key == '\0') this.value = value;&lt;br /&gt;        else getMap(key &amp; 1).put((char)(key &gt;&gt; 1), value);&lt;br /&gt;        return this;}&lt;br /&gt;    &lt;br /&gt;    public T get(char key){&lt;br /&gt;        RadixCharMap current = this;&lt;br /&gt;        if (key == '\0') return this.value;&lt;br /&gt;        else return getMap(key &amp; 1).get((char)(key &gt;&gt; 1));}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 02 Aug 2007 15:07:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4383</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Simple Haskell script for word counting</title>
      <link>http://snippets.dzone.com/posts/show/4263</link>
      <description>This is just a simple piece of code I put together to play with some Haskell when I realised I've not been writing nearly enough of the stuff. &lt;br /&gt;&lt;br /&gt;It reads text from stdin and prints the words it finds together with how many times each one occurred.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Main&lt;br /&gt;where&lt;br /&gt;&lt;br /&gt;import List&lt;br /&gt;import Control.Arrow&lt;br /&gt;&lt;br /&gt;type Comparator a = (a -&gt; a -&gt; Ordering)&lt;br /&gt;&lt;br /&gt;ascending :: (Ord a) =&gt; (b -&gt; a) -&gt; Comparator b&lt;br /&gt;ascending f x y = compare (f x) (f y)&lt;br /&gt;&lt;br /&gt;descending :: (Ord a) =&gt; (b -&gt; a) -&gt; Comparator b&lt;br /&gt;descending = flip . ascending&lt;br /&gt;&lt;br /&gt;secondary :: Comparator a -&gt; Comparator a -&gt; Comparator a&lt;br /&gt;secondary f g x y = case f x y of {&lt;br /&gt;                    EQ -&gt; g x y;&lt;br /&gt;                    z  -&gt; z; }&lt;br /&gt;&lt;br /&gt;-- Returns a list of unique elements together with their frequency. Listed in decreasing order of frequency, followed by&lt;br /&gt;increasing order of the elements.&lt;br /&gt;count :: (Ord a) =&gt; [a] -&gt; [(a, Int)]&lt;br /&gt;count = map (head &amp;&amp;&amp; length) . sortBy (descending length `secondary` ascending head) . group . sort&lt;br /&gt;&lt;br /&gt;main :: IO ()&lt;br /&gt;main = interact $ unlines . map (\(x, y) -&gt; (take 20 $ x ++ repeat ' ')  ++ " : " ++ show y) . count . words&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 13:52:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4263</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Putlines in Haskell</title>
      <link>http://snippets.dzone.com/posts/show/3997</link>
      <description>This is an illustrative example of &lt;br /&gt;&lt;br /&gt;a) Point free style&lt;br /&gt;b) How Haskell IO works.&lt;br /&gt;&lt;br /&gt;If you don't understand Haskell IO, it might be helpful to try and unpick the definition here to see how it works. :-)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import System&lt;br /&gt;&lt;br /&gt;main :: IO ()&lt;br /&gt;main = getArgs &gt;&gt;= sequence_ . (map putStrLn) &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 12 May 2007 15:17:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3997</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Goto in Java</title>
      <link>http://snippets.dzone.com/posts/show/3812</link>
      <description>After my C finite state machine, I figured I'd sink to new levels of depravity by figuring out how to write GOTO in Java. Here's the result.&lt;br /&gt;&lt;br /&gt;Usual disclaimer about "if you use this code then the baby Jesus will cut kittens".&lt;br /&gt;&lt;br /&gt;Update: Thanks to roots_ on freenode ##java for the suggestion of using a do { } while(false) instead, and cybereal for pointing out that I didn't need the label.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Goto&lt;br /&gt;{&lt;br /&gt;    public static int END = Integer.MAX_VALUE;&lt;br /&gt;&lt;br /&gt;    public static void main(String[] args)&lt;br /&gt;    {&lt;br /&gt;        int _goto = 0;&lt;br /&gt;&lt;br /&gt;        do&lt;br /&gt;        {&lt;br /&gt;            switch(_goto)&lt;br /&gt;            {&lt;br /&gt;                case 0:&lt;br /&gt;                case 1:&lt;br /&gt;                    System.out.println("Foo");&lt;br /&gt;                    _goto = 3;&lt;br /&gt;                    continue;&lt;br /&gt;                &lt;br /&gt;                case 2:&lt;br /&gt;                     System.out.println("Baz");&lt;br /&gt;                    _goto = END;&lt;br /&gt;                    continue;&lt;br /&gt;                case 3:&lt;br /&gt;                     System.out.println("Bar");&lt;br /&gt;                    _goto = 2;&lt;br /&gt;                    continue;&lt;br /&gt;             }&lt;br /&gt;        } while(false)&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 13 Apr 2007 08:01:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3812</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Simple finite state machine in C</title>
      <link>http://snippets.dzone.com/posts/show/3793</link>
      <description>I'm vaguely plotting a finite state machine -&gt; C compiler (as toy code, not for a serious project - I know there are plenty already out there), so thought I'd write a sample one by hand to see how it should look. Here's the code for it.&lt;br /&gt;&lt;br /&gt;This checks if stdin contains the phrase 'foo' or 'bar'&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;{&lt;br /&gt;    int c;&lt;br /&gt;&lt;br /&gt;    START: &lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'f' : goto F;&lt;br /&gt;            case 'b' : goto B;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default: goto START; }&lt;br /&gt;&lt;br /&gt;    F:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'o' : goto FO;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;    &lt;br /&gt;    FO:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'o' : goto SUCCESS;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;&lt;br /&gt;    B:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'a' : goto BA;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;&lt;br /&gt;    BA:&lt;br /&gt;        switch(c = getchar()){&lt;br /&gt;            case 'r' : goto SUCCESS;&lt;br /&gt;            case EOF : goto FAIL;&lt;br /&gt;            default  : goto START;}&lt;br /&gt;&lt;br /&gt;    FAIL: &lt;br /&gt;        printf("Does not match.\n");&lt;br /&gt;        return;&lt;br /&gt;    SUCCESS:&lt;br /&gt;        printf("Matches.\n");&lt;br /&gt;        return;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Apr 2007 17:28:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3793</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Non-code snippet: Licensing</title>
      <link>http://snippets.dzone.com/posts/show/3766</link>
      <description>I've just realised that I should really be worrying about licensing more than I do. &lt;br /&gt;&lt;br /&gt;So, in the unlikely event that you want to use any of my code on here, it should be considered to be released under a &lt;a href="http://sam.zoy.org/wtfpl/"&gt;WTFPL&lt;/a&gt;. Do what you please with it. </description>
      <pubDate>Thu, 05 Apr 2007 07:30:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3766</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Class for faking Using in Java</title>
      <link>http://snippets.dzone.com/posts/show/3744</link>
      <description>This is a class for faking the 'using' syntax in Java. The idea is that you subclass it to provide your resource initialisation and finalisation code, providing protected final methods for resource access you don't want to be publically available. Users then create an anonymous inner class that overrides the body() method and has access to these resources only within the method. Sensitive resources should throw an exception if invoked when isLive returns false. &lt;br /&gt;&lt;br /&gt;Now updated to include significantly less evil. (It no longer needs the _return method...)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public abstract class Use&lt;T&gt;&lt;br /&gt;{&lt;br /&gt;        private boolean live = false;&lt;br /&gt;&lt;br /&gt;        protected final void isLive()&lt;br /&gt;        {&lt;br /&gt;            return live;&lt;br /&gt;        } &lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * Override this to provide initialisation code for this Use class. This will always be called before&lt;br /&gt;	 * the body is executed.&lt;br /&gt;	 */&lt;br /&gt;	protected void start()&lt;br /&gt;	{&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * Override this to provide finalisation code for this Use class. This will always be called at the end&lt;br /&gt;	 * of use() execution.&lt;br /&gt;	 */&lt;br /&gt;	protected void finish()&lt;br /&gt;	{&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * The body of the code to execute. Returning a value from this should execute _return(T value).&lt;br /&gt;         */&lt;br /&gt;	protected abstract T body();&lt;br /&gt;&lt;br /&gt;	/**&lt;br /&gt;	 * Returns body(), or null if this was never invoked. This will call&lt;br /&gt;	 * all neccessary initialisation and finalisation code.&lt;br /&gt; 	 */&lt;br /&gt;	public final T use()&lt;br /&gt;	{&lt;br /&gt;		try&lt;br /&gt;		{&lt;br /&gt;			start();&lt;br /&gt;                        live = true;&lt;br /&gt;&lt;br /&gt;			return body();&lt;br /&gt;		}&lt;br /&gt;		finally&lt;br /&gt;		{&lt;br /&gt;                        live = false;&lt;br /&gt;			finish();&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 30 Mar 2007 11:28:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3744</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
  </channel>
</rss>
