Created
March 24, 2017 13:57
-
-
Save nidev/2b3fbad1c8078600f4994bc17343978d to your computer and use it in GitHub Desktop.
Have you ever imagined array[0.5]? here is the answer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // encoding: utf-8 | |
| import "dart:io"; | |
| enum ExportDirection { | |
| Integer, | |
| Double | |
| } | |
| class HybridIndex { | |
| final int integer; | |
| final double realnum; | |
| final ExportDirection _direction; | |
| HybridIndex({ExportDirection d = ExportDirection.Integer, int integer = 0, double realnum = 0.0}) : | |
| this.integer = integer, | |
| this.realnum = realnum, | |
| this._direction = d; | |
| dynamic export() { | |
| switch (_direction) { | |
| case ExportDirection.Double: | |
| return realnum; | |
| default: | |
| return integer; | |
| } | |
| } | |
| } | |
| class DevilsNest<T extends int> { | |
| List<T> _internal; | |
| int get length { | |
| return _internal.length; | |
| } | |
| DevilsNest() { | |
| _internal = new List<T>(); | |
| } | |
| void add(T a) { | |
| _internal.add(a); | |
| } | |
| void addAll(Iterable<T> i) { | |
| _internal.addAll(i); | |
| } | |
| T operator[](HybridIndex hi) { | |
| var index = hi.export(); | |
| if (index is double && index == 0.5) { | |
| return _internal[index.floor()] ^ _internal[index.ceil()]; | |
| } | |
| return _internal[index]; | |
| } | |
| } | |
| void main(List<String> args) { | |
| var array = new DevilsNest(); | |
| args.forEach((arg) => array.add(int.parse(arg))); | |
| if (array.length < 2) { | |
| print("Program needs more than two integers. halted"); | |
| exit(-1); | |
| } | |
| print("array[0] = ${array[new HybridIndex(d: ExportDirection.Integer, integer: 0)]}"); | |
| print("array[1] = ${array[new HybridIndex(d: ExportDirection.Integer, integer: 1)]}"); | |
| print("array[0.5] = ${array[new HybridIndex(d: ExportDirection.Double, realnum: 0.5)]}"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment