text:
/**
@module ember
@submodule ember-runtime
*/

// ..........................................................
// HELPERS
//

import Ember from 'ember-metal/core';
import { get } from 'ember-metal/property_get';
import { set } from 'ember-metal/property_set';
import {
  Mixin,
  aliasMethod
} from 'ember-metal/mixin';
import { computed } from 'ember-metal/computed';
import {
  propertyWillChange,
  propertyDidChange
} from 'ember-metal/property_events';
import {
  addListener,
  removeListener,
  sendEvent,
  hasListeners
} from 'ember-metal/events';
import compare from 'ember-runtime/compare';

var contexts = [];

function popCtx() {
  return contexts.length === 0 ? {} : contexts.pop();
}

function pushCtx(ctx) {
  contexts.push(ctx);
  return null;
}

function iter(key, value) {
  var valueProvided = arguments.length === 2;

  function i(item) {
    var cur = get(item, key);
    return valueProvided ? value === cur : !!cur;
  }

  return i;
}

/**
  This mixin defines the common interface implemented by enumerable objects
  in Ember. Most of these methods follow the standard Array iteration
  API defined up to JavaScript 1.8 (excluding language-specific features that
  cannot be emulated in older versions of JavaScript).

  This mixin is applied automatically to the Array class on page load, so you
  can use any of these methods on simple arrays. If Array already implements
  one of these methods, the mixin will not override them.

  ## Writing Your Own Enumerable

  To make your own custom class enumerable, you need two items:

  1. You must have a length property. This property should change whenever
     the number of items in your enumerable object changes. If you use this
     with an `Ember.Object` subclass, you should be sure to change the length
     property using `set().`

  2. You must implement `nextObject().` See documentation.

  Once you have these two methods implemented, apply the `Ember.Enumerable` mixin
  to your class and you will be able to enumerate the contents of your object
  like any other collection.

  ## Using Ember Enumeration with Other Libraries

  Many other libraries provide some kind of iterator or enumeration like
  facility. This is often where the most common API conflicts occur.
  Ember's API is designed to be as friendly as possible with other
  libraries by implementing only methods that mostly correspond to the
  JavaScript 1.8 API.

  @class Enumerable
  @namespace Ember
  @since Ember 0.9
  @private
*/
export default Mixin.create({

  /**
    __Required.__ You must implement this method to apply this mixin.

    Implement this method to make your class enumerable.

    This method will be called repeatedly during enumeration. The index value
    will always begin with 0 and increment monotonically. You don't have to
    rely on the index value to determine what object to return, but you should
    always check the value and start from the beginning when you see the
    requested index is 0.

    The `previousObject` is the object that was returned from the last call
    to `nextObject` for the current iteration. This is a useful way to
    manage iteration if you are tracing a linked list, for example.

    Finally the context parameter will always contain a hash you can use as
    a "scratchpad" to maintain any other state you need in order to iterate
    properly. The context object is reused and is not reset between
    iterations so make sure you setup the context with a fresh state whenever
    the index parameter is 0.

    Generally iterators will continue to call `nextObject` until the index
    reaches the current length-1. If you run out of data before this
    time for some reason, you should simply return undefined.

    The default implementation of this method simply looks up the index.
    This works great on any Array-like objects.

    @method nextObject
    @param {Number} index the current index of the iteration
    @param {Object} previousObject the value returned by the last call to
      `nextObject`.
    @param {Object} context a context object you can use to maintain state.
    @return {Object} the next object in the iteration or undefined
    @private
  */
  nextObject: null,

  /**
    Helper method returns the first object from a collection. This is usually
    used by bindings and other parts of the framework to extract a single
    object if the enumerable contains only one item.

    If you override this method, you should implement it so that it will
    always return the same value each time it is called. If your enumerable
    contains only one object, this method should always return that object.
    If your enumerable is empty, this method should return `undefined`.

    ```javascript
    var arr = ['a', 'b', 'c'];
    arr.get('firstObject');  // 'a'

    var arr = [];
    arr.get('firstObject');  // undefined
    ```

    @property firstObject
    @return {Object} the object or undefined
    @public
  */
  firstObject: computed('[]', function() {
    if (get(this, 'length') === 0) {
      return undefined;
    }

    // handle generic enumerables
    var context = popCtx();
    var ret = this.nextObject(0, null, context);

    pushCtx(context);

    return ret;
  }),

  /**
    Helper method returns the last object from a collection. If your enumerable
    contains only one object, this method should always return that object.
    If your enumerable is empty, this method should return `undefined`.

    ```javascript
    var arr = ['a', 'b', 'c'];
    arr.get('lastObject');  // 'c'

    var arr = [];
    arr.get('lastObject');  // undefined
    ```

    @property lastObject
    @return {Object} the last object or undefined
    @public
  */
  lastObject: computed('[]', function() {
    var len = get(this, 'length');

    if (len === 0) {
      return undefined;
    }

    var context = popCtx();
    var idx = 0;
    var last = null;
    var cur;

    do {
      last = cur;
      cur = this.nextObject(idx++, last, context);
    } while (cur !== undefined);

    pushCtx(context);

    return last;
  }),

  /**
    Returns `true` if the passed object can be found in the receiver. The
    default version will iterate through the enumerable until the object
    is found. You may want to override this with a more efficient version.

    ```javascript
    var arr = ['a', 'b', 'c'];

    arr.contains('a'); // true
    arr.contains('z'); // false
    ```

    @method contains
    @param {Object} obj The object to search for.
    @return {Boolean} `true` if object is found in enumerable.
    @public
  */
  contains(obj) {
    var found = this.find(function(item) {
      return item === obj;
    });

    return found !== undefined;
  },

  /**
    Iterates through the enumerable, calling the passed function on each
    item. This method corresponds to the `forEach()` method defined in
    JavaScript 1.6.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as `this` on the context. This is a good way
    to give your iterator function access to the current object.

    @method forEach
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Object} receiver
    @private
  */
  forEach(callback, target) {
    if (typeof callback !== 'function') {
      throw new TypeError();
    }

    var context = popCtx();
    var len = get(this, 'length');
    var last = null;

    if (target === undefined) {
      target = null;
    }

    for (var idx = 0; idx < len; idx++) {
      var next = this.nextObject(idx, last, context);
      callback.call(target, next, idx, this);
      last = next;
    }

    last = null;
    context = pushCtx(context);

    return this;
  },

  /**
    Alias for `mapBy`

    @method getEach
    @param {String} key name of the property
    @return {Array} The mapped array.
    @public
  */
  getEach: aliasMethod('mapBy'),

  /**
    Sets the value on the named property for each member. This is more
    efficient than using other methods defined on this helper. If the object
    implements Ember.Observable, the value will be changed to `set(),` otherwise
    it will be set directly. `null` objects are skipped.

    @method setEach
    @param {String} key The key to set
    @param {Object} value The object to set
    @return {Object} receiver
    @public
  */
  setEach(key, value) {
    return this.forEach(function(item) {
      set(item, key, value);
    });
  },

  /**
    Maps all of the items in the enumeration to another value, returning
    a new array. This method corresponds to `map()` defined in JavaScript 1.6.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    It should return the mapped value.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as `this` on the context. This is a good way
    to give your iterator function access to the current object.

    @method map
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Array} The mapped array.
    @public
  */
  map(callback, target) {
    var ret = Ember.A();

    this.forEach(function(x, idx, i) {
      ret[idx] = callback.call(target, x, idx, i);
    });

    return ret;
  },

  /**
    Similar to map, this specialized function returns the value of the named
    property on all items in the enumeration.

    @method mapBy
    @param {String} key name of the property
    @return {Array} The mapped array.
    @public
  */
  mapBy(key) {
    return this.map(function(next) {
      return get(next, key);
    });
  },

  /**
    Returns an array with all of the items in the enumeration that the passed
    function returns true for. This method corresponds to `filter()` defined in
    JavaScript 1.6.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    It should return `true` to include the item in the results, `false`
    otherwise.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as `this` on the context. This is a good way
    to give your iterator function access to the current object.

    @method filter
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Array} A filtered array.
    @public
  */
  filter(callback, target) {
    var ret = Ember.A();

    this.forEach(function(x, idx, i) {
      if (callback.call(target, x, idx, i)) {
        ret.push(x);
      }
    });

    return ret;
  },

  /**
    Returns an array with all of the items in the enumeration where the passed
    function returns false. This method is the inverse of filter().

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - *item* is the current item in the iteration.
    - *index* is the current index in the iteration
    - *enumerable* is the enumerable object itself.

    It should return a falsey value to include the item in the results.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as "this" on the context. This is a good way
    to give your iterator function access to the current object.

    @method reject
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Array} A rejected array.
    @public
  */
  reject(callback, target) {
    return this.filter(function() {
      return !(callback.apply(target, arguments));
    });
  },

  /**
    Returns an array with just the items with the matched property. You
    can pass an optional second argument with the target value. Otherwise
    this will match any property that evaluates to `true`.

    @method filterBy
    @param {String} key the property to test
    @param {*} [value] optional value to test against.
    @return {Array} filtered array
    @public
  */
  filterBy(key, value) {
    return this.filter(iter.apply(this, arguments));
  },

  /**
    Returns an array with the items that do not have truthy values for
    key.  You can pass an optional second argument with the target value.  Otherwise
    this will match any property that evaluates to false.

    @method rejectBy
    @param {String} key the property to test
    @param {String} [value] optional value to test against.
    @return {Array} rejected array
    @public
  */
  rejectBy(key, value) {
    var exactValue = function(item) {
      return get(item, key) === value;
    };

    var hasValue = function(item) {
      return !!get(item, key);
    };

    var use = (arguments.length === 2 ? exactValue : hasValue);

    return this.reject(use);
  },

  /**
    Returns an array with the items that do not have truthy values for
    key.  You can pass an optional second argument with the target value.  Otherwise
    this will match any property that evaluates to false.

    @method rejectProperty
    @param {String} key the property to test
    @param {String} [value] optional value to test against.
    @return {Array} rejected array
    @deprecated Use `rejectBy` instead
    @private
  */
  rejectProperty: aliasMethod('rejectBy'),

  /**
    Returns the first item in the array for which the callback returns true.
    This method works similar to the `filter()` method defined in JavaScript 1.6
    except that it will stop working on the array once a match is found.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    It should return the `true` to include the item in the results, `false`
    otherwise.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as `this` on the context. This is a good way
    to give your iterator function access to the current object.

    @method find
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Object} Found item or `undefined`.
    @public
  */
  find(callback, target) {
    var len = get(this, 'length');

    if (target === undefined) {
      target = null;
    }

    var context = popCtx();
    var found = false;
    var last = null;
    var next, ret;

    for (var idx = 0; idx < len && !found; idx++) {
      next = this.nextObject(idx, last, context);

      if (found = callback.call(target, next, idx, this)) {
        ret = next;
      }

      last = next;
    }

    next = last = null;
    context = pushCtx(context);

    return ret;
  },

  /**
    Returns the first item with a property matching the passed value. You
    can pass an optional second argument with the target value. Otherwise
    this will match any property that evaluates to `true`.

    This method works much like the more generic `find()` method.

    @method findBy
    @param {String} key the property to test
    @param {String} [value] optional value to test against.
    @return {Object} found item or `undefined`
    @public
  */
  findBy(key, value) {
    return this.find(iter.apply(this, arguments));
  },

  /**
    Returns `true` if the passed function returns true for every item in the
    enumeration. This corresponds with the `every()` method in JavaScript 1.6.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    It should return the `true` or `false`.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as `this` on the context. This is a good way
    to give your iterator function access to the current object.

    Example Usage:

    ```javascript
    if (people.every(isEngineer)) {
      Paychecks.addBigBonus();
    }
    ```

    @method every
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Boolean}
    @private
  */
  every(callback, target) {
    return !this.find((x, idx, i) => !callback.call(target, x, idx, i));
  },

  /**
    @method everyBy
    @param {String} key the property to test
    @param {String} [value] optional value to test against.
    @deprecated Use `isEvery` instead
    @return {Boolean}
    @public
  */
  everyBy: aliasMethod('isEvery'),

  /**
    @method everyProperty
    @param {String} key the property to test
    @param {String} [value] optional value to test against.
    @deprecated Use `isEvery` instead
    @return {Boolean}
    @private
  */
  everyProperty: aliasMethod('isEvery'),

  /**
    Returns `true` if the passed property resolves to the value of the second
    argument for all items in the enumerable. This method is often simpler/faster
    than using a callback.

    @method isEvery
    @param {String} key the property to test
    @param {String} [value] optional value to test against. Defaults to `true`
    @return {Boolean}
    @since 1.3.0
    @public
  */
  isEvery(key, value) {
    return this.every(iter.apply(this, arguments));
  },

  /**
    Returns `true` if the passed function returns true for any item in the
    enumeration. This corresponds with the `some()` method in JavaScript 1.6.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    It should return the `true` to include the item in the results, `false`
    otherwise.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as `this` on the context. This is a good way
    to give your iterator function access to the current object.

    Usage Example:

    ```javascript
    if (people.any(isManager)) {
      Paychecks.addBiggerBonus();
    }
    ```

    @method any
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Boolean} `true` if the passed function returns `true` for any item
    @private
  */
  any(callback, target) {
    var len = get(this, 'length');
    var context = popCtx();
    var found = false;
    var last = null;
    var next, idx;

    if (target === undefined) {
      target = null;
    }

    for (idx = 0; idx < len && !found; idx++) {
      next  = this.nextObject(idx, last, context);
      found = callback.call(target, next, idx, this);
      last  = next;
    }

    next = last = null;
    context = pushCtx(context);
    return found;
  },

  /**
    Returns `true` if the passed function returns true for any item in the
    enumeration. This corresponds with the `some()` method in JavaScript 1.6.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(item, index, enumerable);
    ```

    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    It should return the `true` to include the item in the results, `false`
    otherwise.

    Note that in addition to a callback, you can also pass an optional target
    object that will be set as `this` on the context. This is a good way
    to give your iterator function access to the current object.

    Usage Example:

    ```javascript
    if (people.some(isManager)) {
      Paychecks.addBiggerBonus();
    }
    ```

    @method some
    @param {Function} callback The callback to execute
    @param {Object} [target] The target object to use
    @return {Boolean} `true` if the passed function returns `true` for any item
    @deprecated Use `any` instead
    @private
  */
  some: aliasMethod('any'),

  /**
    Returns `true` if the passed property resolves to the value of the second
    argument for any item in the enumerable. This method is often simpler/faster
    than using a callback.

    @method isAny
    @param {String} key the property to test
    @param {String} [value] optional value to test against. Defaults to `true`
    @return {Boolean}
    @since 1.3.0
    @private
  */
  isAny(key, value) {
    return this.any(iter.apply(this, arguments));
  },

  /**
    @method anyBy
    @param {String} key the property to test
    @param {String} [value] optional value to test against.
    @return {Boolean}
    @deprecated Use `isAny` instead
    @private
  */
  anyBy: aliasMethod('isAny'),

  /**
    This will combine the values of the enumerator into a single value. It
    is a useful way to collect a summary value from an enumeration. This
    corresponds to the `reduce()` method defined in JavaScript 1.8.

    The callback method you provide should have the following signature (all
    parameters are optional):

    ```javascript
    function(previousValue, item, index, enumerable);
    ```

    - `previousValue` is the value returned by the last call to the iterator.
    - `item` is the current item in the iteration.
    - `index` is the current index in the iteration.
    - `enumerable` is the enumerable object itself.

    Return the new cumulative value.

    In addition to the callback you can also pass an `initialValue`. An error
    will be raised if you do not pass an initial value and the enumerator is
    empty.

    Note that unlike the other methods, this method does not allow you to
    pass a target object to set as this for the callback. It's part of the
    spec. Sorry.

    @method reduce
    @param {Function} callback The callback to execute
    @param {Object} initialValue Initial value for the reduce
    @param {String} reducerProperty internal use only.
    @return {Object} The reduced value.
    @private
  */
  reduce(callback, initialValue, reducerProperty) {
    if (typeof callback !== 'function') {
      throw new TypeError();
    }

    var ret = initialValue;

    this.forEach(function(item, i) {
      ret = callback(ret, item, i, this, reducerProperty);
    }, this);

    return ret;
  },

  /**
    Invokes the named method on every object in the receiver that
    implements it. This method corresponds to the implementation in
    Prototype 1.6.

    @method invoke
    @param {String} methodName the name of the method
    @param {Object...} args optional arguments to pass as well.
    @return {Array} return values from calling invoke.
    @private
  */
  invoke(methodName, ...args) {
    var ret = Ember.A();

    this.forEach(function(x, idx) {
      var method = x && x[methodName];

      if ('function' === typeof method) {
        ret[idx] = args ? method.apply(x, args) : x[methodName]();
      }
    }, this);

    return ret;
  },

  /**
    Simply converts the enumerable into a genuine array. The order is not
    guaranteed. Corresponds to the method implemented by Prototype.

    @method toArray
    @return {Array} the enumerable as an array.
    @private
  */
  toArray() {
    var ret = Ember.A();

    this.forEach(function(o, idx) {
      ret[idx] = o;
    });

    return ret;
  },

  /**
    Returns a copy of the array with all `null` and `undefined` elements removed.

    ```javascript
    var arr = ['a', null, 'c', undefined];
    arr.compact();  // ['a', 'c']
    ```

    @method compact
    @return {Array} the array without null and undefined elements.
    @private
  */
  compact() {
    return this.filter(function(value) {
      return value != null;
    });
  },

  /**
    Returns a new enumerable that excludes the passed value. The default
    implementation returns an array regardless of the receiver type unless
    the receiver does not contain the value.

    ```javascript
    var arr = ['a', 'b', 'a', 'c'];
    arr.without('a');  // ['b', 'c']
    ```

    @method without
    @param {Object} value
    @return {Ember.Enumerable}
    @private
  */
  without(value) {
    if (!this.contains(value)) {
      return this; // nothing to do
    }

    var ret = Ember.A();

    this.forEach(function(k) {
      if (k !== value) {
        ret[ret.length] = k;
      }
    });

    return ret;
  },

  /**
    Returns a new enumerable that contains only unique values. The default
    implementation returns an array regardless of the receiver type.

    ```javascript
    var arr = ['a', 'a', 'b', 'b'];
    arr.uniq();  // ['a', 'b']
    ```

    This only works on primitive data types, e.g. Strings, Numbers, etc.

    @method uniq
    @return {Ember.Enumerable}
    @private
  */
  uniq() {
    var ret = Ember.A();

    this.forEach((k) => {
      if (ret.indexOf(k) < 0) {
        ret.push(k);
      }
    });

    return ret;
  },

  /**
    This property will trigger anytime the enumerable's content changes.
    You can observe this property to be notified of changes to the enumerable's
    content.

    For plain enumerables, this property is read only. `Array` overrides
    this method.

    @property []
    @type Array
    @return this
    @private
  */
  '[]': computed({
    get(key) { return this; }
  }),

  // ..........................................................
  // ENUMERABLE OBSERVERS
  //

  /**
    Registers an enumerable observer. Must implement `Ember.EnumerableObserver`
    mixin.

    @method addEnumerableObserver
    @param {Object} target
    @param {Object} [opts]
    @return this
    @private
  */
  addEnumerableObserver(target, opts) {
    var willChange = (opts && opts.willChange) || 'enumerableWillChange';
    var didChange  = (opts && opts.didChange) || 'enumerableDidChange';
    var hasObservers = get(this, 'hasEnumerableObservers');

    if (!hasObservers) {
      propertyWillChange(this, 'hasEnumerableObservers');
    }

    addListener(this, '@enumerable:before', target, willChange);
    addListener(this, '@enumerable:change', target, didChange);

    if (!hasObservers) {
      propertyDidChange(this, 'hasEnumerableObservers');
    }

    return this;
  },

  /**
    Removes a registered enumerable observer.

    @method removeEnumerableObserver
    @param {Object} target
    @param {Object} [opts]
    @return this
    @private
  */
  removeEnumerableObserver(target, opts) {
    var willChange = (opts && opts.willChange) || 'enumerableWillChange';
    var didChange  = (opts && opts.didChange) || 'enumerableDidChange';
    var hasObservers = get(this, 'hasEnumerableObservers');

    if (hasObservers) {
      propertyWillChange(this, 'hasEnumerableObservers');
    }

    removeListener(this, '@enumerable:before', target, willChange);
    removeListener(this, '@enumerable:change', target, didChange);

    if (hasObservers) {
      propertyDidChange(this, 'hasEnumerableObservers');
    }

    return this;
  },

  /**
    Becomes true whenever the array currently has observers watching changes
    on the array.

    @property hasEnumerableObservers
    @type Boolean
    @private
  */
  hasEnumerableObservers: computed(function() {
    return hasListeners(this, '@enumerable:change') || hasListeners(this, '@enumerable:before');
  }),

  /**
    Invoke this method just before the contents of your enumerable will
    change. You can either omit the parameters completely or pass the objects
    to be removed or added if available or just a count.

    @method enumerableContentWillChange
    @param {Ember.Enumerable|Number} removing An enumerable of the objects to
      be removed or the number of items to be removed.
    @param {Ember.Enumerable|Number} adding An enumerable of the objects to be
      added or the number of items to be added.
    @chainable
    @private
  */
  enumerableContentWillChange(removing, adding) {
    var removeCnt, addCnt, hasDelta;

    if ('number' === typeof removing) {
      removeCnt = removing;
    } else if (removing) {
      removeCnt = get(removing, 'length');
    } else {
      removeCnt = removing = -1;
    }

    if ('number' === typeof adding) {
      addCnt = adding;
    } else if (adding) {
      addCnt = get(adding, 'length');
    } else {
      addCnt = adding = -1;
    }

    hasDelta = addCnt < 0 || removeCnt < 0 || addCnt - removeCnt !== 0;

    if (removing === -1) {
      removing = null;
    }

    if (adding === -1) {
      adding = null;
    }

    propertyWillChange(this, '[]');

    if (hasDelta) {
      propertyWillChange(this, 'length');
    }

    sendEvent(this, '@enumerable:before', [this, removing, adding]);

    return this;
  },

  /**
    Invoke this method when the contents of your enumerable has changed.
    This will notify any observers watching for content changes. If you are
    implementing an ordered enumerable (such as an array), also pass the
    start and end values where the content changed so that it can be used to
    notify range observers.

    @method enumerableContentDidChange
    @param {Ember.Enumerable|Number} removing An enumerable of the objects to
      be removed or the number of items to be removed.
    @param {Ember.Enumerable|Number} adding  An enumerable of the objects to
      be added or the number of items to be added.
    @chainable
    @private
  */
  enumerableContentDidChange(removing, adding) {
    var removeCnt, addCnt, hasDelta;

    if ('number' === typeof removing) {
      removeCnt = removing;
    } else if (removing) {
      removeCnt = get(removing, 'length');
    } else {
      removeCnt = removing = -1;
    }

    if ('number' === typeof adding) {
      addCnt = adding;
    } else if (adding) {
      addCnt = get(adding, 'length');
    } else {
      addCnt = adding = -1;
    }

    hasDelta = addCnt < 0 || removeCnt < 0 || addCnt - removeCnt !== 0;

    if (removing === -1) {
      removing = null;
    }

    if (adding === -1) {
      adding = null;
    }

    sendEvent(this, '@enumerable:change', [this, removing, adding]);

    if (hasDelta) {
      propertyDidChange(this, 'length');
    }

    propertyDidChange(this, '[]');

    return this;
  },

  /**
    Converts the enumerable into an array and sorts by the keys
    specified in the argument.

    You may provide multiple arguments to sort by multiple properties.

    @method sortBy
    @param {String} property name(s) to sort on
    @return {Array} The sorted array.
    @since 1.2.0
    @private
  */
  sortBy() {
    var sortKeys = arguments;

    return this.toArray().sort(function(a, b) {
      for (var i = 0; i < sortKeys.length; i++) {
        var key = sortKeys[i];
        var propA = get(a, key);
        var propB = get(b, key);
        // return 1 or -1 else continue to the next sortKey
        var compareValue = compare(propA, propB);

        if (compareValue) {
          return compareValue;
        }
      }
      return 0;
    });
  }
});


