hunk.code_window:                 <Logo color="#2C3E50" inline />
                 </SVGSet>
               </a>
               <a href="./" target="_self">
                 Docs
               </a>
               <a href="https://stackoverflow.com/questions/tagged/immutable.js?sort=votes">
                 Questions
               </a>
  
hunk.labels: keep keep keep keep replace keep keep keep keep
hunk.after_edit:               Docs (v{packageJson.version})
  
hunk.file_path: pages/src/docs/src/DocHeader.js
hunk.type: replace
file: /**
   * Copyright (c) 2014-present, Facebook, Inc.
   *
   * This source code is licensed under the MIT license found in the
   * LICENSE file in the root directory of this source tree.
   */
  
  import { KeyedCollection } from './Collection';
  import { IS_ORDERED_SENTINEL, isOrdered } from './Predicates';
  import { Map, isMap, emptyMap } from './Map';
  import { emptyList } from './List';
  import { DELETE, NOT_SET, SIZE } from './TrieUtils';
  import assertNotInfinite from './utils/assertNotInfinite';
  
  export class OrderedMap extends Map {
    // @pragma Construction
  
    constructor(value) {
      return value === null || value === undefined
        ? emptyOrderedMap()
        : isOrderedMap(value)
          ? value
          : emptyOrderedMap().withMutations(map => {
              const iter = KeyedCollection(value);
              assertNotInfinite(iter.size);
              iter.forEach((v, k) => map.set(k, v));
            });
    }
  
    static of(/*...values*/) {
      return this(arguments);
    }
  
    toString() {
      return this.__toString('OrderedMap {', '}');
    }
  
    // @pragma Access
  
    get(k, notSetValue) {
      const index = this._map.get(k);
      return index !== undefined ? this._list.get(index)[1] : notSetValue;
    }
  
    // @pragma Modification
  
    clear() {
      if (this.size === 0) {
        return this;
      }
      if (this.__ownerID) {
        this.size = 0;
        this._map.clear();
        this._list.clear();
        return this;
      }
      return emptyOrderedMap();
    }
  
    set(k, v) {
      return updateOrderedMap(this, k, v);
    }
  
    remove(k) {
      return updateOrderedMap(this, k, NOT_SET);
    }
  
    wasAltered() {
      return this._map.wasAltered() || this._list.wasAltered();
    }
  
    __iterate(fn, reverse) {
      return this._list.__iterate(
        entry => entry && fn(entry[1], entry[0], this),
        reverse
      );
    }
  
    __iterator(type, reverse) {
      return this._list.fromEntrySeq().__iterator(type, reverse);
    }
  
    __ensureOwner(ownerID) {
      if (ownerID === this.__ownerID) {
        return this;
      }
      const newMap = this._map.__ensureOwner(ownerID);
      const newList = this._list.__ensureOwner(ownerID);
      if (!ownerID) {
        if (this.size === 0) {
          return emptyOrderedMap();
        }
        this.__ownerID = ownerID;
        this._map = newMap;
        this._list = newList;
        return this;
      }
      return makeOrderedMap(newMap, newList, ownerID, this.__hash);
    }
  }
  
  function isOrderedMap(maybeOrderedMap) {
    return isMap(maybeOrderedMap) && isOrdered(maybeOrderedMap);
  }
  
  OrderedMap.isOrderedMap = isOrderedMap;
  
  OrderedMap.prototype[IS_ORDERED_SENTINEL] = true;
  OrderedMap.prototype[DELETE] = OrderedMap.prototype.remove;
  
  function makeOrderedMap(map, list, ownerID, hash) {
    const omap = Object.create(OrderedMap.prototype);
    omap.size = map ? map.size : 0;
    omap._map = map;
    omap._list = list;
    omap.__ownerID = ownerID;
    omap.__hash = hash;
    return omap;
  }
  
  let EMPTY_ORDERED_MAP;
  export function emptyOrderedMap() {
    return (
      EMPTY_ORDERED_MAP ||
      (EMPTY_ORDERED_MAP = makeOrderedMap(emptyMap(), emptyList()))
    );
  }
  
  function updateOrderedMap(omap, k, v) {
    const map = omap._map;
    const list = omap._list;
    const i = map.get(k);
    const has = i !== undefined;
    let newMap;
    let newList;
    if (v === NOT_SET) {
      // removed
      if (!has) {
        return omap;
      }
      if (list.size >= SIZE && list.size >= map.size * 2) {
        newList = list.filter((entry, idx) => entry !== undefined && i !== idx);
        newMap = newList
          .toKeyedSeq()
          .map(entry => entry[0])
          .flip()
          .toMap();
        if (omap.__ownerID) {
          newMap.__ownerID = newList.__ownerID = omap.__ownerID;
        }
      } else {
        newMap = map.remove(k);
        newList = i === list.size - 1 ? list.pop() : list.set(i, undefined);
      }
    } else if (has) {
      if (v === list.get(i)[1]) {
        return omap;
      }
      newMap = map;
      newList = list.set(i, [k, v]);
    } else {
      newMap = map.set(k, list.size);
      newList = list.set(list.size, [k, v]);
    }
    if (omap.__ownerID) {
      omap.size = newMap.size;
      omap._map = newMap;
      omap._list = newList;
      omap.__hash = undefined;
      return omap;
    }
    return makeOrderedMap(newMap, newList);
  }
  