jak-project/common/util/Range.h

71 lines
2 KiB
C
Raw Normal View History

#pragma once
#include <iterator>
/*!
* A Range is similar to a Python range. It can represent an empty range or a range of consecutive
* values. It supports iterators so you can write a loop like:
* for (auto x : Range<int>(1, 4)) {
* }
* and it will work like you expect, without allocating memory for all the elements.
*
* A Range's iterator actually store values, not references, so this is designed to be used
* with small things, like integers.
*
* Note - for size and iterator distances, a Range will use the same type as its elements.
*/
template <typename T>
class Range {
public:
Range() = default;
Range(const T& start, const T& end) : m_start(start), m_end(end) {}
const T& first() const { return m_start; }
const T& last() const { return m_end; }
T& first() { return m_start; }
T& last() { return m_end; }
bool contains(T& val) const { return val >= m_start && val < m_end; }
bool empty() const { return m_end <= m_start; }
T size() const { return m_end - m_start; }
bool operator==(const Range<T>& other) const {
return m_start == other.m_start && m_end == other.m_end;
}
bool operator!=(const Range<T>& other) const { return !((*this) == other); }
struct Iterator {
using iterator_category = std::input_iterator_tag;
using difference_type = T;
using value_type = T;
using pointer = const T*;
using reference = const T&;
Iterator(const T& val) : m_val(val) {}
reference operator*() const { return m_val; }
pointer operator->() { return &m_val; }
Iterator& operator++() {
m_val++;
return *this;
}
const Iterator operator++(int) {
Iterator old = *this;
++(*this);
return old;
}
friend bool operator==(const Iterator& a, const Iterator& b) { return a.m_val == b.m_val; }
friend bool operator!=(const Iterator& a, const Iterator& b) { return a.m_val != b.m_val; }
private:
T m_val;
};
Iterator begin() { return Iterator(m_start); }
Iterator end() { return Iterator(m_end); }
private:
T m_start = {};
T m_end = {};
LSP: A bunch of new OpenGOAL language features (#3437) - Integrate the AST into the LSP, this makes parsing and tokenizing the files much easier - Consolidate most of the symbol info tracking in `goalc` to a single map. Fixed some issues where the old map would never evict symbols when re-compiling files. There is still some more to cleanup, but this now can be used as an incrementally updated source-of-truth for the LSP - re-compile files when they are saved. Ideally this would be done everytime they are changed but that: - may be too aggressive - goalc doesn't compile incrementally yet so it likely would be a worse UX Features added, see https://github.com/open-goal/opengoal-vscode/issues/256 - Hover ![image](https://github.com/open-goal/jak-project/assets/13153231/58dadb5d-582c-4c1f-9ffe-eaa4c85a0255) ![image](https://github.com/open-goal/jak-project/assets/13153231/b383adde-57fc-462c-a256-b2de5c30ca9a) - LSP Status fixed - Type Hierarchy ![image](https://github.com/open-goal/jak-project/assets/13153231/8e681377-1d4e-4336-ad70-1695a4607340) - Document Color ![image](https://github.com/open-goal/jak-project/assets/13153231/4e48ccd8-0ed1-4459-a133-5277561e4201) - Document Symbols ![Screenshot 2024-03-27 004105](https://github.com/open-goal/jak-project/assets/13153231/8e655034-43c4-4261-b6e0-85de00cbfc7f) - Completions ![Screenshot 2024-03-30 004504](https://github.com/open-goal/jak-project/assets/13153231/d123a187-af90-466b-9eb7-561b2ee97cd1) --------- Co-authored-by: Hat Kid <6624576+Hat-Kid@users.noreply.github.com>
2024-03-30 19:49:07 -04:00
};