text:
<!DOCTYPE html>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Predict — skccm 0.1 documentation</title>

    <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />

        <link rel="index" title="Index"
              href="genindex.html"/>
        <link rel="search" title="Search" href="search.html"/>
    <link rel="top" title="skccm 0.1 documentation" href="index.html"/>
        <link rel="next" title="Score" href="score.html"/>
        <link rel="prev" title="Embed" href="embed.html"/>

             skccm

                0.1

  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>

Contents:

Install

Quick Example

Generate Data

Embed

Predict

Training and Testing

Distance Calculation

Weighted Average

Library Length

Score

Module Reference

Acknowledgements

        skccm

Docs »

Predict

             View page source

Predict¶

At the heart of convergent cross mapping is the k-nearest neighbors algorithm. In fact, the package uses scikit-learn’s nearest neighbor implementation for efficient calculation of distances and to retrieve the indices of the nearest neighbors. It is a good idea to understand the k-nearest neighbor algorithm before interpreting what this package implements.

Training and Testing¶

Before making predictions, split the embedded time series into a testing set and a training set. This protects against highly autocorrelated time series. For example, random walk time series can seem like they are coupled if they are not split into a training set and testing set.

The code for this is done with a function in skccm.utilities.

from skccm.utilities import train_test_split

#split the embedded time series
x1tr, x1te, x2tr, x2te = train_test_split(X1,X2, percent=.75)

