1/* Copyright (C) 2021-2024 Free Software Foundation, Inc.
2   Contributed by Oracle.
3
4   This file is part of GNU Binutils.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, 51 Franklin Street - Fifth Floor, Boston,
19   MA 02110-1301, USA.  */
20
21/*
22 *	Index Map2D implementation.
23 *
24 *    Index Map2D is dynamic two dimensional array
25 */
26
27#ifndef _DBE_INDEXMAP2D_H
28#define _DBE_INDEXMAP2D_H
29
30#include <assert.h>
31#include <vec.h>
32#include <Map2D.h>
33
34template <typename Key1_t, typename Key2_t, typename Value_t>
35class IndexMap2D : public Map2D<Key1_t, Key2_t, Value_t>
36{
37public:
38
39  IndexMap2D ();
40  ~IndexMap2D ();
41
42  void put (Key1_t key1, Key2_t key2, Value_t val);
43  Value_t get (Key1_t key1, Key2_t key2);
44  Value_t get (Key1_t key1, Key2_t key2,
45	       typename Map2D<Key1_t, Key2_t, Value_t>::Relation rel);
46  Value_t remove (Key1_t key1, Key2_t key2);
47
48private:
49
50  Vector<Vector<Value_t>*> *map1;
51};
52
53template <typename Key1_t, typename Key2_t, typename Value_t>
54IndexMap2D<Key1_t, Key2_t, Value_t>::IndexMap2D ()
55{
56  map1 = new Vector<Vector<Value_t>*>;
57}
58
59template <typename Key1_t, typename Key2_t, typename Value_t>
60IndexMap2D<Key1_t, Key2_t, Value_t>::~IndexMap2D ()
61{
62  map1->destroy ();
63  delete map1;
64}
65
66template <typename Key1_t, typename Key2_t, typename Value_t>
67void
68IndexMap2D<Key1_t, Key2_t, Value_t>::put (Key1_t key1, Key2_t key2, Value_t val)
69{
70  if (key1 < 0 || key2 < 0)
71    return;
72  Vector<Value_t> *map2 = NULL;
73  if (key1 < map1->size ())
74    map2 = map1->fetch ((int) key1);
75  if (map2 == NULL)
76    {
77      map2 = new Vector<Value_t>;
78      map1->store ((int) key1, map2);
79    }
80  map2->store ((int) key2, val);
81}
82
83template <typename Key1_t, typename Key2_t, typename Value_t>
84Value_t
85IndexMap2D<Key1_t, Key2_t, Value_t>::get (Key1_t key1, Key2_t key2)
86{
87  if (key1 < 0 || key1 >= map1->size () || key2 < 0)
88    return (Value_t) 0;
89  Vector<Value_t> *map2 = map1->fetch ((int) key1);
90  if (map2 == NULL || key2 >= map2->size ())
91    return (Value_t) 0;
92  return map2->fetch ((int) key2);
93}
94
95template <typename Key1_t, typename Key2_t, typename Value_t>
96Value_t
97IndexMap2D<Key1_t, Key2_t, Value_t>::get (Key1_t key1, Key2_t key2,
98			  typename Map2D<Key1_t, Key2_t, Value_t>::Relation rel)
99{
100  if (rel != Map2D<Key1_t, Key2_t, Value_t>::REL_EQEQ)
101    return (Value_t) 0;
102  return get (key1, key2);
103}
104
105template <typename Key1_t, typename Key2_t, typename Value_t>
106Value_t
107IndexMap2D<Key1_t, Key2_t, Value_t>::remove (Key1_t key1, Key2_t key2)
108{
109  if (key1 < 0 || key1 >= map1->size () || key2 < 0)
110    return (Value_t) 0;
111  Vector<Value_t> *map2 = map1->fetch ((int) key1);
112  if (map2 == NULL || key2 >= map2->size ())
113    return (Value_t) 0;
114  Value_t res = map2->fetch ((int) key2);
115  map2->store ((int) key2, (Value_t) 0);
116  return res;
117}
118
119#endif
120