File indexing completed on 2024-04-28 04:05:22

0001 /*
0002     SPDX-FileCopyrightText: 2015 Jakob Gruber <jakob.gruber@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef STREAKS_H
0008 #define STREAKS_H
0009 
0010 #include <QList>
0011 #include <QSharedPointer>
0012 
0013 #include "boardmap.h"
0014 #include "boardstate.h"
0015 
0016 class Streaks
0017 {
0018 public:
0019     struct Streak {
0020         Streak() : solved(false) { }
0021         int value;
0022         bool solved;
0023     };
0024 
0025     Streaks(QSharedPointer<BoardMap> map, QSharedPointer<BoardState> state);
0026 
0027     /* Updates streaks affected by changes to (x,y). */
0028     void update(int x, int y);
0029 
0030     /* Updates streaks, taking the entire board into account. */
0031     void update();
0032 
0033     /* Returns the request row/col streak. These contain the least information required by
0034        the frontend, which is (for each position within a streak): "which number is this",
0035        and "is this position solved" */
0036     QList<Streaks::Streak> getRowStreak(int y) const;
0037     QList<Streaks::Streak> getColStreak(int x) const;
0038 
0039 private: /* Types. */
0040     struct StreakPrivate : public Streak {
0041         int begin;  /**< The first index of this streak. */
0042         int end;    /**< The first index after this streak. */
0043     };
0044 
0045 private: /* Functions. */
0046     /* Takes a sequence of states and returns streaks. */
0047     static QList<StreakPrivate> lineToStreaks(const QList<Board::State> &line,
0048                                                 Board::State filler);
0049 
0050     /** Given a map streak sequence as well as the current state of the associated line,
0051      *  processStreak() returns the state streaks. */
0052     static QList<Streak> processStreak(const QList<StreakPrivate> &map,
0053                                          const QList<Board::State> &l);
0054 
0055 private: /* Variables. */
0056     QSharedPointer<BoardMap> m_map;
0057     QSharedPointer<BoardState> m_state;
0058 
0059     /** Map streaks are calculated once upon construction and then stay immutable
0060      *  for the remaining lifetime of a Streaks object. They store information about
0061      *  the actual map streaks, including the streak beginning index, end index, and
0062      *  length. */
0063     QList<QList<StreakPrivate> > m_map_row_streaks;
0064     QList<QList<StreakPrivate> > m_map_col_streaks;
0065 
0066     /** State streaks are recomputed whenever the state of an associated cell changes.
0067      *  Unlike the map streaks, row streaks only store the publicly available information:
0068      *  the length of a streak, and whether it's solved or not. */
0069     QList<QList<Streak> > m_state_row_streaks;
0070     QList<QList<Streak> > m_state_col_streaks;
0071 };
0072 
0073 #endif // STREAKS_H