The code above uses the first 75% as the training set (to rebuild the shadow manifolds) and uses the last 25% as the testing set.

Distance Calculation¶

At this point, you will have these four embedded time series:

X1tr

X1te

X2tr

X2te

The distance is calculated from every sample in X1te to every sample in X1tr. The same is then done for X2tr and X2te. The distances are then sorted and the closest \(k\) indices are kept to make a prediction in the next step. \(k\) is the embedding dimension plus 1. So if your embedding dimension was three, then the amount of near neighbors used to make a prediction will be four.

Weighted Average¶

The next step is to use the near neighbor indices and weights to make a prediction about the other time series. The indices that were found by calculating the distance from every sample in X1te to every sample in X1tr, are used on X2tr to make a prediction about X2te. This seems a little counterintuitive, but it is expected that if one time series influences the other, the system being forced should be in a similar state when the system doing the forcing is in a certain configuration.

Notice the distances and indices have the same number of samples as the testing set, but an extra dimension. This is because you need \(K+1\) near neighbors in order to surround a point.

Library Length¶

A prediction is made for multiple library lengths. As the library length is increased, the prediction skill should converge. By increasing the library length, the density of the rebuilt attractor is increasing. As that attractor becomes more and more populated, better predictions should be able to be made.

        Next

         Previous

        © Copyright 2017, Nick Cortale.

  Built with Sphinx using a theme provided by Read the Docs.

redpajama_set_name: RedPajamaGithub


text:
namespace faint {

// Handles utf-8 also on Windows, MSVC:s fopen does not.
FILE* faint_fopen_write_binary(const FilePath&);
FILE* faint_fopen_read_binary(const FilePath&);

// Handles utf-8 also on Windows, MSVC:s open does not.
// Returns a file handle.
int faint_open(const FilePath&, int oflag, int pmode);

} // namespace

#endif


text:
<div class="pod filter podToggleOpen <?php echo $data->isFiltering() ? 'active' : '' ?>">

		<a href="<?php echo Request::uri(array("{$table}_clear"=>"true")) ?>" class="icon iconReset">Clear Search
		<form>

				Saved Searches

					<input type="text" name="<?php echo $table . "_load" ?>" autocomplete="off" />

						<?php foreach ($data->savedFilterOptions() as $option) { ?>

<?php echo $option ?>

						<?php } ?>

		</form>

	<form class="filterOptions">
		Search: <?php echo text_field_tag("{$table}_filter", $data->filterParam(), array('class'=>"mainFilter")) ?>

			Save Search

				<input type="text" name="<?php echo $table . "_save" ?>" autocomplete="off" />

					<?php foreach ($data->savedFilterOptions() as $option) { ?>

<?php echo $option ?>

					<?php } ?>

Manage Saved Searches

			<?php foreach (Schema::filterableColumns($table) as $colName=>$filterColumn) { ?>

				<?php
					$self->renderPartial(
						(SqlTypes::isDate($filterColumn['type'])) ? "filter_date" : "filter_options",
						array("table"=>$table, "column"=>$colName, "data"=>$data)
					);
				?>

			<?php } ?>

				<input type="submit" class="btnLink btnLinkLight btnLinkThin icon iconSearch" value="Search" />

	</form>


text:
use crate::{Address, Bytecode, IdentifierWrapper, VerifyInput, VerifyInputWithRecursion};
use anyhow::{bail, format_err};
use aptos_types::{account_config::CORE_CODE_ADDRESS, event::EventKey, transaction::Module};

use move_binary_format::{
    access::ModuleAccess,
    file_format::{
        Ability, AbilitySet, CompiledModule, CompiledScript, StructTypeParameter, Visibility,
    },
};
use move_core_types::{
    account_address::AccountAddress,
    identifier::Identifier,
    language_storage::{ModuleId, StructTag, TypeTag},
    parser::{parse_struct_tag, parse_type_tag},
    transaction_argument::TransactionArgument,
};
use move_resource_viewer::{AnnotatedMoveStruct, AnnotatedMoveValue};

use poem_openapi::{types::Type, Enum, Object, Union};
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use std::{
    collections::BTreeMap,
    convert::{From, Into, TryFrom, TryInto},
    fmt,
    fmt::Display,
    result::Result,
    str::FromStr,
};

/// A parsed Move resource
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveResource {
    #[serde(rename = "type")]
    #[oai(rename = "type")]
    pub typ: MoveStructTag,
    pub data: MoveStructValue,
}

impl TryFrom<AnnotatedMoveStruct> for MoveResource {
    type Error = anyhow::Error;

    fn try_from(s: AnnotatedMoveStruct) -> anyhow::Result<Self> {
        Ok(Self {
            typ: s.type_.clone().into(),
            data: s.try_into()?,
        })
    }
}

/// A string encoded U64
///
/// Encoded as a string to encode into JSON
#[derive(Clone, Debug, Default, Eq, PartialEq, Copy)]
pub struct U64(pub u64);

