Use std::map<> in CPLST_Select

Replace integer state value with an enum.


Change-Id: I78eacf90465692946640ea5a031af03259fd417f
Reviewed-on: https://pdfium-review.googlesource.com/3155
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fxedit/fxet_list.cpp b/fpdfsdk/fxedit/fxet_list.cpp
index b10bb62..c10b6a2 100644
--- a/fpdfsdk/fxedit/fxet_list.cpp
+++ b/fpdfsdk/fxedit/fxet_list.cpp
@@ -6,6 +6,7 @@
 
 #include "fpdfsdk/fxedit/fxet_list.h"
 
+#include <algorithm>
 #include <utility>
 
 #include "core/fpdfdoc/cpvt_word.h"
@@ -86,108 +87,46 @@
 
 CPLST_Select::CPLST_Select() {}
 
-CPLST_Select::~CPLST_Select() {
-  for (int32_t i = 0, sz = m_aItems.GetSize(); i < sz; i++)
-    delete m_aItems.GetAt(i);
-
-  m_aItems.RemoveAll();
-}
+CPLST_Select::~CPLST_Select() {}
 
 void CPLST_Select::Add(int32_t nItemIndex) {
-  int32_t nIndex = Find(nItemIndex);
-
-  if (nIndex < 0) {
-    m_aItems.Add(new CPLST_Select_Item(nItemIndex, 1));
-  } else {
-    if (CPLST_Select_Item* pItem = m_aItems.GetAt(nIndex)) {
-      pItem->nState = 1;
-    }
-  }
+  m_Items[nItemIndex] = SELECTING;
 }
 
 void CPLST_Select::Add(int32_t nBeginIndex, int32_t nEndIndex) {
-  if (nBeginIndex > nEndIndex) {
-    int32_t nTemp = nEndIndex;
-    nEndIndex = nBeginIndex;
-    nBeginIndex = nTemp;
-  }
+  if (nBeginIndex > nEndIndex)
+    std::swap(nBeginIndex, nEndIndex);
 
-  for (int32_t i = nBeginIndex; i <= nEndIndex; i++)
+  for (int32_t i = nBeginIndex; i <= nEndIndex; ++i)
     Add(i);
 }
 
 void CPLST_Select::Sub(int32_t nItemIndex) {
-  for (int32_t i = m_aItems.GetSize() - 1; i >= 0; i--) {
-    if (CPLST_Select_Item* pItem = m_aItems.GetAt(i))
-      if (pItem->nItemIndex == nItemIndex)
-        pItem->nState = -1;
-  }
+  auto it = m_Items.find(nItemIndex);
+  if (it != m_Items.end())
+    it->second = DESELECTING;
 }
 
 void CPLST_Select::Sub(int32_t nBeginIndex, int32_t nEndIndex) {
-  if (nBeginIndex > nEndIndex) {
-    int32_t nTemp = nEndIndex;
-    nEndIndex = nBeginIndex;
-    nBeginIndex = nTemp;
-  }
+  if (nBeginIndex > nEndIndex)
+    std::swap(nBeginIndex, nEndIndex);
 
-  for (int32_t i = nBeginIndex; i <= nEndIndex; i++)
+  for (int32_t i = nBeginIndex; i <= nEndIndex; ++i)
     Sub(i);
 }
 
