Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ui2oh.cpp « editor - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dfe4f93ef803db43d85f059fc22d58a2307062f8 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "editor/ui2oh.hpp"

#include "base/assert.hpp"

#include <algorithm>
#include <set>
#include <string>

#include "3party/opening_hours/opening_hours.hpp"

namespace
{
using osmoh::operator ""_h;

osmoh::Timespan const kTwentyFourHours = {0_h, 24_h};

editor::ui::OpeningDays MakeOpeningDays(osmoh::Weekdays const & wds)
{
  std::set<osmoh::Weekday> openingDays;
  for (auto const & wd : wds.GetWeekdayRanges())
  {
    if (wd.HasSunday())
      openingDays.insert(osmoh::Weekday::Sunday);
    if (wd.HasMonday())
      openingDays.insert(osmoh::Weekday::Monday);
    if (wd.HasTuesday())
      openingDays.insert(osmoh::Weekday::Tuesday);
    if (wd.HasWednesday())
      openingDays.insert(osmoh::Weekday::Wednesday);
    if (wd.HasThursday())
      openingDays.insert(osmoh::Weekday::Thursday);
    if (wd.HasFriday())
      openingDays.insert(osmoh::Weekday::Friday);
    if (wd.HasSaturday())
      openingDays.insert(osmoh::Weekday::Saturday);
  }
  return openingDays;
}

void SetUpWeekdays(osmoh::Weekdays const & wds, editor::ui::TimeTable & tt)
{
  tt.SetOpeningDays(MakeOpeningDays(wds));
}

void SetUpTimeTable(osmoh::TTimespans spans, editor::ui::TimeTable & tt)
{
  using namespace osmoh;

  // Expand plus: 13:15+ -> 13:15-24:00.
  for (auto & span : spans)
    span.ExpandPlus();

  std::sort(std::begin(spans), std::end(spans), [](Timespan const & a, Timespan const & b) {
    auto const start1 = a.GetStart().GetHourMinutes().GetDuration();
    auto const start2 = b.GetStart().GetHourMinutes().GetDuration();

    return start1 < start2;
  });

  // Take first start and last end as opening time span.
  tt.SetOpeningTime({spans.front().GetStart(), spans.back().GetEnd()});

  // Add an end of a span of index i and start of following span
  // as exclude time.
  for (size_t i = 0; i + 1 < spans.size(); ++i)
    tt.AddExcludeTime({spans[i].GetEnd(), spans[i + 1].GetStart()});
}

int32_t WeekdayNumber(osmoh::Weekday const wd) { return static_cast<int32_t>(wd); }

constexpr uint32_t kDaysInWeek = 7;

// Shifts values from 1 to 7 like this: 1 2 3 4 5 6 7 -> 2 3 4 5 6 7 1
int32_t NextWeekdayNumber(osmoh::Weekday const wd)
{
  auto dayNumber = WeekdayNumber(wd);
  // If first element of the gourp would be evaluated to 0
  // the resulting formula whould be (dayNumber + 1) % kDaysInWeek.
  // Since the first one evaluates to 1
  // the formula is:
  return dayNumber % kDaysInWeek + 1;
}

// Returns a vector of Weekdays with no gaps and with Sunday as the last day.
// Exampls:
// su, mo, we -> mo, we, su;
// su, mo, fr, sa -> fr, sa, su, mo.
std::vector<osmoh::Weekday> RemoveInversion(editor::ui::OpeningDays const & days)
{
  std::vector<osmoh::Weekday> result(begin(days), end(days));
  if ((NextWeekdayNumber(result.back()) != WeekdayNumber(result.front()) &&
       result.back() != osmoh::Weekday::Sunday) || result.size() < 2)
    return result;

  auto inversion = adjacent_find(begin(result), end(result),
                [](osmoh::Weekday const a, osmoh::Weekday const b)
                {
                  return NextWeekdayNumber(a) != WeekdayNumber(b);
                });

  if (inversion != end(result))
    rotate(begin(result), ++inversion, end(result));

  if (result.front() == osmoh::Weekday::Sunday)
    rotate(begin(result), begin(result) + 1, end(result));

  return result;
}

using Weekdays = std::vector<osmoh::Weekday>;

std::vector<Weekdays> SplitIntoIntervals(editor::ui::OpeningDays const & days)
{
  ASSERT_GREATER(days.size(), 0, ("At least one day must present."));
  std::vector<Weekdays> result;
  auto const & noInversionDays = RemoveInversion(days);
  ASSERT(!noInversionDays.empty(), ());

  auto previous = *begin(noInversionDays);
  result.push_back({previous});

  for (auto it = next(begin(noInversionDays)); it != end(noInversionDays); ++it)
  {
    if (NextWeekdayNumber(previous) != WeekdayNumber(*it))
      result.push_back({});
    result.back().push_back(*it);
    previous = *it;
  }

  return result;
}

osmoh::Weekdays MakeWeekdays(editor::ui::TimeTable const & tt)
{
  osmoh::Weekdays wds;

  for (auto const & daysInterval : SplitIntoIntervals(tt.GetOpeningDays()))
  {
    osmoh::WeekdayRange wdr;
    wdr.SetStart(*begin(daysInterval));
    if (daysInterval.size() > 1)
      wdr.SetEnd(*(prev(end(daysInterval))));

    wds.AddWeekdayRange(wdr);
  }

  return wds;
}

osmoh::TTimespans MakeTimespans(editor::ui::TimeTable const & tt)
{
  if (tt.IsTwentyFourHours())
    return {kTwentyFourHours};

  auto const & excludeTime = tt.GetExcludeTime();
  if (excludeTime.empty())
    return {tt.GetOpeningTime()};

  osmoh::TTimespans spans{{tt.GetOpeningTime().GetStart(), excludeTime[0].GetStart()}};

  for (size_t i = 0; i + 1 < excludeTime.size(); ++i)
    spans.emplace_back(excludeTime[i].GetEnd(), excludeTime[i + 1].GetStart());

  spans.emplace_back(excludeTime.back().GetEnd(), tt.GetOpeningTime().GetEnd());

  return spans;
}

editor::ui::OpeningDays const kWholeWeek = {
  osmoh::Weekday::Monday,
  osmoh::Weekday::Tuesday,
  osmoh::Weekday::Wednesday,
  osmoh::Weekday::Thursday,
  osmoh::Weekday::Friday,
  osmoh::Weekday::Saturday,
  osmoh::Weekday::Sunday
};

editor::ui::OpeningDays GetCommonDays(editor::ui::OpeningDays const & a,
                                      editor::ui::OpeningDays const & b)
{
  editor::ui::OpeningDays result;
  std::set_intersection(begin(a), end(a), begin(b), end(b), inserter(result, begin(result)));
  return result;
}

osmoh::HourMinutes::TMinutes::rep GetDuration(osmoh::Time const & time)
{
  return time.GetHourMinutes().GetDurationCount();
}

bool Includes(osmoh::Timespan const & a, osmoh::Timespan const & b)
{
  return GetDuration(a.GetStart()) <= GetDuration(b.GetStart()) &&
         GetDuration(b.GetEnd()) <= GetDuration(a.GetEnd());
}

bool ExcludeRulePart(osmoh::RuleSequence const & rulePart, editor::ui::TimeTableSet & tts)
{
  auto const ttsInitialSize = tts.Size();
  for (size_t i = 0; i < ttsInitialSize; ++i)
  {
    auto tt = tts.Get(i);
    auto const ttOpeningDays = tt.GetOpeningDays();
    auto const commonDays = GetCommonDays(ttOpeningDays,
                                          MakeOpeningDays(rulePart.GetWeekdays()));

    auto const removeCommonDays = [&commonDays](editor::ui::TimeTableSet::Proxy & tt)
    {
      for (auto const day : commonDays)
        VERIFY(tt.RemoveWorkingDay(day), ("Can't remove working day"));
      VERIFY(tt.Commit(), ("Can't commit changes"));
    };

    auto const twentyFourHoursGuard = [](editor::ui::TimeTable & tt)
    {
      if (tt.IsTwentyFourHours())
      {
        tt.SetTwentyFourHours(false);
        // TODO(mgsergio): Consider TimeTable refactoring:
        // get rid of separation of TwentyFourHours and OpeningTime.
        tt.SetOpeningTime(kTwentyFourHours);
      }
    };

    auto const & excludeTime = rulePart.GetTimes();
    // The whole rule matches to the tt.
    if (commonDays.size() == ttOpeningDays.size())
    {
      // rulePart applies to commonDays in a whole.
      if (excludeTime.empty())
        return tts.Remove(i);

      twentyFourHoursGuard(tt);

      for (auto const & time : excludeTime)
      {
        // Whatever it is, it's already closed at a time out of opening time.
        if (!Includes(tt.GetOpeningTime(), time))
          continue;

        // The whole opening time interval should be switched off
        if (!tt.AddExcludeTime(time))
          return tts.Remove(i);
      }
      VERIFY(tt.Commit(), ("Can't update time table"));
      return true;
    }
    // A rule is applied to a subset of a time table. We should
    // subtract common parts from tt and add a new time table if needed.
    if (commonDays.size() != 0)
    {
      // rulePart applies to commonDays in a whole.
      if (excludeTime.empty())
      {
        removeCommonDays(tt);
        continue;
      }

      twentyFourHoursGuard(tt);

      editor::ui::TimeTable copy = tt;
      VERIFY(copy.SetOpeningDays(commonDays), ("Can't set opening days"));

      auto doAppendRest = true;
      for (auto const & time : excludeTime)
      {
        // Whatever it is, it's already closed at a time out of opening time.
        if (!Includes(copy.GetOpeningTime(), time))
          continue;

        // The whole opening time interval should be switched off
        if (!copy.AddExcludeTime(time))
        {
          doAppendRest = false;
          break;
        }
      }

      removeCommonDays(tt);

      if (doAppendRest)
        VERIFY(tts.Append(copy), ("Can't add new time table"));
    }
  }
  return true;
}
}  // namespace