impl U64 {
    pub fn inner(&self) -> &u64 {
        &self.0
    }
}

impl From<u64> for U64 {
    fn from(d: u64) -> Self {
        Self(d)
    }
}

impl From<U64> for u64 {
    fn from(d: U64) -> Self {
        d.0
    }
}

impl From<U64> for move_core_types::value::MoveValue {
    fn from(d: U64) -> Self {
        move_core_types::value::MoveValue::U64(d.0)
    }
}

impl fmt::Display for U64 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", &self.0)
    }
}

impl Serialize for U64 {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.0.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for U64 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = <String>::deserialize(deserializer)?;
        s.parse().map_err(D::Error::custom)
    }
}

impl FromStr for U64 {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let data = s.parse::<u64>().map_err(|e| {
            format_err!("Parsing u64 string {:?} failed, caused by error: {}", s, e)
        })?;

        Ok(U64(data))
    }
}

/// A string encoded U128
///
/// Encoded as a string to encode into JSON
#[derive(Clone, Debug, Default, PartialEq, Eq, Copy)]
pub struct U128(pub u128);

impl U128 {
    pub fn inner(&self) -> &u128 {
        &self.0
    }
}

impl From<u128> for U128 {
    fn from(d: u128) -> Self {
        Self(d)
    }
}

impl From<U128> for u128 {
    fn from(d: U128) -> Self {
        d.0
    }
}

impl From<U128> for move_core_types::value::MoveValue {
    fn from(d: U128) -> Self {
        move_core_types::value::MoveValue::U128(d.0)
    }
}

impl Serialize for U128 {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.0.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for U128 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = <String>::deserialize(deserializer)?;
        let data = s.parse::<u128>().map_err(D::Error::custom)?;

        Ok(U128(data))
    }
}

impl FromStr for U128 {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let data = s.parse::<u128>().map_err(|e| {
            format_err!("Parsing u128 string {:?} failed, caused by error: {}", s, e)
        })?;

        Ok(U128(data))
    }
}

/// Hex encoded bytes to allow for having bytes represented in JSON
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HexEncodedBytes(pub Vec<u8>);

impl HexEncodedBytes {
    pub fn json(&self) -> anyhow::Result<serde_json::Value> {
        Ok(serde_json::to_value(self)?)
    }
}

impl FromStr for HexEncodedBytes {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> anyhow::Result<Self, anyhow::Error> {
        let hex_str = if let Some(hex) = s.strip_prefix("0x") {
            hex
        } else {
            s
        };
        Ok(Self(hex::decode(hex_str).map_err(|e| {
            format_err!(
                "decode hex-encoded string({:?}) failed, caused by error: {}",
                s,
                e
            )
        })?))
    }
}

impl fmt::Display for HexEncodedBytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{}", hex::encode(&self.0))?;
        Ok(())
    }
}

impl Serialize for HexEncodedBytes {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for HexEncodedBytes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = <String>::deserialize(deserializer)?;
        s.parse().map_err(D::Error::custom)
    }
}

impl From<Vec<u8>> for HexEncodedBytes {
    fn from(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }
}

impl From<HexEncodedBytes> for Vec<u8> {
    fn from(bytes: HexEncodedBytes) -> Self {
        bytes.0
    }
}

impl From<HexEncodedBytes> for move_core_types::value::MoveValue {
    fn from(d: HexEncodedBytes) -> Self {
        move_core_types::value::MoveValue::Vector(
            d.0.into_iter()
                .map(move_core_types::value::MoveValue::U8)
                .collect(),
        )
    }
}

impl TryFrom<HexEncodedBytes> for EventKey {
    type Error = anyhow::Error;
    fn try_from(bytes: HexEncodedBytes) -> anyhow::Result<Self> {
        Ok(bcs::from_bytes(&bytes.0)?)
    }
}

impl HexEncodedBytes {
    pub fn inner(&self) -> &[u8] {
        &self.0
    }
}

/// A JSON map representation of a Move struct's inner types
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MoveStructValue(pub BTreeMap<IdentifierWrapper, serde_json::Value>);

impl TryFrom<AnnotatedMoveStruct> for MoveStructValue {
    type Error = anyhow::Error;
    fn try_from(s: AnnotatedMoveStruct) -> anyhow::Result<Self> {
        let mut map = BTreeMap::new();
        for (id, val) in s.value {
            map.insert(id.into(), MoveValue::try_from(val)?.json()?);
        }
        Ok(Self(map))
    }
}

/// An enum of the possible Move value types
#[derive(Clone, Debug, PartialEq, Union)]
pub enum MoveValue {
    /// A u8 Move type
    U8(u8),
    U64(U64),
    U128(U128),
    /// A bool Move type
    Bool(bool),
    Address(Address),
    /// A vector Move type.  May have any other [`MoveValue`] nested inside it
    Vector(Vec<MoveValue>),
    Bytes(HexEncodedBytes),
    Struct(MoveStructValue),
    /// A string Move type
    String(String),
}

impl MoveValue {
    pub fn json(&self) -> anyhow::Result<serde_json::Value> {
        Ok(serde_json::to_value(self)?)
    }

    pub fn is_utf8_string(st: &StructTag) -> bool {
        st.address == CORE_CODE_ADDRESS
            && st.name.to_string() == "String"
            && st.module.to_string() == "string"
    }

    pub fn convert_utf8_string(v: AnnotatedMoveStruct) -> anyhow::Result<MoveValue> {
        if let Some((_, AnnotatedMoveValue::Bytes(bytes))) = v.value.into_iter().next() {
            match String::from_utf8(bytes.clone()) {
                Ok(string) => Ok(MoveValue::String(string)),
                Err(_) => {
                    // There's no real use in logging the error, since this is only done on output conversion
                    Ok(MoveValue::String(format!(
                        "Unparsable utf-8 {}",
                        HexEncodedBytes(bytes)
                    )))
                }
            }
        } else {
            bail!("expect string::String, but failed to decode struct value");
        }
    }
}

impl TryFrom<AnnotatedMoveValue> for MoveValue {
    type Error = anyhow::Error;

    fn try_from(val: AnnotatedMoveValue) -> anyhow::Result<Self> {
        Ok(match val {
            AnnotatedMoveValue::U8(v) => MoveValue::U8(v),
            AnnotatedMoveValue::U64(v) => MoveValue::U64(U64(v)),
            AnnotatedMoveValue::U128(v) => MoveValue::U128(U128(v)),
            AnnotatedMoveValue::Bool(v) => MoveValue::Bool(v),
            AnnotatedMoveValue::Address(v) => MoveValue::Address(v.into()),
            AnnotatedMoveValue::Vector(_, vals) => MoveValue::Vector(
                vals.into_iter()
                    .map(MoveValue::try_from)
                    .collect::<anyhow::Result<_>>()?,
            ),
            AnnotatedMoveValue::Bytes(v) => MoveValue::Bytes(HexEncodedBytes(v)),
            AnnotatedMoveValue::Struct(v) => {
                if MoveValue::is_utf8_string(&v.type_) {
                    MoveValue::convert_utf8_string(v)?
                } else {
                    MoveValue::Struct(v.try_into()?)
                }
            }
        })
    }
}

impl From<TransactionArgument> for MoveValue {
    fn from(val: TransactionArgument) -> Self {
        match val {
            TransactionArgument::U8(v) => MoveValue::U8(v),
            TransactionArgument::U64(v) => MoveValue::U64(U64(v)),
            TransactionArgument::U128(v) => MoveValue::U128(U128(v)),
            TransactionArgument::Bool(v) => MoveValue::Bool(v),
            TransactionArgument::Address(v) => MoveValue::Address(v.into()),
            TransactionArgument::U8Vector(bytes) => MoveValue::Bytes(HexEncodedBytes(bytes)),
        }
    }
}

impl Serialize for MoveValue {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match &self {
            MoveValue::U8(v) => v.serialize(serializer),
            MoveValue::U64(v) => v.serialize(serializer),
            MoveValue::U128(v) => v.serialize(serializer),
            MoveValue::Bool(v) => v.serialize(serializer),
            MoveValue::Address(v) => v.serialize(serializer),
            MoveValue::Vector(v) => v.serialize(serializer),
            MoveValue::Bytes(v) => v.serialize(serializer),
            MoveValue::Struct(v) => v.serialize(serializer),
            MoveValue::String(v) => v.serialize(serializer),
        }
    }
}

/// A Move struct tag for referencing an onchain struct type
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MoveStructTag {
    pub address: Address,
    pub module: IdentifierWrapper,
    pub name: IdentifierWrapper,
    /// Generic type parameters associated with the struct
    pub generic_type_params: Vec<MoveType>,
}

impl VerifyInputWithRecursion for MoveStructTag {
    fn verify(&self, recursion_count: u8) -> anyhow::Result<()> {
        if recursion_count > MAX_RECURSIVE_TYPES_ALLOWED {
            bail!(
                "Move struct tag {} has gone over the limit of recursive types {}",
                self,
                MAX_RECURSIVE_TYPES_ALLOWED
            );
        }
        verify_module_identifier(self.module.as_str())
            .map_err(|_| anyhow::anyhow!("invalid struct tag: {}", self))?;
        verify_identifier(self.name.as_str())
            .map_err(|_| anyhow::anyhow!("invalid struct tag: {}", self))?;
        for param in self.generic_type_params.iter() {
            param.verify(recursion_count + 1).map_err(|err| {
                anyhow::anyhow!(
                    "Invalid struct tag for generic type params: {} {}",
                    self,
                    err
                )
            })?;
        }

        Ok(())
    }
}

impl MoveStructTag {
    pub fn new(
        address: Address,
        module: IdentifierWrapper,
        name: IdentifierWrapper,
        generic_type_params: Vec<MoveType>,
    ) -> Self {
        Self {
            address,
            module,
            name,
            generic_type_params,
        }
    }
}

impl FromStr for MoveStructTag {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> anyhow::Result<Self, anyhow::Error> {
        Ok(parse_struct_tag(s)?.into())
    }
}

impl From<StructTag> for MoveStructTag {
    fn from(tag: StructTag) -> Self {
        Self {
            address: tag.address.into(),
            module: tag.module.into(),
            name: tag.name.into(),
            generic_type_params: tag.type_params.into_iter().map(MoveType::from).collect(),
        }
    }
}