-int32_t CPLST_Select::Find(int32_t nItemIndex) const {
-  for (int32_t i = 0, sz = m_aItems.GetSize(); i < sz; i++) {
-    if (CPLST_Select_Item* pItem = m_aItems.GetAt(i)) {
-      if (pItem->nItemIndex == nItemIndex)
-        return i;
-    }
-  }
-
-  return -1;
-}
-
-bool CPLST_Select::IsExist(int32_t nItemIndex) const {
-  return Find(nItemIndex) >= 0;
-}
-
-int32_t CPLST_Select::GetCount() const {
-  return m_aItems.GetSize();
-}
-
-int32_t CPLST_Select::GetItemIndex(int32_t nIndex) const {
-  if (nIndex >= 0 && nIndex < m_aItems.GetSize())
-    if (CPLST_Select_Item* pItem = m_aItems.GetAt(nIndex))
-      return pItem->nItemIndex;
-
-  return -1;
-}
-
-int32_t CPLST_Select::GetState(int32_t nIndex) const {
-  if (nIndex >= 0 && nIndex < m_aItems.GetSize())
-    if (CPLST_Select_Item* pItem = m_aItems.GetAt(nIndex))
-      return pItem->nState;
-
-  return 0;
-}
-
 void CPLST_Select::DeselectAll() {
-  for (int32_t i = 0, sz = m_aItems.GetSize(); i < sz; i++) {
-    if (CPLST_Select_Item* pItem = m_aItems.GetAt(i)) {
-      pItem->nState = -1;
-    }
-  }
+  for (auto& item : m_Items)
+    item.second = DESELECTING;
 }
 
 void CPLST_Select::Done() {
-  for (int32_t i = m_aItems.GetSize() - 1; i >= 0; i--) {
-    if (CPLST_Select_Item* pItem = m_aItems.GetAt(i)) {
-      if (pItem->nState == -1) {
-        delete pItem;
-        m_aItems.RemoveAt(i);
-      } else {
-        pItem->nState = 0;
-      }
-    }
+  auto it = m_Items.begin();
+  while (it != m_Items.end()) {
+    if (it->second == DESELECTING)
+      it = m_Items.erase(it);
+    else
+      (it++)->second = NORMAL;
   }
 }
 
@@ -470,20 +409,10 @@
 }
 
 void CFX_ListCtrl::SelectItems() {
-  for (int32_t i = 0, sz = m_aSelItems.GetCount(); i < sz; i++) {
-    int32_t nItemIndex = m_aSelItems.GetItemIndex(i);
-    int32_t nState = m_aSelItems.GetState(i);
-
-    switch (nState) {
-      case 1:
-        SetMultipleSelect(nItemIndex, true);
-        break;
-      case -1:
-        SetMultipleSelect(nItemIndex, false);
-        break;
-    }
+  for (const auto& item : m_aSelItems) {
+    if (item.second != CPLST_Select::NORMAL)
+      SetMultipleSelect(item.first, item.second == CPLST_Select::SELECTING);
   }
-
   m_aSelItems.Done();
 }
 
diff --git a/fpdfsdk/fxedit/fxet_list.h b/fpdfsdk/fxedit/fxet_list.h
index 65426e0..d0fef1c 100644
--- a/fpdfsdk/fxedit/fxet_list.h
+++ b/fpdfsdk/fxedit/fxet_list.h
@@ -7,6 +7,7 @@
 #ifndef FPDFSDK_FXEDIT_FXET_LIST_H_
 #define FPDFSDK_FXEDIT_FXET_LIST_H_
 
+#include <map>
 #include <memory>
 #include <vector>
 
@@ -169,36 +170,26 @@
   CLST_Rect m_rcContent;  // positive forever!
 };
 
-struct CPLST_Select_Item {
-  CPLST_Select_Item(int32_t other_nItemIndex, int32_t other_nState) {
-    nItemIndex = other_nItemIndex;
-    nState = other_nState;
-  }
-
-  int32_t nItemIndex;
-  int32_t nState;  // 0:normal select -1:to deselect 1: to select
-};
-
 class CPLST_Select {
  public:
+  enum State { DESELECTING = -1, NORMAL = 0, SELECTING = 1 };
+  using const_iterator = std::map<int32_t, State>::const_iterator;
+
   CPLST_Select();
   virtual ~CPLST_Select();
 
- public:
   void Add(int32_t nItemIndex);
   void Add(int32_t nBeginIndex, int32_t nEndIndex);
   void Sub(int32_t nItemIndex);
   void Sub(int32_t nBeginIndex, int32_t nEndIndex);
-  bool IsExist(int32_t nItemIndex) const;
-  int32_t Find(int32_t nItemIndex) const;
-  int32_t GetCount() const;
-  int32_t GetItemIndex(int32_t nIndex) const;
-  int32_t GetState(int32_t nIndex) const;
-  void Done();
   void DeselectAll();
+  void Done();
+
+  const_iterator begin() const { return m_Items.begin(); }
+  const_iterator end() const { return m_Items.end(); }
 
  private:
-  CFX_ArrayTemplate<CPLST_Select_Item*> m_aItems;
+  std::map<int32_t, State> m_Items;
 };
 
 class CFX_ListCtrl : protected CFX_ListContainer {