/***************************************************************************

    source::worx xtree
    Copyright © 2024-2025 c.holzheuer
    christoph.holzheuer@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#ifndef XQMAPINDEX_H
#define XQMAPINDEX_H

#include <QMap>

/**
 * @brief Holds the string to index mapping for the QXMaptor classes
 */

class XQMapIndex : public QMap<QString, int>
{

public:

  virtual ~XQMapIndex() = default;

  void addKey(const QString& key, int index)
  {
    (*this)[key] = index;
  }

  int indexOf(const QString& key) const
  {
    if (contains(key))
      return (*this)[key];
    return -1;
  }


  void update(int index)
  {

    XQMapIndex newindex;
    QMapIterator<QString, int> iter(*this);

    while (iter.hasNext())
    {
      iter.next();

      // item idx == kill-index: continue
      // item idx <  kill-index: store item
      // item idx >  kill-index: decrement & store item

      int idx = iter.value();
      if (idx == index)
        continue;
      if (idx > index)
        idx--;
      // schlüssel auch sichern
      newindex[(iter.key())] = idx;

    }

    swap(newindex);

  }

};


#endif // XQMAPINDEX_H