impl From<&StructTag> for MoveStructTag {
    fn from(tag: &StructTag) -> Self {
        Self {
            address: tag.address.into(),
            module: IdentifierWrapper::from(&tag.module),
            name: IdentifierWrapper::from(&tag.name),
            generic_type_params: tag.type_params.iter().map(MoveType::from).collect(),
        }
    }
}

impl fmt::Display for MoveStructTag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}::{}::{}", self.address, self.module, self.name)?;
        if let Some(first_ty) = self.generic_type_params.first() {
            write!(f, "<")?;
            write!(f, "{}", first_ty)?;
            for ty in self.generic_type_params.iter().skip(1) {
                write!(f, ", {}", ty)?;
            }
            write!(f, ">")?;
        }
        Ok(())
    }
}

impl Serialize for MoveStructTag {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for MoveStructTag {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let data = <String>::deserialize(deserializer)?;
        data.parse().map_err(D::Error::custom)
    }
}

impl TryFrom<MoveStructTag> for StructTag {
    type Error = anyhow::Error;
    fn try_from(tag: MoveStructTag) -> anyhow::Result<Self> {
        Ok(Self {
            address: tag.address.into(),
            module: tag.module.into(),
            name: tag.name.into(),
            type_params: tag
                .generic_type_params
                .into_iter()
                .map(|p| p.try_into())
                .collect::<anyhow::Result<Vec<TypeTag>>>()?,
        })
    }
}

/// An enum of Move's possible types on-chain
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MoveType {
    /// A bool type
    Bool,
    /// An 8-bit unsigned int
    U8,
    /// A 64-bit unsigned int
    U64,
    /// A 128-bit unsigned int
    U128,
    /// A 32-byte account address
    Address,
    /// An account signer
    Signer,
    /// A Vector of [`MoveType`]
    Vector { items: Box<MoveType> },
    /// A struct of [`MoveStructTag`]
    Struct(MoveStructTag),
    /// A generic type param with index
    GenericTypeParam { index: u16 },
    /// A reference
    Reference { mutable: bool, to: Box<MoveType> },
    /// A move type that couldn't be parsed
    ///
    /// This prevents the parser from just throwing an error because one field
    /// was unparsable, and gives the value in it.
    Unparsable(String),
}

/// Maximum number of recursive types
/// Currently, this is allowed up to the serde limit of 16
///
/// TODO: Should this number be re-evaluated
pub const MAX_RECURSIVE_TYPES_ALLOWED: u8 = 16;

impl VerifyInputWithRecursion for MoveType {
    fn verify(&self, recursion_count: u8) -> anyhow::Result<()> {
        if recursion_count > MAX_RECURSIVE_TYPES_ALLOWED {
            bail!(
                "Move type {} has gone over the limit of recursive types {}",
                self,
                MAX_RECURSIVE_TYPES_ALLOWED
            );
        }
        match self {
            MoveType::Vector { items } => items.verify(recursion_count + 1),
            MoveType::Struct(struct_tag) => struct_tag.verify(recursion_count + 1),
            MoveType::GenericTypeParam { .. } => Ok(()),
            MoveType::Reference { to, .. } => to.verify(recursion_count + 1),
            MoveType::Unparsable(inner) => bail!("Unable to parse move type {}", inner),
            _ => Ok(()),
        }
    }
}

impl MoveType {
    // Returns corresponding JSON data type for the value of `MoveType`
    pub fn json_type_name(&self) -> String {
        match self {
            MoveType::U8 => "integer".to_owned(),
            MoveType::U64 => "string<u64>".to_owned(),
            MoveType::U128 => "string<u128>".to_owned(),
            MoveType::Signer | MoveType::Address => "string<address>".to_owned(),
            MoveType::Bool => "boolean".to_owned(),
            MoveType::Vector { items } => {
                if matches!(**items, MoveType::U8) {
                    "string<hex>".to_owned()
                } else {
                    format!("array<{}>", items.json_type_name())
                }
            }
            MoveType::Struct(_) | MoveType::GenericTypeParam { index: _ } => {
                "string<move_struct_tag_id>".to_owned()
            }
            MoveType::Reference { mutable: _, to } => to.json_type_name(),
            MoveType::Unparsable(string) => string.to_string(),
        }
    }
}

impl fmt::Display for MoveType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MoveType::U8 => write!(f, "u8"),
            MoveType::U64 => write!(f, "u64"),
            MoveType::U128 => write!(f, "u128"),
            MoveType::Address => write!(f, "address"),
            MoveType::Signer => write!(f, "signer"),
            MoveType::Bool => write!(f, "bool"),
            MoveType::Vector { items } => write!(f, "vector<{}>", items),
            MoveType::Struct(s) => write!(f, "{}", s),
            MoveType::GenericTypeParam { index } => write!(f, "T{}", index),
            MoveType::Reference { mutable, to } => {
                if *mutable {
                    write!(f, "&mut {}", to)
                } else {
                    write!(f, "&{}", to)
                }
            }
            MoveType::Unparsable(string) => write!(f, "unparsable<{}>", string),
        }
    }
}

// This function cannot handle the full range of types that MoveType can
// represent. Internally, it uses parse_type_tag, which cannot handle references
// or generic type parameters. This function adds nominal support for references
// on top of parse_type_tag, but it still does not work for generic type params.
// For that, we have the Unparsable variant of MoveType, so the deserialization
// doesn't fail when dealing with these values.
impl FromStr for MoveType {
    type Err = anyhow::Error;

    fn from_str(mut s: &str) -> Result<Self, Self::Err> {
        let mut is_ref = false;
        let mut is_mut = false;
        if s.starts_with('&') {
            s = &s[1..];
            is_ref = true;
        }
        if is_ref && s.starts_with("mut ") {
            s = &s[4..];
            is_mut = true;
        }
        // Previously this would just crap out, but this meant the API could
        // return a serialized version of an object and not be able to
        // deserialize it using that same object.
        let inner = match parse_type_tag(s) {
            Ok(inner) => inner.into(),
            Err(_e) => MoveType::Unparsable(s.to_string()),
        };
        if is_ref {
            Ok(MoveType::Reference {
                mutable: is_mut,
                to: Box::new(inner),
            })
        } else {
            Ok(inner)
        }
    }
}

impl Serialize for MoveType {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.to_string().serialize(serializer)
    }
}

// This deserialization has limitations, see the FromStr impl for MoveType.
impl<'de> Deserialize<'de> for MoveType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let data = <String>::deserialize(deserializer)
            .map_err(|e| D::Error::custom(format_err!("deserialize Move type failed, {}", e)))?;
        data.parse().map_err(D::Error::custom)
    }
}

impl MoveType {
    pub fn is_signer(&self) -> bool {
        match self {
            MoveType::Signer => true,
            MoveType::Reference { mutable: _, to } => to.is_signer(),
            _ => false,
        }
    }
}

impl From<TypeTag> for MoveType {
    fn from(tag: TypeTag) -> Self {
        match tag {
            TypeTag::Bool => MoveType::Bool,
            TypeTag::U8 => MoveType::U8,
            TypeTag::U64 => MoveType::U64,
            TypeTag::U128 => MoveType::U128,
            TypeTag::Address => MoveType::Address,
            TypeTag::Signer => MoveType::Signer,
            TypeTag::Vector(v) => MoveType::Vector {
                items: Box::new(MoveType::from(*v)),
            },
            TypeTag::Struct(v) => MoveType::Struct((*v).into()),
        }
    }
}

impl From<&TypeTag> for MoveType {
    fn from(tag: &TypeTag) -> Self {
        match tag {
            TypeTag::Bool => MoveType::Bool,
            TypeTag::U8 => MoveType::U8,
            TypeTag::U64 => MoveType::U64,
            TypeTag::U128 => MoveType::U128,
            TypeTag::Address => MoveType::Address,
            TypeTag::Signer => MoveType::Signer,
            TypeTag::Vector(v) => MoveType::Vector {
                items: Box::new(MoveType::from(v.as_ref())),
            },
            TypeTag::Struct(v) => MoveType::Struct((&**v).into()),
        }
    }
}

impl TryFrom<MoveType> for TypeTag {
    type Error = anyhow::Error;
    fn try_from(tag: MoveType) -> anyhow::Result<Self> {
        let ret = match tag {
            MoveType::Bool => TypeTag::Bool,
            MoveType::U8 => TypeTag::U8,
            MoveType::U64 => TypeTag::U64,
            MoveType::U128 => TypeTag::U128,
            MoveType::Address => TypeTag::Address,
            MoveType::Signer => TypeTag::Signer,
            MoveType::Vector { items } => TypeTag::Vector(Box::new((*items).try_into()?)),
            MoveType::Struct(v) => TypeTag::Struct(Box::new(v.try_into()?)),
            _ => {
                return Err(anyhow::anyhow!(
                    "Invalid move type for converting into `TypeTag`: {:?}",
                    &tag
                ))
            }
        };
        Ok(ret)
    }
}

/// A Move module
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveModule {
    pub address: Address,
    pub name: IdentifierWrapper,
    /// Friends of the module
    pub friends: Vec<MoveModuleId>,
    /// Public functions of the module
    pub exposed_functions: Vec<MoveFunction>,
    /// Structs of the module
    pub structs: Vec<MoveStruct>,
}

impl From<CompiledModule> for MoveModule {
    fn from(m: CompiledModule) -> Self {
        let (address, name) = <(AccountAddress, Identifier)>::from(m.self_id());
        Self {
            address: address.into(),
            name: name.into(),
            friends: m
                .immediate_friends()
                .into_iter()
                .map(|f| f.into())
                .collect(),
            exposed_functions: m
                .function_defs
                .iter()
                .filter(|def| match def.visibility {
                    Visibility::Public | Visibility::Friend => true,
                    Visibility::Private => false,
                })
                .map(|def| m.new_move_function(def))
                .collect(),
            structs: m
                .struct_defs
                .iter()
                .map(|def| m.new_move_struct(def))
                .collect(),
        }
    }
}

/// A Move module Id
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct MoveModuleId {
    pub address: Address,
    pub name: IdentifierWrapper,
}

impl VerifyInput for MoveModuleId {
    fn verify(&self) -> anyhow::Result<()> {
        self.name.verify().map_err(|_| invalid_move_module_id(self))
    }
}

