# all files / ol/structs/ linkedlist.js

99% Statements 99/100
85.71% Branches 48/56
100% Functions 13/13
99% Lines 99/100

    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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    2491×

    1×

    69×

    69×

    69×

    69×

    69×

    1×

    160×

    160×

    160×
    52×
    52×
    52×
    51×
    51×

    108×
    108×
    108×
    108×
    108×
    106×

    108×
    108×

    160×
    160×

    1×
    20×
    20×
    20×
    20×
    20×
    19×

    20×
    20×

    20×

    20×
    1×
    1×
    1×
    19×

    19×
    19×

    20×

    1×
    53×
    53×
    52×

    1×

    1×
    2×
    2×
    1×

    1×

    1×
    200×
    199×
    199×

    1×

    1×
    15×
    14×

    1×

    1×
    18×
    17×
    17×

    1×

    1×
    15×
    14×

    1×

    1×
    31×
    30×

    1×

    1×
    1×
    1×
    1×

    1×
    3×
    3×
    1×
    1×
    1×
    1×
    1×
    1×

    2×
    2×
    2×
    2×

    3×
    3×
    3×
    3×

    1×
    28×

     goog.provide('ol.structs.LinkedList');

    /**
     * Creates an empty linked list structure.
     *
     * @constructor
     * @struct
     * @param {boolean=} opt_circular The last item is connected to the first one,
     * and the first item to the last one. Default is true.
     */
    ol.structs.LinkedList = function(opt_circular) {

      /**
       * @private
       * @type {ol.LinkedListItem|undefined}
       */
      this.first_ = undefined;

      /**
       * @private
       * @type {ol.LinkedListItem|undefined}
       */
      this.last_ = undefined;

      /**
       * @private
       * @type {ol.LinkedListItem|undefined}
       */
      this.head_ = undefined;

      /**
       * @private
       * @type {boolean}
       */
      this.circular_ = opt_circular === undefined ? true : opt_circular;

      /**
       * @private
       * @type {number}
       */
      this.length_ = 0;
    };

    /**
     * Inserts an item into the linked list right after the current one.
     *
     * @param {?} data Item data.
     */
    ol.structs.LinkedList.prototype.insertItem = function(data) {

      /** @type {ol.LinkedListItem} */
      var item = {
        prev: undefined,
        next: undefined,
        data: data
      };

      var head = this.head_;

      //Initialize the list.
      if (!head) {
        this.first_ = item;
        this.last_ = item;
        if (this.circular_) {
          item.next = item;
          item.prev = item;
        }
      } else {
        //Link the new item to the adjacent ones.
        var next = head.next;
        item.prev = head;
        item.next = next;
        head.next = item;
        if (next) {
          next.prev = item;
        }

        Eif (head === this.last_) {
          this.last_ = item;
        }
      }
      this.head_ = item;
      this.length_++;
    };

    /**
     * Removes the current item from the list. Sets the cursor to the next item,
     * if possible.
     */
    ol.structs.LinkedList.prototype.removeItem = function() {
      var head = this.head_;
      Eif (head) {
        var next = head.next;
        var prev = head.prev;
        if (next) {
          next.prev = prev;
        }
        Eif (prev) {
          prev.next = next;
        }
        this.head_ = next || prev;

        if (this.first_ === this.last_) {
          this.head_ = undefined;
          this.first_ = undefined;
          this.last_ = undefined;
        } else Iif (this.first_ === head) {
          this.first_ = this.head_;
        } else Eif (this.last_ === head) {
          this.last_ = prev ? this.head_.prev : this.head_;
        }
        this.length_--;
      }
    };

    /**
     * Sets the cursor to the first item, and returns the associated data.
     *
     * @return {?} Item data.
     */
    ol.structs.LinkedList.prototype.firstItem = function() {
      this.head_ = this.first_;
      if (this.head_) {
        return this.head_.data;
      }
      return undefined;
    };

    /**
    * Sets the cursor to the last item, and returns the associated data.
    *
    * @return {?} Item data.
    */
    ol.structs.LinkedList.prototype.lastItem = function() {
      this.head_ = this.last_;
      if (this.head_) {
        return this.head_.data;
      }
      return undefined;
    };

    /**
     * Sets the cursor to the next item, and returns the associated data.
     *
     * @return {?} Item data.
     */
    ol.structs.LinkedList.prototype.nextItem = function() {
      if (this.head_ && this.head_.next) {
        this.head_ = this.head_.next;
        return this.head_.data;
      }
      return undefined;
    };

    /**
     * Returns the next item's data without moving the cursor.
     *
     * @return {?} Item data.
     */
    ol.structs.LinkedList.prototype.getNextItem = function() {
      if (this.head_ && this.head_.next) {
        return this.head_.next.data;
      }
      return undefined;
    };

    /**
     * Sets the cursor to the previous item, and returns the associated data.
     *
     * @return {?} Item data.
     */
    ol.structs.LinkedList.prototype.prevItem = function() {
      if (this.head_ && this.head_.prev) {
        this.head_ = this.head_.prev;
        return this.head_.data;
      }
      return undefined;
    };

    /**
     * Returns the previous item's data without moving the cursor.
     *
     * @return {?} Item data.
     */
    ol.structs.LinkedList.prototype.getPrevItem = function() {
      if (this.head_ && this.head_.prev) {
        return this.head_.prev.data;
      }
      return undefined;
    };

    /**
     * Returns the current item's data.
     *
     * @return {?} Item data.
     */
    ol.structs.LinkedList.prototype.getCurrItem = function() {
      if (this.head_) {
        return this.head_.data;
      }
      return undefined;
    };

    /**
     * Sets the first item of the list. This only works for circular lists, and sets
     * the last item accordingly.
     */
    ol.structs.LinkedList.prototype.setFirstItem = function() {
      Eif (this.circular_ && this.head_) {
        this.first_ = this.head_;
        this.last_ = this.head_.prev;
      }
    };

    /**
     * Concatenates two lists.
     * @param {ol.structs.LinkedList} list List to merge into the current list.
     */
    ol.structs.LinkedList.prototype.concat = function(list) {
      Eif (list.head_) {
        if (this.head_) {
          var end = this.head_.next;
          this.head_.next = list.first_;
          list.first_.prev = this.head_;
          end.prev = list.last_;
          list.last_.next = end;
          this.length_ += list.length_;
        } else {
          this.head_ = list.head_;
          this.first_ = list.first_;
          this.last_ = list.last_;
          this.length_ = list.length_;
        }
        list.head_ = undefined;
        list.first_ = undefined;
        list.last_ = undefined;
        list.length_ = 0;
      }
    };

    /**
     * Returns the current length of the list.
     *
     * @return {number} Length.
     */
    ol.structs.LinkedList.prototype.getLength = function() {
      return this.length_;
    };