namespace editor
{
osmoh::OpeningHours MakeOpeningHours(ui::TimeTableSet const & tts)
{
  ASSERT_GREATER(tts.Size(), 0, ("At least one time table must present."));

  if (tts.IsTwentyFourPerSeven())
  {
    osmoh::RuleSequence rulePart;
    rulePart.SetTwentyFourHours(true);
    return osmoh::OpeningHours({rulePart});
  }

  osmoh::TRuleSequences rule;
  for (auto const & tt : tts)
  {
    osmoh::RuleSequence rulePart;
    rulePart.SetWeekdays(MakeWeekdays(tt));
    rulePart.SetTimes(MakeTimespans(tt));
    rule.push_back(rulePart);
  }

  return rule;
}

bool MakeTimeTableSet(osmoh::OpeningHours const & oh, ui::TimeTableSet & tts)
{
  if (!oh.IsValid())
    return false;

  if (oh.HasYearSelector() || oh.HasWeekSelector() || oh.HasMonthSelector())
    return false;

  tts = ui::TimeTableSet();
  if (oh.IsTwentyFourHours())
    return true;

  bool first = true;
  for (auto const & rulePart : oh.GetRule())
  {
    ui::TimeTable tt = ui::TimeTable::GetUninitializedTimeTable();
    tt.SetOpeningTime(tt.GetPredefinedOpeningTime());

    // Comments and unknown rules belong to advanced mode.
    if (rulePart.GetModifier() == osmoh::RuleSequence::Modifier::Unknown ||
        rulePart.GetModifier() == osmoh::RuleSequence::Modifier::Comment)
      return false;

    if (rulePart.GetModifier() == osmoh::RuleSequence::Modifier::Closed)
    {
      // off modifier in the first part in oh is useless.
      if (first == true)
        return false;

      if (!ExcludeRulePart(rulePart, tts))
        return false;
      continue;
    }

    if (rulePart.HasWeekdays())
      SetUpWeekdays(rulePart.GetWeekdays(), tt);
    else
      tt.SetOpeningDays(kWholeWeek);

    auto const & times = rulePart.GetTimes();

    bool isTwentyFourHours =
        times.empty() || (times.size() == 1 && times.front() == kTwentyFourHours);

    if (isTwentyFourHours)
    {
      tt.SetTwentyFourHours(true);
    }
    else
    {
      tt.SetTwentyFourHours(false);
      SetUpTimeTable(rulePart.GetTimes(), tt);
    }

    // Check size as well since ExcludeRulePart can add new time tables.
    bool const appended = first && tts.Size() == 1 ? tts.Replace(tt, 0) : tts.Append(tt);
    first = false;
    if (!appended)
      return false;
  }

  return true;
}
} // namespace editor