impl From<ModuleId> for MoveModuleId {
    fn from(id: ModuleId) -> Self {
        let (address, name) = <(AccountAddress, Identifier)>::from(id);
        Self {
            address: address.into(),
            name: name.into(),
        }
    }
}

impl From<MoveModuleId> for ModuleId {
    fn from(id: MoveModuleId) -> Self {
        ModuleId::new(id.address.into(), id.name.into())
    }
}

impl fmt::Display for MoveModuleId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}::{}", self.address, self.name)
    }
}

impl FromStr for MoveModuleId {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some((address, name)) = s.split_once("::") {
            return Ok(Self {
                address: address.parse().map_err(|_| invalid_move_module_id(s))?,
                name: name.parse().map_err(|_| invalid_move_module_id(s))?,
            });
        }
        Err(invalid_move_module_id(s))
    }
}

#[inline]
fn invalid_move_module_id<S: Display + Sized>(s: S) -> anyhow::Error {
    format_err!("Invalid Move module ID: {}", s)
}

impl Serialize for MoveModuleId {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for MoveModuleId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let module_id = <String>::deserialize(deserializer)?;
        module_id.parse().map_err(D::Error::custom)
    }
}

/// A move struct
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveStruct {
    pub name: IdentifierWrapper,
    /// Whether the struct is a native struct of Move
    pub is_native: bool,
    /// Abilities associated with the struct
    pub abilities: Vec<MoveAbility>,
    /// Generic types associated with the struct
    pub generic_type_params: Vec<MoveStructGenericTypeParam>,
    /// Fields associated with the struct
    pub fields: Vec<MoveStructField>,
}

/// A move ability e.g. drop, store
// TODO: Consider finding a way to derive NewType here instead of using the
// custom macro, since some of the enum type information (such as the
// variants) is currently being lost.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MoveAbility(pub Ability);

impl From<Ability> for MoveAbility {
    fn from(a: Ability) -> Self {
        Self(a)
    }
}

impl From<MoveAbility> for Ability {
    fn from(a: MoveAbility) -> Self {
        a.0
    }
}

impl fmt::Display for MoveAbility {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let st = match self.0 {
            Ability::Copy => "copy",
            Ability::Drop => "drop",
            Ability::Store => "store",
            Ability::Key => "key",
        };
        write!(f, "{}", st)
    }
}

impl FromStr for MoveAbility {
    type Err = anyhow::Error;

    fn from_str(ability: &str) -> Result<Self, Self::Err> {
        Ok(Self(match ability {
            "copy" => Ability::Copy,
            "drop" => Ability::Drop,
            "store" => Ability::Store,
            "key" => Ability::Key,
            _ => return Err(anyhow::anyhow!("Invalid ability string: {}", ability)),
        }))
    }
}

impl Serialize for MoveAbility {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for MoveAbility {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let ability = <String>::deserialize(deserializer)?;
        ability.parse().map_err(D::Error::custom)
    }
}

/// Move generic type param
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveStructGenericTypeParam {
    /// Move abilities tied to the generic type param and associated with the type that uses it
    pub constraints: Vec<MoveAbility>,
    /// Whether the type is a phantom type
    #[oai(skip)]
    pub is_phantom: bool,
}

impl From<&StructTypeParameter> for MoveStructGenericTypeParam {
    fn from(param: &StructTypeParameter) -> Self {
        Self {
            constraints: param
                .constraints
                .into_iter()
                .map(MoveAbility::from)
                .collect(),
            is_phantom: param.is_phantom,
        }
    }
}

/// Move struct field
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveStructField {
    pub name: IdentifierWrapper,
    #[serde(rename = "type")]
    #[oai(rename = "type")]
    pub typ: MoveType,
}

/// Move function
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveFunction {
    pub name: IdentifierWrapper,
    pub visibility: MoveFunctionVisibility,
    /// Whether the function can be called as an entry function directly in a transaction
    pub is_entry: bool,
    /// Generic type params associated with the Move function
    pub generic_type_params: Vec<MoveFunctionGenericTypeParam>,
    /// Parameters associated with the move function
    pub params: Vec<MoveType>,
    /// Return type of the function
    #[serde(rename = "return")]
    #[oai(rename = "return")]
    pub return_: Vec<MoveType>,
}

impl From<&CompiledScript> for MoveFunction {
    fn from(script: &CompiledScript) -> Self {
        Self {
            name: Identifier::new("main").unwrap().into(),
            visibility: MoveFunctionVisibility::Public,
            is_entry: true,
            generic_type_params: script
                .type_parameters
                .iter()
                .map(MoveFunctionGenericTypeParam::from)
                .collect(),
            params: script
                .signature_at(script.parameters)
                .0
                .iter()
                .map(|s| script.new_move_type(s))
                .collect(),
            return_: vec![],
        }
    }
}

/// Move function visibility
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Enum)]
#[serde(rename_all = "snake_case")]
#[oai(rename_all = "snake_case")]
pub enum MoveFunctionVisibility {
    /// Visible only by this module
    Private,
    /// Visible by all modules
    Public,
    /// Visible by friend modules
    Friend,
}

impl From<Visibility> for MoveFunctionVisibility {
    fn from(v: Visibility) -> Self {
        match &v {
            Visibility::Private => Self::Private,
            Visibility::Public => Self::Public,
            Visibility::Friend => Self::Friend,
        }
    }
}

impl From<MoveFunctionVisibility> for Visibility {
    fn from(v: MoveFunctionVisibility) -> Self {
        match &v {
            MoveFunctionVisibility::Private => Self::Private,
            MoveFunctionVisibility::Public => Self::Public,
            MoveFunctionVisibility::Friend => Self::Friend,
        }
    }
}

/// Move function generic type param
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveFunctionGenericTypeParam {
    /// Move abilities tied to the generic type param and associated with the function that uses it
    pub constraints: Vec<MoveAbility>,
}

impl From<&AbilitySet> for MoveFunctionGenericTypeParam {
    fn from(constraints: &AbilitySet) -> Self {
        Self {
            constraints: constraints.into_iter().map(MoveAbility::from).collect(),
        }
    }
}

/// Move module bytecode along with it's ABI
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveModuleBytecode {
    pub bytecode: HexEncodedBytes,
    // We don't need deserialize MoveModule as it should be serialized
    // from `bytecode`.
    #[serde(skip_deserializing)]
    pub abi: Option<MoveModule>,
}

impl VerifyInput for MoveModuleBytecode {
    fn verify(&self) -> anyhow::Result<()> {
        if self.bytecode.is_empty() {
            bail!("Move module bytecode is empty")
        }

        Ok(())
    }
}

impl MoveModuleBytecode {
    pub fn new(bytes: Vec<u8>) -> Self {
        Self {
            bytecode: bytes.into(),
            abi: None,
        }
    }

    pub fn try_parse_abi(mut self) -> anyhow::Result<Self> {
        if self.abi.is_none() {
            // Ignore error, because it is possible a transaction module payload contains
            // invalid bytecode.
            // So we ignore the error and output bytecode without abi.
            if let Ok(module) = CompiledModule::deserialize(self.bytecode.inner()) {
                self.abi = Some(module.try_into()?);
            }
        }
        Ok(self)
    }
}

impl From<Module> for MoveModuleBytecode {
    fn from(m: Module) -> Self {
        Self::new(m.into_inner())
    }
}

/// Move script bytecode
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct MoveScriptBytecode {
    pub bytecode: HexEncodedBytes,
    // We don't need deserialize MoveModule as it should be serialized
    // from `bytecode`.
    #[serde(skip_deserializing)]
    pub abi: Option<MoveFunction>,
}

impl VerifyInput for MoveScriptBytecode {
    fn verify(&self) -> anyhow::Result<()> {
        if self.bytecode.is_empty() {
            bail!("Move script bytecode is empty")
        }

        Ok(())
    }
}

impl MoveScriptBytecode {
    pub fn new(bytes: Vec<u8>) -> Self {
        Self {
            bytecode: bytes.into(),
            abi: None,
        }
    }

    pub fn try_parse_abi(mut self) -> Self {
        if self.abi.is_none() {
            // ignore error, because it is possible a transaction script payload contains
            // invalid bytecode.
            // So we ignore the error and output bytecode without abi.
            if let Ok(script) = CompiledScript::deserialize(self.bytecode.inner()) {
                self.abi = Some((&script).into());
            }
        }
        self
    }
}

/// Entry function id
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EntryFunctionId {
    pub module: MoveModuleId,
    pub name: IdentifierWrapper,
}

impl VerifyInput for EntryFunctionId {
    fn verify(&self) -> anyhow::Result<()> {
        self.module
            .verify()
            .map_err(|_| invalid_entry_function_id(self))?;
        self.name
            .verify()
            .map_err(|_| invalid_entry_function_id(self))
    }
}

impl FromStr for EntryFunctionId {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some((module, name)) = s.rsplit_once("::") {
            return Ok(Self {
                module: module.parse().map_err(|_| invalid_entry_function_id(s))?,
                name: name.parse().map_err(|_| invalid_entry_function_id(s))?,
            });
        }
        Err(invalid_entry_function_id(s))
    }
}

#[inline]
fn invalid_entry_function_id<S: Display + Sized>(s: S) -> anyhow::Error {
    format_err!("Invalid entry function ID {}", s)
}

impl Serialize for EntryFunctionId {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.to_string().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for EntryFunctionId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let entry_fun_id = <String>::deserialize(deserializer)?;
        entry_fun_id.parse().map_err(D::Error::custom)
    }
}

impl fmt::Display for EntryFunctionId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}::{}", self.module, self.name)
    }
}

pub fn verify_function_identifier(function: &str) -> anyhow::Result<()> {
    verify_identifier(function).map_err(|_| format_err!("invalid Move function name: {}", function))
}
pub fn verify_module_identifier(module: &str) -> anyhow::Result<()> {
    verify_identifier(module).map_err(|_| format_err!("invalid Move module name: {}", module))
}

pub fn verify_field_identifier(field: &str) -> anyhow::Result<()> {
    verify_identifier(field).map_err(|_| format_err!("invalid Move field name: {}", field))
}

