KISS

Keep It Simple Stupid

HashMapT in Bada

| comments

Hash map (a.k.a. dictionary) is a useful and convenient data structure. Bada SDK provides HashMap and HashMapT classes implementing it. I normally used the templated version of it, HashMapT. However, I came across the fact that it supported neither base class String nor DateTime out-of-box.

The following classes, children of IHashCodeProviderT<> and IComparerT<>, solve this misunderstanding:

HashMapTExtensions.h
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
/*
 * HashMapTExtensions.h
 */

#ifndef HASHMAPTEXTENSIONS_H_
#define HASHMAPTEXTENSIONS_H_

#include <FBase.h>

// The following *KeyHashCodeProvider classes are required to use
// String and DateTime as a key for HashMapT
class StringKeyHashCodeProvider: public Osp::Base::Collection::IHashCodeProviderT<
      Osp::Base::String>
{
public:
  virtual int GetHashCode(const Osp::Base::String &obj) const;
};

class DateTimeKeyHashCodeProvider: public Osp::Base::Collection::IHashCodeProviderT<
      Osp::Base::DateTime>
{
public:
  virtual int GetHashCode(const Osp::Base::DateTime &obj) const;
};


// The following *KeyComparer classes are required to use
// String and DateTime as a key for HashMapT
class StringKeyComparer: public Osp::Base::Collection::IComparerT<
      Osp::Base::String>
{
public:
  virtual result Compare(const Osp::Base::String &obj1,
          const Osp::Base::String &obj2, int &cmp) const;
};

class DateTimeKeyComparer: public Osp::Base::Collection::IComparerT<
      Osp::Base::DateTime>
{
public:
  virtual result Compare(const Osp::Base::DateTime &obj1,
          const Osp::Base::DateTime &obj2, int &cmp) const;
};

#endif /* HASHMAPTEXTENSIONS_H_ */
HashMapTExtensions.cpp
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
/*
 * HashMapTExtensions.cpp
 */

#include "HashMapTExtensions.h"

using namespace Osp::Base;

int StringKeyHashCodeProvider::GetHashCode(const String &obj) const
{
  return obj.GetHashCode();
}

int DateTimeKeyHashCodeProvider::GetHashCode(const DateTime &obj) const
{
  return obj.GetHashCode();
}


result StringKeyComparer::Compare(const String &obj1, const String &obj2,
      int &cmp) const
{
  cmp = obj1.CompareTo(obj2);
  return E_SUCCESS;
}

result DateTimeKeyComparer::Compare(const DateTime &obj1, const DateTime &obj2,
      int &cmp) const
{
  cmp = obj1.CompareTo(obj2);
  return E_SUCCESS;
}

Then you use the classes when Constructing a HashMapT object.

Comments