18#include <initializer_list>
38 void QuickSort(
int start,
int end);
41 void Insertion(
int start,
int end);
44 void Exch(
int i,
int j) { Item a = v[i]; v[i] = v[j]; v[j] = a; }
47 void QuickSortIdx(
int start,
int end);
50 void ExchIdx(
int i,
int j) {
int a = (*idx)[i]; (*idx)[i] = (*idx)[j]; (*idx)[j] = a; }
53 static inline int Min(
int a,
int b)
noexcept {
return (a < b) ? a : b; }
56 static inline int Max(
int a,
int b)
noexcept {
return (a > b) ? a : b; }
99 TVector(std::initializer_list<Item> init) {
100 Count(
static_cast<int>(init.size()));
102 for (
const auto& val : init)
153 int oldCount =
Count();
154 Count(oldCount +
static_cast<int>(init.size()));
156 for (
const auto& val : init)
157 v[oldCount + i++] = val;
218 const Item*
Data()
const {
return v; }
230 bool Empty()
const {
return (count == 0); }
283 int Find(Item& i,
bool binary =
false,
int left = 0,
int right = -1);
316 Item*
begin() noexcept {
return v; }
317 Item*
end() noexcept {
return v + count; }
318 const Item*
begin() const noexcept {
return v; }
319 const Item*
end() const noexcept {
return v + count; }
371 Item* aux =
new Item[size];
373 int limite = (count < size ? count : size);
374 for (
int k = limite - 1; k >= 0; --k)
395 : idx(nullptr), v(nullptr), sz(0), count(0)
419 for (
int k = 0; k < size; ++k)
420 operator[](k) = init[k];
458 for (
int i = 0; i < o.count; ++i) {
472 : v(o.v), sz(o.sz), count(o.count)
510 int oldCount = count;
512 Count(oldCount + o.count);
514 for (
int k = 0; k < o.count; ++k) {
515 v[oldCount + k] = o.v[k];
548 if (i < 0 || i >= count)
570 if (right < 0 || right > count - 1) right = count - 1;
571 if (left < 0 || left > right) left = 0;
572 while (left <= right) {
573 int mid = (left + right) >> 1;
574 if (v[mid] == i)
return mid;
575 if (v[mid] < i) left = mid + 1;
576 else right = mid - 1;
580 for (
int k = 0; k < count; ++k)
599 if (idxvect ==
nullptr) {
600 QuickSort(0, count - 1);
601 Insertion(0, count - 1);
606 for (
int k = 0; k < count; ++k)
608 QuickSortIdx(0, count - 1);
622 if (end > count - 1 || end < 0) end = count - 1;
623 if (start < 0) start = 0;
625 QuickSort(start, end);
626 Insertion(start, end);
640 if (end - start > 32) {
642 Exch((start + end) >> 1, end - 1);
643 if (v[end - 1] < v[start]) Exch(end - 1, start);
644 if (v[end] < v[start]) Exch(end, start);
645 if (v[end] < v[end - 1]) Exch(end, end - 1);
650 int i = start - 1, j = end;
653 while (v[++i] < pivot);
654 while (pivot < v[--j] && j > start);
660 QuickSort(start - 1, i - 1);
661 QuickSort(i + 1, end + 1);
674 for (
int i = end; i > start; --i)
678 for (
int i = start + 2; i <= end; ++i) {
681 while (tmp < v[j - 1]) {
699 if (end - start < 3) {
701 if (end - start == 1) {
702 if (v[(*idx)[end]] < v[(*idx)[start]])
705 else if (end - start == 2) {
706 if (v[(*idx)[end - 1]] < v[(*idx)[start]])
707 ExchIdx(end - 1, start);
708 if (v[(*idx)[end]] < v[(*idx)[start]])
710 if (v[(*idx)[end]] < v[(*idx)[end - 1]])
711 ExchIdx(end, end - 1);
716 ExchIdx((start + end) >> 1, end - 1);
717 if (v[(*idx)[end - 1]] < v[(*idx)[start]])
718 ExchIdx(end - 1, start);
719 if (v[(*idx)[end]] < v[(*idx)[start]])
721 if (v[(*idx)[end]] < v[(*idx)[end - 1]])
722 ExchIdx(end, end - 1);
727 int i = start - 1, j = end;
728 Item pivot = v[(*idx)[end]];
730 while (v[(*idx)[++i]] < pivot);
731 while (pivot < v[(*idx)[--j]] && j > start);
737 if (i > start) QuickSortIdx(start - 1, i - 1);
738 if (i < end) QuickSortIdx(i + 1, end + 1);
754 for (
int k = count - 1; k > 0; --k)
768 for (
int w = 0; w < count; ++w) {
786 for (
int j = 0; j < count; ++j) {
802 for (
int k = 0; k < count; ++k) {
819 for (
int k = i; k < count - 1; ++k) {
861 int k = 0, w = 0, s = Count();
862 while (k < other.
Count()) {
863 while (w < s &&
operator[](w) < other[k]) {
866 if (w >= s ||
operator[](w) > other[k]) {
886 int k = 0, w = 0, z = 0;
887 while (k < other.
Count() && w < Count()) {
888 if (
operator[](w) < other[k]) {
891 else if (
operator[](w) == other[k]) {
912 int k = 0, w = 0, z = 0;
913 while (k < other.
Count() && w < Count()) {
914 if (v[w] < other[k]) {
917 else if (v[w] == other[k]) {
924 while (w < Count()) {
939 if (count != other.
Count())
return false;
940 for (
int k = 0; k < count; ++k) {
941 if (v[k] != other[k])
return false;
955 if (count > other.
Count())
return false;
957 for (
int k = 0; k < count; ++k) {
958 while (k2 < other.
Count() && v[k2] < v[k]) {
961 if (k2 >= other.
Count() || other[k2] != v[k]) {
978 if (index < 0) index = Count();
981 int oldCount = Count();
983 if (index > oldCount) index = oldCount;
984 for (
int k = oldCount - 1; k >= index; --k) {
987 for (
int k = 0; k < w; ++k) {
988 v[index + k] = src[k];
1001template <
class Item>
1004 if (index < 0) index = Count();
1005 int oldCount = Count();
1006 Count(oldCount + 1);
1007 if (index > oldCount) index = oldCount;
1008 for (
int k = oldCount - 1; k >= index; --k) {
1019template <
class Item>
1022 for (
int k = 0; k < count / 2; ++k) {
1023 Exch(k, count - 1 - k);
1039template <
class Item>
1043 int n1 = Count(), n2 = other.
Count();
1046 result = abs(n1 - n2);
1047 int m = Min(n1, n2);
1048 for (
int i = 0; i < m; ++i) {
1049 if (v[i] != other[i]) ++result;
1052 else if (type == 1) {
1053 int m = Min(n1, n2);
1054 for (
int i = 0; i < m; ++i) {
1055 result += abs(v[i] - other[i]);
1058 else if (type == 2) {
1059 result = Max(n1, n2);
1060 for (
int i = 0; i + 1 < n1; ++i) {
1061 int j = other.
Find(v[i]);
1062 if (j >= 0 && j + 1 < n2 && v[i + 1] == other[j + 1]) {
1067 else if (type == 3) {
1068 int rows = n1 + 1, cols = n2 + 1;
1071 for (
int i = 0; i < rows; ++i) mtx[i] = i;
1072 for (
int j = 1; j < cols; ++j) mtx[j * rows] = j;
1074 for (
int i = 1; i < rows; ++i) {
1075 for (
int j = 1; j < cols; ++j) {
1076 int cost = (v[i - 1] == other[j - 1] ? 0 : 1);
1077 int val = mtx[(i - 1) + rows * (j - 1)] + cost;
1078 val = Min(val, mtx[(i - 1) + rows * j] + 1);
1079 val = Min(val, mtx[i + rows * (j - 1)] + 1);
1080 mtx[i + rows * j] = val;
1083 result = mtx[n1 + rows * n2];
1091 char buf[256] =
"", * bufferGrande =
nullptr;
1095 if (!str || *str ==
'\0')
1098 if ((tamanho = (
int)strlen(str)) < 256)
1099 snprintf(buf,
sizeof(buf),
"%s", str);
1101 if ((bufferGrande =
new char[tamanho + 1]) ==
nullptr)
1103 snprintf(bufferGrande, tamanho + 1,
"%s", str);
1104 token = bufferGrande;
1108 if ((pt = strchr(token,
','))) {
1113 if ((pt = strchr(token,
',')))
1120 if ((pt = strchr(token,
':'))) {
1123 int A = atoi(token);
1125 if ((pt2 = strchr(pt + 1,
':'))) {
1127 if ((C = atoi(pt2 + 1)) <= 0)
1130 int B = atoi(pt + 1);
1136 for (
int i = A; i <= B; i += C)
1140 *
this += atoi(token);
1144 delete[] bufferGrande;
Interface para geração de números aleatórios independentes do sistema operativo.
TVector< int > _TV(const char *str)
friend bool operator!=(const TVector< Item > &a, const TVector< Item > &b)
TVector< Item > & Count(int value)
Ajusta o tamanho lógico do vetor para value.
TVector< Item > & BeASet()
Converte o vetor num conjunto: remove duplicados e ordena.
virtual ~TVector() noexcept
Destrutor.
static Item erro
Valor retornado em casos de acesso inválido.
bool Equal(const TVector< Item > &v) const
Verifica se dois vetores-conjunto são iguais.
friend TVector< Item > operator+(TVector< Item > a, const TVector< Item > &b)
TVector & operator=(TVector &&o) noexcept
Operador de atribuição por movimentação.
TVector< Item > & Replace(Item const &iold, Item const &inew)
Substitui todas as ocorrências de um valor antigo por um novo.
Item & operator[](int i)
Acesso por índice com auto-expansão.
TVector & operator=(const TVector &o)
Operador de atribuição por cópia.
TVector< Item > & operator+=(const char *str)
Acrescenta elementos a partir de uma string no formato de lista.
const Item * begin() const noexcept
TVector< Item > & Insert(TVector< Item > &v, int index=0)
Insere um vetor de itens na posição indicada.
TVector< Item > & RandomOrder()
Coloca os elementos em ordem aleatória (Fisher–Yates shuffle).
TVector< Item > & Difference(const TVector< Item > &v)
Diferença deste conjunto em relação a outro.
TVector< Item > & Sort(TVector< int > *idxvect=nullptr)
Ordena todo o vetor, opcionalmente devolvendo índices ordenados.
TVector< Item > & operator+=(const Item &x)
TVector & operator+=(const TVector &o)
Concatena outro vetor a este.
TVector< Item > & operator-=(const Item &x)
friend bool operator==(const TVector< Item > &a, const TVector< Item > &b)
TVector(const char *str)
Constrói um vetor de inteiros a partir de uma string no formato de lista.
TVector< Item > & Remove(Item const &i)
Remove todas as ocorrências de um dado elemento.
TVector & operator+=(std::initializer_list< Item > init)
Adiciona múltiplos elementos ao final do vetor.
int Distance(TVector< Item > &v, int type=0)
Calcula várias métricas de “distância” entre vetores.
TVector< Item > & Intersection(const TVector< Item > &v)
Interseção deste conjunto com outro.
const Item & operator[](int i) const
Acesso constante por índice sem modificação de tamanho.
const Item * Data() const
Acesso direto constante.
bool Contained(const TVector< Item > &v) const
Verifica se este conjunto está contido no outro.
TVector< Item > & Add(Item a)
TVector< Item > & Reset(Item const &i)
Preenche todo o vetor com um mesmo valor.
TVector< Item > & Invert()
Inverte a ordem dos elementos no vetor.
TVector< Item > & Insert(Item a, int index=0)
Insere um único elemento na posição indicada.
void Sort(int start, int end=-1)
Ordena um subintervalo [start,end] do vetor.
TVector< Item > & Delete(int i)
Remove o elemento na posição i deslocando os seguintes.
Item * Data()
Acesso direto.
int Find(Item &i, bool binary=false, int left=0, int right=-1)
Procura um elemento no vetor.
TVector(int size, Item const *init)
Constrói um vetor pré-carregado a partir de um array.
TVector< Item > & Union(const TVector< Item > &v)
Realiza a união deste conjunto com outro.
TVector(int size=0)
Construtor.
const Item * end() const noexcept
TVector< Item > & Push(Item a)
unsigned int rand(int seq)
Retorna o próximo valor pseudo-aleatório.