File indexing completed on 2025-03-09 05:11:44

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QFile>
0010 #include <QtGlobal>
0011 
0012 namespace Diff
0013 {
0014 
0015 template<typename T>
0016 class Array2
0017 {
0018     T *_data;
0019     int c1, c2;
0020 
0021 public:
0022     Array2(int c1, int c2);
0023     ~Array2();
0024 
0025     Q_ALWAYS_INLINE T &operator()(int i1, int i2);
0026 
0027     void exportToCsv(const QString &file);
0028 };
0029 
0030 template<typename T>
0031 Q_OUTOFLINE_TEMPLATE Array2<T>::Array2(int c1, int c2)
0032     : c1(c1)
0033     , c2(c2)
0034 {
0035     _data = new T[c1 * c2];
0036 }
0037 
0038 template<typename T>
0039 Q_OUTOFLINE_TEMPLATE Array2<T>::~Array2()
0040 {
0041     delete[] _data;
0042 }
0043 
0044 template<typename T>
0045 Q_OUTOFLINE_TEMPLATE T &Array2<T>::operator()(int i1, int i2)
0046 {
0047     return _data[c1 * i2 + i1];
0048 }
0049 
0050 template<typename T>
0051 Q_OUTOFLINE_TEMPLATE void Array2<T>::exportToCsv(const QString &file)
0052 {
0053     QFile f(file);
0054     f.open(QIODevice::WriteOnly | QIODevice::Text);
0055     for (int i = 0; i <= c1; i++) {
0056         for (int j = 0; j <= c2; j++)
0057             f.write(QByteArray::number(operator()(i, j)) + ",");
0058         f.write("\n");
0059     }
0060     f.close();
0061 }
0062 template<typename T>
0063 class Array3
0064 {
0065     T *_data;
0066     int c1, c2, c3;
0067 
0068 public:
0069     Array3(int c1, int c2, int c3);
0070     ~Array3();
0071 
0072     Q_ALWAYS_INLINE T &operator()(int i1, int i2, int i3);
0073 };
0074 
0075 template<typename T>
0076 Q_OUTOFLINE_TEMPLATE Array3<T>::Array3(int c1, int c2, int c3)
0077     : c1(c1)
0078     , c2(c2)
0079     , c3(c3)
0080 {
0081     _data = new T[c1 * c2 * c3];
0082 }
0083 
0084 template<typename T>
0085 Q_OUTOFLINE_TEMPLATE Array3<T>::~Array3()
0086 {
0087     delete[] _data;
0088 }
0089 
0090 template<typename T>
0091 Q_OUTOFLINE_TEMPLATE T &Array3<T>::operator()(int i1, int i2, int i3)
0092 {
0093     return _data[i3 + (c2 * c3 * i1) + (c3 * i2)];
0094 }
0095 
0096 }