pub fn verify_identifier(identifier: &str) -> anyhow::Result<()> {
    if identifier.contains("::") {
        Err(format_err!(
            "Identifier should not contain '::' {}",
            identifier
        ))
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use aptos_types::account_address::AccountAddress;
    use move_binary_format::file_format::AbilitySet;
    use move_core_types::{
        identifier::Identifier,
        language_storage::{StructTag, TypeTag},
    };
    use move_resource_viewer::{AnnotatedMoveStruct, AnnotatedMoveValue};

    use serde::{de::DeserializeOwned, Serialize};
    use serde_json::{json, to_value, Value};
    use std::{boxed::Box, convert::TryFrom, fmt::Debug};

    #[test]
    fn test_serialize_move_type_tag() {
        use TypeTag::*;
        fn assert_serialize(t: TypeTag, expected: Value) {
            let value = to_value(MoveType::from(t)).unwrap();
            assert_json(value, expected)
        }
        assert_serialize(Bool, json!("bool"));
        assert_serialize(U8, json!("u8"));
        assert_serialize(U64, json!("u64"));
        assert_serialize(U128, json!("u128"));
        assert_serialize(Address, json!("address"));
        assert_serialize(Signer, json!("signer"));

        assert_serialize(Vector(Box::new(U8)), json!("vector<u8>"));

        assert_serialize(
            Struct(Box::new(create_nested_struct())),
            json!("0x1::Home::ABC<address, 0x1::account::Base<u128, vector<u64>, vector<0x1::type::String>, 0x1::type::String>>"),
        );
    }

    #[test]
    fn test_serialize_move_resource() {
        use AnnotatedMoveValue::*;

        let res = MoveResource::try_from(annotated_move_struct(
            "Values",
            vec![
                (identifier("field_u8"), U8(7)),
                (identifier("field_u64"), U64(7)),
                (identifier("field_u128"), U128(7)),
                (identifier("field_bool"), Bool(true)),
                (identifier("field_address"), Address(address("0xdd"))),
                (
                    identifier("field_vector"),
                    Vector(TypeTag::U128, vec![U128(128)]),
                ),
                (identifier("field_bytes"), Bytes(vec![9, 9])),
                (
                    identifier("field_struct"),
                    Struct(annotated_move_struct(
                        "Nested",
                        vec![(
                            identifier("nested_vector"),
                            Vector(
                                TypeTag::Struct(Box::new(type_struct("Host"))),
                                vec![Struct(annotated_move_struct(
                                    "String",
                                    vec![
                                        (identifier("address1"), Address(address("0x0"))),
                                        (identifier("address2"), Address(address("0x123"))),
                                    ],
                                ))],
                            ),
                        )],
                    )),
                ),
            ],
        ))
        .unwrap();
        let value = to_value(&res).unwrap();
        assert_json(
            value,
            json!({
                "type": "0x1::type::Values",
                "data": {
                    "field_u8": 7,
                    "field_u64": "7",
                    "field_u128": "7",
                    "field_bool": true,
                    "field_address": "0xdd",
                    "field_vector": ["128"],
                    "field_bytes": "0x0909",
                    "field_struct": {
                        "nested_vector": [{"address1": "0x0", "address2": "0x123"}]
                    },
                }
            }),
        );
    }

    #[test]
    fn test_serialize_move_resource_with_address_0x0() {
        let res = MoveResource::try_from(annotated_move_struct(
            "Values",
            vec![(
                identifier("address_0x0"),
                AnnotatedMoveValue::Address(address("0x0")),
            )],
        ))
        .unwrap();
        let value = to_value(&res).unwrap();
        assert_json(
            value,
            json!({
                "type": "0x1::type::Values",
                "data": {
                    "address_0x0": "0x0",
                }
            }),
        );
    }

    #[test]
    fn test_serialize_deserialize_u64() {
        test_serialize_deserialize(U64::from(u64::MAX), json!(u64::MAX.to_string()))
    }

    #[test]
    fn test_serialize_deserialize_u128() {
        test_serialize_deserialize(U128::from(u128::MAX), json!(u128::MAX.to_string()))
    }

    #[test]
    fn test_serialize_deserialize_move_module_id() {
        test_serialize_deserialize(
            MoveModuleId {
                address: "0x1".parse().unwrap(),
                name: "Aptos".parse().unwrap(),
            },
            json!("0x1::Aptos"),
        );
    }

    #[test]
    fn test_parse_invalid_move_module_id_string() {
        assert_eq!(
            "Invalid Move module ID: 0x1",
            "0x1".parse::<MoveModuleId>().err().unwrap().to_string()
        );
        assert_eq!(
            "Invalid Move module ID: 0x1:",
            "0x1:".parse::<MoveModuleId>().err().unwrap().to_string()
        );
        assert_eq!(
            "Invalid Move module ID: 0x1:::",
            "0x1:::".parse::<MoveModuleId>().err().unwrap().to_string()
        );
        assert_eq!(
            "Invalid Move module ID: 0x1::???",
            "0x1::???"
                .parse::<MoveModuleId>()
                .err()
                .unwrap()
                .to_string()
        );
        assert_eq!(
            "Invalid Move module ID: Aptos::Aptos",
            "Aptos::Aptos"
                .parse::<MoveModuleId>()
                .err()
                .unwrap()
                .to_string()
        );
        assert_eq!(
            "Invalid Move module ID: 0x1::Aptos::Aptos",
            "0x1::Aptos::Aptos"
                .parse::<MoveModuleId>()
                .err()
                .unwrap()
                .to_string()
        );
    }

    #[test]
    fn test_serialize_deserialize_move_entry_function_id() {
        test_serialize_deserialize(
            EntryFunctionId {
                module: MoveModuleId {
                    address: "0x1".parse().unwrap(),
                    name: "Aptos".parse().unwrap(),
                },
                name: "Add".parse().unwrap(),
            },
            json!("0x1::Aptos::Add"),
        );
    }

    #[test]
    fn test_parse_invalid_move_entry_function_id_string() {
        assert_eq!(
            "Invalid entry function ID 0x1",
            "0x1".parse::<EntryFunctionId>().err().unwrap().to_string()
        );
        assert_eq!(
            "Invalid entry function ID 0x1:",
            "0x1:".parse::<EntryFunctionId>().err().unwrap().to_string()
        );
        assert_eq!(
            "Invalid entry function ID 0x1:::",
            "0x1:::"
                .parse::<EntryFunctionId>()
                .err()
                .unwrap()
                .to_string()
        );
        assert_eq!(
            "Invalid entry function ID 0x1::???",
            "0x1::???"
                .parse::<EntryFunctionId>()
                .err()
                .unwrap()
                .to_string()
        );
        assert_eq!(
            "Invalid entry function ID Aptos::Aptos",
            "Aptos::Aptos"
                .parse::<EntryFunctionId>()
                .err()
                .unwrap()
                .to_string()
        );
        assert_eq!(
            "Invalid entry function ID Aptos::Aptos::??",
            "Aptos::Aptos::??"
                .parse::<EntryFunctionId>()
                .err()
                .unwrap()
                .to_string()
        );
        assert_eq!(
            "Invalid entry function ID 0x1::Aptos::Aptos::Aptos",
            "0x1::Aptos::Aptos::Aptos"
                .parse::<EntryFunctionId>()
                .err()
                .unwrap()
                .to_string()
        );
    }

    #[test]
    fn test_serialize_deserialize_hex_encoded_bytes() {
        let bytes = hex::decode("abcd").unwrap();
        test_serialize_deserialize(HexEncodedBytes::from(bytes), json!("0xabcd"))
    }

    fn test_serialize_deserialize<O>(obj: O, expected: Value)
    where
        O: Serialize + DeserializeOwned + PartialEq + Debug,
    {
        let val = serde_json::to_value(&obj).unwrap();
        assert_eq!(val, expected);

        let data: O = serde_json::from_value(val).unwrap();
        assert_eq!(data, obj);
    }

    fn create_nested_struct() -> StructTag {
        let account = create_generic_type_struct();
        StructTag {
            address: address("0x1"),
            module: identifier("Home"),
            name: identifier("ABC"),
            type_params: vec![TypeTag::Address, TypeTag::Struct(Box::new(account))],
        }
    }

    fn create_generic_type_struct() -> StructTag {
        StructTag {
            address: address("0x1"),
            module: identifier("account"),
            name: identifier("Base"),
            type_params: vec![
                TypeTag::U128,
                TypeTag::Vector(Box::new(TypeTag::U64)),
                TypeTag::Vector(Box::new(TypeTag::Struct(Box::new(type_struct("String"))))),
                TypeTag::Struct(Box::new(type_struct("String"))),
            ],
        }
    }

    fn type_struct(t: &str) -> StructTag {
        StructTag {
            address: address("0x1"),
            module: identifier("type"),
            name: identifier(t),
            type_params: vec![],
        }
    }

    fn address(hex: &str) -> AccountAddress {
        AccountAddress::from_hex_literal(hex).unwrap()
    }

    fn annotated_move_struct(
        typ: &str,
        values: Vec<(Identifier, AnnotatedMoveValue)>,
    ) -> AnnotatedMoveStruct {
        AnnotatedMoveStruct {
            abilities: AbilitySet::EMPTY,
            type_: type_struct(typ),
            value: values,
        }
    }

    fn identifier(id: &str) -> Identifier {
        Identifier::new(id).unwrap()
    }

    fn assert_json(ret: Value, expected: Value) {
        assert_eq!(
            &ret,
            &expected,
            "\nexpected: {}, \nbut got: {}",
            pretty(&expected),
            pretty(&ret)
        )
    }

    fn pretty(val: &Value) -> String {
        serde_json::to_string_pretty(val).unwrap()
    }
}


text:
from collections import OrderedDict
from csv import DictWriter
from datetime import datetime
from functools import reduce
import operator
import sys

import numpy
from tabulate import tabulate

from .._compat import pickle
from .util import ansi
from .util import get_conv_infos
from .util import is_conv2d

class PrintLog:
    def __init__(self):
        self.first_iteration = True

    def __call__(self, nn, train_history):
        print(self.table(nn, train_history))
        sys.stdout.flush()

    def table(self, nn, train_history):
        info = train_history[-1]

        info_tabulate = OrderedDict([
            ('epoch', info['epoch']),
            ('trn loss', "{}{:.5f}{}".format(
                ansi.CYAN if info['train_loss_best'] else "",
                info['train_loss'],
                ansi.ENDC if info['train_loss_best'] else "",
                )),
            ('val loss', "{}{:.5f}{}".format(
                ansi.GREEN if info['valid_loss_best'] else "",
                info['valid_loss'],
                ansi.ENDC if info['valid_loss_best'] else "",
                )),
            ('trn/val', info['train_loss'] / info['valid_loss']),
            ])

        if not nn.regression:
            info_tabulate['valid acc'] = info['valid_accuracy']

        for name, func in nn.scores_train:
            info_tabulate[name] = info[name]

        for name, func in nn.scores_valid:
            info_tabulate[name] = info[name]

        if nn.custom_scores:
            for custom_score in nn.custom_scores:
                info_tabulate[custom_score[0]] = info[custom_score[0]]

        info_tabulate['dur'] = "{:.2f}s".format(info['dur'])

        tabulated = tabulate(
            [info_tabulate], headers="keys", floatfmt='.5f')

        out = ""
        if self.first_iteration:
            out = "\n".join(tabulated.split('\n', 2)[:2])
            out += "\n"
            self.first_iteration = False

        out += tabulated.rsplit('\n', 1)[-1]
        return out

class SaveWeights:
    def __init__(self, path, every_n_epochs=1, only_best=False,
                 pickle=False, verbose=0):
        self.path = path
        self.every_n_epochs = every_n_epochs
        self.only_best = only_best
        self.pickle = pickle
        self.verbose = verbose

    def __call__(self, nn, train_history):
        if self.only_best:
            this_loss = train_history[-1]['valid_loss']
            best_loss = min([h['valid_loss'] for h in train_history])
            if this_loss > best_loss:
                return

        if train_history[-1]['epoch'] % self.every_n_epochs != 0:
            return

        format_args = {
            'loss': train_history[-1]['valid_loss'],
            'timestamp': datetime.now().strftime('%Y-%m-%d-%H-%M-%S'),
            'epoch': '{:04d}'.format(train_history[-1]['epoch']),
            }
        path = self.path.format(**format_args)

        if self.verbose:
            print("Writing {}".format(path))

        if self.pickle:
            with open(path, 'wb') as f:
                pickle.dump(nn, f, -1)
        else:
            nn.save_params_to(path)

class RestoreBestWeights:
    def __init__(self, remember=None):
        self.remember = remember

    def __call__(self, nn, train_history):
        if self.remember is None:
            [self.remember] = [handler for handler in nn.on_epoch_finished
                               if isinstance(handler, RememberBestWeights)]
        nn.load_params_from(self.remember.best_weights)
        if self.remember.verbose:
            print("Loaded best weights from epoch {} where {} was {}".format(
                self.remember.best_weights_epoch,
                self.remember.score or self.remember.loss,
                self.remember.best_weights_loss * (
                    -1 if self.remember.score else 1),
                ))

_RestoreBestWeights = RestoreBestWeights

class RememberBestWeights:
    def __init__(self, loss='valid_loss', score=None, verbose=1):
        self.loss = loss
        self.score = score
        self.verbose = verbose
        self.best_weights = None
        self.best_weights_loss = sys.maxsize
        self.best_weights_epoch = None

    def __call__(self, nn, train_history):
        key = self.score if self.score is not None else self.loss

        curr_loss = train_history[-1][key]
        if self.score:
            curr_loss *= -1

        if curr_loss < self.best_weights_loss:
            self.best_weights = nn.get_all_params_values()
            self.best_weights_loss = curr_loss
            self.best_weights_epoch = train_history[-1]['epoch']

class PrintLayerInfo:
    def __init__(self):
        pass

    def __call__(self, nn, train_history=None):
        if train_history:
            return

        message = self._get_greeting(nn)
        print(message)
        print("## Layer information")
        print("")

        layers_contain_conv2d = is_conv2d(nn.layers_.values())
        if not layers_contain_conv2d or (nn.verbose < 2):
            layer_info = self._get_layer_info_plain(nn)
            legend = None
        else:
            layer_info, legend = self._get_layer_info_conv(nn)
        print(layer_info)
        if legend is not None:
            print(legend)
        print("")
        sys.stdout.flush()

    @staticmethod
    def _get_greeting(nn):
        shapes = [param.get_value().shape for param in
                  nn.get_all_params(trainable=True) if param]
        nparams = reduce(operator.add, [reduce(operator.mul, shape) for
                                        shape in shapes])
        message = ("# Neural Network with {} learnable parameters"
                   "\n".format(nparams))
        return message

    @staticmethod
    def _get_layer_info_plain(nn):
        nums = list(range(len(nn.layers_)))
        names = [layer.name for layer in nn.layers_.values()]
        output_shapes = ['x'.join(map(str, layer.output_shape[1:]))
                         for layer in nn.layers_.values()]

        table = OrderedDict([
            ('#', nums),
            ('name', names),
            ('size', output_shapes),
        ])
        layer_infos = tabulate(table, 'keys')
        return layer_infos

    @staticmethod
    def _get_layer_info_conv(nn):
        if nn.verbose > 2:
            detailed = True
        else:
            detailed = False

        layer_infos = get_conv_infos(nn, detailed=detailed)

        mag = "{}{}{}".format(ansi.MAGENTA, "magenta", ansi.ENDC)
        cya = "{}{}{}".format(ansi.CYAN, "cyan", ansi.ENDC)
        red = "{}{}{}".format(ansi.RED, "red", ansi.ENDC)
        legend = (
            "\nExplanation"
            "\n    X, Y:    image dimensions"
            "\n    cap.:    learning capacity"
            "\n    cov.:    coverage of image"
            "\n    {}: capacity too low (<1/6)"
            "\n    {}:    image coverage too high (>100%)"
            "\n    {}:     capacity too low and coverage too high\n"
            "".format(mag, cya, red)
            )

        return layer_infos, legend

class WeightLog:
    """Keep a log of your network's weights and weight changes.

    Pass instances of :class:`WeightLog` as an `on_batch_finished`
    handler into your network.
    """
    def __init__(self, save_to=None, write_every=8):
        """
        :param save_to: If given, `save_to` must be a path into which
                        I will write weight statistics in CSV format.
        """
        self.last_weights = None
        self.history = []
        self.save_to = save_to
        self.write_every = write_every
        self._dictwriter = None
        self._save_to_file = None

    def __call__(self, nn, train_history):
        weights = nn.get_all_params_values()

        if self.save_to and self._dictwriter is None:
            fieldnames = []
            for key in weights.keys():
                for i, p in enumerate(weights[key]):
                    fieldnames.extend([
                        '{}_{} wdiff'.format(key, i),
                        '{}_{} wabsmean'.format(key, i),
                        '{}_{} wmean'.format(key, i),
                        ])

            newfile = self.last_weights is None
            if newfile:
                self._save_to_file = open(self.save_to, 'w')
            else:
                self._save_to_file = open(self.save_to, 'a')
            self._dictwriter = DictWriter(self._save_to_file, fieldnames)
            if newfile:
                self._dictwriter.writeheader()

        entry = {}
        lw = self.last_weights if self.last_weights is not None else weights
        for key in weights.keys():
            for i, (p1, p2) in enumerate(zip(lw[key], weights[key])):
                wdiff = numpy.abs(p1 - p2).mean()
                wabsmean = numpy.abs(p2).mean()
                wmean = p2.mean()
                entry.update({
                    '{}_{} wdiff'.format(key, i): wdiff,
                    '{}_{} wabsmean'.format(key, i): wabsmean,
                    '{}_{} wmean'.format(key, i): wmean,
                    })
        self.history.append(entry)

        if self.save_to:
            if len(self.history) % self.write_every == 0:
                self._dictwriter.writerows(self.history[-self.write_every:])
                self._save_to_file.flush()

        self.last_weights = weights

    def __getstate__(self):
        state = dict(self.__dict__)
        state['_save_to_file'] = None
        state['_dictwriter'] = None
        return state


text:
package org.broadinstitute.dsde.vault.services.uBAMCollection

import com.wordnik.swagger.annotations._
import org.broadinstitute.dsde.vault.DmClientService
import org.broadinstitute.dsde.vault.model.uBAMCollectionJsonProtocol._
import org.broadinstitute.dsde.vault.model.{UBamCollectionIngest, UBamCollectionIngestResponse}
import org.broadinstitute.dsde.vault.services.VaultDirectives
import spray.httpx.SprayJsonSupport._
import spray.routing.HttpService

@Api(value = "/ubamcollections", description = "Collections Service", produces = "application/json")
trait UBamCollectionIngestService extends HttpService with VaultDirectives {

  private final val ApiPrefix = "ubamcollections"
  private final val ApiVersions = "v1"

  val ubciRoute = uBAMCollectionIngestRoute

  @ApiOperation(
    value = "Creates an UBam collection object",
    nickname = "ubam_collection_ingest",
    httpMethod = "POST",
    produces = "application/json",
    consumes = "application/json",
    response = classOf[UBamCollectionIngestResponse],
    notes = "Accepts a json packet as POST. Creates a Vault collection object with the supplied ubam ids and the supplied metadata. " +
      " Returns the Vault ID of the created object.")
  @ApiImplicitParams(Array(
    new ApiImplicitParam(name = "version", required = true, dataType = "string", paramType = "path", value = "API version", allowableValues = ApiVersions),
    new ApiImplicitParam(name = "body", required = true, dataType = "org.broadinstitute.dsde.vault.model.UBamCollectionIngest", paramType = "body", value = "Collection to create")
  ))
  @ApiResponses(Array(
    new ApiResponse(code = 200, message = "Successful"),
    new ApiResponse(code = 400, message = "Malformed Input"),
    new ApiResponse(code = 500, message = "Vault Internal Error")
  ))
  def uBAMCollectionIngestRoute =
    path( ApiPrefix / "v" ~ IntNumber) { version =>
      post {
        respondWithJSON {
            entity(as[UBamCollectionIngest]) {
              ingest =>
                requestContext =>
                  val dmService = actorRefFactory.actorOf(DmClientService.props(requestContext))
                  val ingestActor = actorRefFactory.actorOf(IngestServiceHandler.props(requestContext, version, dmService))
                  ingestActor ! IngestServiceHandler.IngestMessage(ingest)
            }
          }
        }
      }
}