# all files / technicalindicators/lib/Utils/ FixedSizeLinkedList.js

98.04% Statements 50/51
90% Branches 27/30
100% Functions 6/6
98.04% Lines 50/51

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80

    1×

    1×

    1×
    57×

    57×
    57×
    57×
    57×
    57×
    57×
    57×
    57×
    1645×

    1×

    1×
    1645×
    917×
    917×

    917×
    149×
    9×
    917×
    149×
    35×

    728×

    1645×
    341×
    140×
    1645×
    341×
    40×

    1×
    65×
    65×
    510×

    1×
    9×
    9×
    9×
    9×
    113×
    41×

    1×
    35×
    35×
    35×
    35×
    411×
    28×

    1×/**
     * Created by AAravindan on 5/7/16.
     */
    var LinkedList = require("linkedlist")

    var Util       = require('util');

    function FixedSizeLinkedList(size, maintainHigh, maintainLow) {
      if(I!size || typeof size !== 'number'){
        throw('Size required and should be a number.');
      }
      LinkedList.call(this);
      this.size = size;
      this.maintainHigh = maintainHigh;
      this.maintainLow  = maintainLow;
      this.periodHigh = 0;
      this.periodLow  = Infinity;
      this._push = this.push;
      this.push = function(data) {
        this.add(data);
      }
    }

    Util.inherits(FixedSizeLinkedList, LinkedList);

    FixedSizeLinkedList.prototype.add =  function(data) {
      if(this.length === this.size){
         this.lastShift = this.shift();
         this._push(data);

        //TODO: FInd a better way
         if(this.maintainHigh )
          if (this.lastShift == this.periodHigh)
            this.calculatePeriodHigh();
         if(this.maintainLow)
          if (this.lastShift == this.periodLow)
            this.calculatePeriodLow();
      }else {
        this._push(data);
      }

      //TODO: FInd a better way
      if(this.maintainHigh )
        if(this.periodHigh <= data)
          (this.periodHigh = data);
      if(this.maintainLow)
        if(this.periodLow >= data)
          (this.periodLow = data);
    };

    FixedSizeLinkedList.prototype.iterator = function *(linkedList) {
      this.resetCursor();
      while(this.next()){
        yield this.current;
      }
    };

    FixedSizeLinkedList.prototype.calculatePeriodHigh = function (linkedList) {
      this.resetCursor();
      if(Ethis.next())
        this.periodHigh = this.current;
      while(this.next()){
        if(this.periodHigh <= this.current){
          this.periodHigh = this.current;
        };
      };
    };

    FixedSizeLinkedList.prototype.calculatePeriodLow = function () {
      this.resetCursor();
      if(Ethis.next())
        this.periodLow = this.current;
      while(this.next()){
        if(this.periodLow >= this.current){
          this.periodLow = this.current;
        };
      };
    };

    module.exports = FixedSizeLinkedList;