AceTime 2.4.0 Date and time classes for Arduino that support timezones from the TZ Database.

ZoneSorterByName.h
1 /*
2 * MIT License
3 * Copyright (c) 2021 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_ZONE_SORTER_BY_NAME_H
7 #define ACE_TIME_ZONE_SORTER_BY_NAME_H
8
9 #include <AceSorting.h>
10 #include "ZoneManager.h"
11
12 namespace ace_time {
13
20 template <typename ZM>
21 class ZoneSorterByName {
22 public:
27 ZoneSorterByName(const ZM& zoneManager) :
28 mZoneManager(zoneManager)
29 {}
30
35 void fillIndexes(uint16_t indexes[], uint16_t size) const {
36 for (uint16_t i = 0; i < size; i++) {
37 indexes[i] = i;
38 }
39 }
40
42 void sortIndexes(uint16_t indexes[], uint16_t size) const {
43 ace_sorting::shellSortKnuth(indexes, size,
44 [this](uint16_t indexA, uint16_t indexB) -> bool {
45 auto za = this->mZoneManager.getZoneForIndex(indexA);
46 auto zb = this->mZoneManager.getZoneForIndex(indexB);
47 return za.kname().compareTo(zb.kname()) < 0;
48 }
49 );
50 }
51
53 void sortIds(uint32_t ids[], uint16_t size) const {
54 ace_sorting::shellSortKnuth(ids, size,
55 [this](uint32_t a, uint32_t b) -> bool {
56 uint16_t indexA = this->mZoneManager.indexForZoneId(a);
57 uint16_t indexB = this->mZoneManager.indexForZoneId(b);
58 auto za = this->mZoneManager.getZoneForIndex(indexA);
59 auto zb = this->mZoneManager.getZoneForIndex(indexB);
60 return za.kname().compareTo(zb.kname()) < 0;
61 }
62 );
63 }
64
66 void sortNames(const char* names[], uint16_t size) const {
67 ace_sorting::shellSortKnuth(names, size,
68 [this](const char* a, const char* b) -> bool {
69 uint16_t indexA = this->mZoneManager.indexForZoneName(a);
70 uint16_t indexB = this->mZoneManager.indexForZoneName(b);
71 auto za = this->mZoneManager.getZoneForIndex(indexA);
72 auto zb = this->mZoneManager.getZoneForIndex(indexB);
73 return za.kname().compareTo(zb.kname()) < 0;
74 }
75 );
76 }
77
78 private:
79 // disable copy constructor and assignment operator
80 ZoneSorterByName(const ZoneSorterByName&) = delete;
81 ZoneSorterByName& operator=(const ZoneSorterByName&) = delete;
82
83 private:
84 const ZM& mZoneManager;
85 };
86
87 }
88
89 #endif
ace_time::ZoneSorterByName
ZoneSorterByName, templatized on BasicZoneManager or ExtendedZoneManager.
Definition: ZoneSorterByName.h:21
ace_time::ZoneSorterByName::ZoneSorterByName
ZoneSorterByName(const ZM &zoneManager)
Constructor.
Definition: ZoneSorterByName.h:27
ace_time::ZoneSorterByName::sortNames
void sortNames(const char *names[], uint16_t size) const
Sort the given array of zone names by UTC offset, then by name.
Definition: ZoneSorterByName.h:66
ace_time::ZoneSorterByName::sortIndexes
void sortIndexes(uint16_t indexes[], uint16_t size) const
Sort the given array of indexes by UTC offset, then by name.
Definition: ZoneSorterByName.h:42
ace_time::ZoneSorterByName::sortIds
void sortIds(uint32_t ids[], uint16_t size) const
Sort the given array of zone ids by UTC offset, then by name.
Definition: ZoneSorterByName.h:53
ace_time::ZoneSorterByName::fillIndexes
void fillIndexes(uint16_t indexes[], uint16_t size) const
Fill the given array of indexes with index from [0, size).
Definition: ZoneSorterByName.h:35