 
 import Swift
 
-public struct Pixmap<Element> {
+// MARK: Utils
+
+internal class _Buffer<Element>: ManagedBuffer<Int,Element> {
+  deinit {
+    self.withUnsafeMutablePointerToElements { $0.destroy(self.value) }
+  }
+}
+
+internal extension _Buffer {
+  internal func clone() -> _Buffer<Element> {
+    return self.withUnsafeMutablePointerToElements { clonedElements in
+      return _Buffer.create(self.allocatedElementCount) { newBuffer in
+        newBuffer.withUnsafeMutablePointerToElements { newElements in
+          newElements.initializeFrom(clonedElements, count: self.value)
+        }
+
+        return self.value
+      } as! _Buffer
+    }
+  }
+
+  internal func resize(newSize: Int) -> _Buffer<Element> {
+    return self.withUnsafeMutablePointerToElements { elements in
+      return _Buffer.create(newSize) { newBuffer in
+        defer {
+          // Tell the old buffer that it no longer
+          // has any elements to manage.
+          self.value = 0
+        }
+
+        newBuffer.withUnsafeMutablePointerToElements { newElems in
+          newElems.moveInitializeFrom(elements, count: self.value)
+        }
+
+        return self.value
+      } as! _Buffer
+    }
+  }
+}
+
+// MARK: Implementation
+
+public struct Pixmap<Pixel> {
   // Using UnsafeMutablePointer<Element> increases perfomace by 0.3 seconds,
-  // but I am to lazy to implement efficient deallocator for allocated memory.
-  internal private(set) var data: Array<Element>
+  // but I (contributor) am to lazy to implement efficient deallocator for allocated memory.
+  internal private(set) var buffer: _Buffer<Pixel>
   public internal(set) var width, height: Int
 
-  public init(width: Int, height: Int, fillValue: Element) {
+  public init(width: Int, height: Int, fillValue: Pixel) {
     self.width = width
     self.height = height
-    // AutoreleasingUnsafeMutablePointer(UnsafeMutablePointer.alloc(width * height))
-    self.data = Array(count: width * height, repeatedValue: fillValue)
-  }
-
-  // internal func isIndexValid(x: Int, _ y: Int) -> Bool {
-  //     return x >= 0 && x < w && y >= 0 && y < h
-  // }
-
-  public subscript(x: Int, y: Int) -> Element {
+    self.buffer = _Buffer.create(width * height) { _ in 0 } as! _Buffer
+  }
+}
+
+extension Pixmap: MutableCollectionType {
+  public var startIndex: Int { return 0 }
+  public var endIndex: Int { return self.buffer.value }
+
+  /// Return element at `position` from the underlying storage.
+  public subscript(position: Int) -> Pixel {
     get {
-      // precondition(self.isIndexValid(x, y), "Index out-of-bounds")
-      return data[x * y + y]
+      return self.buffer.withUnsafeMutablePointerToElements { $0[position] }
     }
 
     mutating set {
-      // precondition(self.isIndexValid(x,y), "Index out-of-bounds")
-      data[x * y + y] = newValue
+      if !isUniquelyReferenced(&self.buffer) {
+        self.buffer = buffer.clone()
+      }
+
+      self.buffer.withUnsafeMutablePointerToElements { $0[position] = newValue }
+    }
+  }
+}
+
+public extension Pixmap {
+  /// Return row at `position` from the underlying storage.
+  public subscript(row position: Int) -> Pixmap.SubSequence {
+    get {
+      let from = self.width * position
+      let to   = self.width + from
+
+      return self[from..<to]
+    }
+  }
+
+  public subscript(x: Int, y: Int) -> Pixel {
+    get {
+      return self[row: x][y]
+    }
+
+    mutating set {
+      let position = (self.width * x) + y
+      self[position] = newValue
     }
   }
 }
 
 public protocol CanvasType {
-  mutating func setPixel(x: Int, _ y: Int, color: UInt32)
-}
-
-public struct Canvas: CanvasType {
-  public internal(set) var pixmap: Pixmap<UInt32>
-
-  public init(width: Int, height: Int, fillValue: UInt32) {
-    self.pixmap = Pixmap<UInt32>(width: width, height: height, fillValue: fillValue)
-  }
-
-  public mutating func setPixel(x: Int, _ y: Int, color: UInt32) {
-    pixmap[x, y] = color
-  }
-}
+  typealias Value
+  subscript(x: Int, y: Int) -> Value { get mutating set }
+  mutating func setPixel(x: Int, _ y: Int, color: Value)
+}
+
+extension CanvasType {
+  public mutating func setPixel(x: Int, _ y: Int, color: Value) {
+    self[x, y] = color
+  }
+}
+
+extension Pixmap: CanvasType {}
+
+typealias Canvas = Pixmap<UInt32>
 
 public struct Vector3 {
   public var x, y, z: Int
 
+  public init(x: Int = 0, y: Int = 0, z: Int = 0) {
+    self.x = x
+    self.y = y
+    self.z = z
+  }
+
   public subscript(position: Int) -> Int {
     get {
-      //precondition(i >= 0 && i < 3, "Index out-of-bounds")
       switch position {
       case 0: return self.x
       case 1: return self.y
       case 2: return self.z
-      default: return 0
+      default: fatalError("Index out-of-bounds")
       }
     }
 
     mutating set {
-      //precondition(i >= 0 && i < 3, "Index out-of-bounds")
       switch position {
       case 0: self.x = newValue
       case 1: self.y = newValue
       case 2: self.z = newValue
-      default: break
-      }
-    }
-  }
-}
-
-public func ==(left: Vector3, right: Vector3) -> Bool {
-  return (left.x == right.x) && (left.y == right.y) && (left.z == right.z)
-}
-
-public func !=(left: Vector3, right: Vector3) -> Bool {
-  return !(left == right)
-}
-
-public struct RasterState {
-  public var step, d: Vector3
-  public var majorAxis: Int
-
-  public init() {
-    self.step = Vector3(x: 0, y: 0, z: 0)
-    self.d = Vector3(x: 0, y: 0, z: 0)
-    self.majorAxis = 0
-  }
+      default: fatalError("Index out-of-bounds")
+      }
+    }
+  }
+}
+
+public func ==(lhs: Vector3, rhs: Vector3) -> Bool {
+  return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.z == rhs.z)
+}
+
+public func !=(lhs: Vector3, rhs: Vector3) -> Bool {
+  return !(lhs == rhs)
 }
 
 public struct LineRaster {
   public var from: Vector3
   public internal(set) var to: Vector3
-  public var state: RasterState?
+  internal private(set) var state: Optional<LineRaster.State>
+
+  internal struct State {
+    internal var step, d: Vector3
+    internal var majorAxis: Int
+
+    internal init(step: Vector3 = Vector3(), d: Vector3 = Vector3(), majorAxis: Int = 0) {
+      self.majorAxis = majorAxis
+      self.step = step
+      self.d = d
+    }
+  }
 
   public init(from: Vector3, to: Vector3) {
     self.from = from
     self.to = to
   }
 
-  public mutating func nextPoint() -> Vector3? {
-    // WARNING: `if var _ = _ {}` and `guard var _ = _ else {}`
-    // patterns will be deprecated in the future Swift versions.
-    guard var state = self.state else {
-      var state = RasterState()
+  @inline(__always)
+  public mutating func nextPoint() -> Optional<Vector3> {
+    var state = self.state ?? LineRaster.State()
+    defer {
+      self.state = state
+    }
+
+    switch self.state {
+    case .None:
       var max = 0
       for i in 0..<3 {
-        let d = self.to[i] - self.from[i]
+        var d = self.to[i] - self.from[i]
         state.step[i] = d > 0 ? 1 : -1
 
-        let da = abs(d)
-        if da > max {
-          max = da
+        d = abs(d)
+        if d > max {
+          max = d
           state.majorAxis = i
         }
       }
-      self.state = state
-      return self.from
-    }
-
-    guard self.from != self.to else { return nil }
-
-    let calsResidualSteps = { axis in abs(self.to[axis] - self.from[axis]) }
-
-    self.from[state.majorAxis] += state.step[state.majorAxis]
-    let rsBase = calsResidualSteps(state.majorAxis)
-    for i in 0..<3 where i != state.majorAxis {
-      let rs = calsResidualSteps(i)
-
-      if rs > 0 {
-        state.d[i] += rs
-        if state.d[i] >= rsBase {
-          state.d[i] -= rsBase
-          self.from[i] += state.step[i]
+
+    case .Some(_):
+      guard self.from != self.to else { return nil }
+
+      @inline(__always)
+      func calsResidualSteps(axis: Int) -> Int {
+        return abs(self.to[axis] - self.from[axis])
+      }
+
+      self.from[state.majorAxis] += state.step[state.majorAxis]
+      let base = calsResidualSteps(state.majorAxis)
+      for i in 0..<3 where i != state.majorAxis {
+        let rs = calsResidualSteps(i)
+
+        if rs > 0 {
+          state.d[i] += rs
+          if state.d[i] >= base {
+            state.d[i] -= base
+            self.from[i] += state.step[i]
+          }
         }
       }
     }


for point in raster {
  let color = UInt32.max
  canvas.setPixel(point.x, point.y, color: color)
  print("Swift: point: x: \(point.x), y: \(point.y), z:\(point.z), color: #\(String(color, radix: 16))")
}

// var myCanvas: CanvasType = canvas
// for _ in 0..<1000000 {
//   testCode(myCanvas)
// }

  let raster = LineRaster(from: a, to: b)
  for point in raster {
    let color = UInt32.max
    canvas[point.x, point.y] = color
  }
}

internal func testCodeInout(inout canvas: Canvas) {
  let a = Vector3(x: 0, y:0, z:0)
  let b = Vector3(x: 50, y:55, z:-20)
  let raster = LineRaster(from: a, to: b)
  for point in raster {
    let color = UInt32.max
    canvas[point.x, point.y] = color
  }
}

internal func testCodeGeneric<T: CanvasType where T.Value == UInt32>(canvas: T) {
  var canvas = canvas

  let a = Vector3(x: 0, y:0, z:0)
