35 void QuickSort(
int start,
int end);
38 void Insertion(
int start,
int end);
41 void Exch(
int i,
int j) { Item a = v[i]; v[i] = v[j]; v[j] = a; }
44 void QuickSortIdx(
int start,
int end);
47 void ExchIdx(
int i,
int j) {
int a = (*idx)[i]; (*idx)[i] = (*idx)[j]; (*idx)[j] = a; }
50 static inline int Min(
int a,
int b)
noexcept {
return (a < b) ? a : b; }
53 static inline int Max(
int a,
int b)
noexcept {
return (a > b) ? a : b; }
213 int Find(Item& i,
bool binary =
false,
int left = 0,
int right = -1);
246 Item*
begin() noexcept {
return v; }
247 Item*
end() noexcept {
return v + count; }
248 const Item*
begin() const noexcept {
return v; }
249 const Item*
end() const noexcept {
return v + count; }
310 Item* aux =
new Item[size];
312 for (
int k = 0; k < count; ++k)
333 : idx(nullptr), v(nullptr), sz(0), count(0)
357 for (
int k = 0; k < size; ++k)
358 operator[](k) = init[k];
396 for (
int i = 0; i < o.count; ++i) {
410 : v(o.v), sz(o.sz), count(o.count)
448 int oldCount = count;
450 Count(oldCount + o.count);
452 for (
int k = 0; k < o.count; ++k) {
453 v[oldCount + k] = o.v[k];
486 if (i < 0 || i >= count)
508 if (right < 0 || right > count - 1) right = count - 1;
509 if (left < 0 || left > right) left = 0;
510 while (left <= right) {
511 int mid = (left + right) >> 1;
512 if (v[mid] == i)
return mid;
513 if (v[mid] < i) left = mid + 1;
514 else right = mid - 1;
518 for (
int k = 0; k < count; ++k)
541 if (idxvect ==
nullptr) {
542 QuickSort(0, count - 1);
543 Insertion(0, count - 1);
548 for (
int k = 0; k < count; ++k)
550 QuickSortIdx(0, count - 1);
564 if (end > count - 1 || end < 0) end = count - 1;
565 if (start < 0) start = 0;
567 QuickSort(start, end);
568 Insertion(start, end);
582 if (end - start > 32) {
584 Exch((start + end) >> 1, end - 1);
585 if (v[end - 1] < v[start]) Exch(end - 1, start);
586 if (v[end] < v[start]) Exch(end, start);
587 if (v[end] < v[end - 1]) Exch(end, end - 1);
592 int i = start - 1, j = end;
595 while (v[++i] < pivot);
596 while (pivot < v[--j] && j > start);
602 QuickSort(start - 1, i - 1);
603 QuickSort(i + 1, end + 1);
616 for (
int i = end; i > start; --i)
620 for (
int i = start + 2; i <= end; ++i) {
623 while (tmp < v[j - 1]) {
641 if (end - start < 3) {
643 if (end - start == 1) {
644 if (v[(*idx)[end]] < v[(*idx)[start]])
647 else if (end - start == 2) {
648 if (v[(*idx)[end - 1]] < v[(*idx)[start]])
649 ExchIdx(end - 1, start);
650 if (v[(*idx)[end]] < v[(*idx)[start]])
652 if (v[(*idx)[end]] < v[(*idx)[end - 1]])
653 ExchIdx(end, end - 1);
658 ExchIdx((start + end) >> 1, end - 1);
659 if (v[(*idx)[end - 1]] < v[(*idx)[start]])
660 ExchIdx(end - 1, start);
661 if (v[(*idx)[end]] < v[(*idx)[start]])
663 if (v[(*idx)[end]] < v[(*idx)[end - 1]])
664 ExchIdx(end, end - 1);
669 int i = start - 1, j = end;
670 Item pivot = v[(*idx)[end]];
672 while (v[(*idx)[++i]] < pivot);
673 while (pivot < v[(*idx)[--j]] && j > start);
679 if (i > start) QuickSortIdx(start - 1, i - 1);
680 if (i < end) QuickSortIdx(i + 1, end + 1);
696 for (
int k = count - 1; k > 0; --k)
710 for (
int w = 0; w < count; ++w) {
728 for (
int j = 0; j < count; ++j) {
744 for (
int k = 0; k < count; ++k) {
761 for (
int k = i; k < count - 1; ++k) {
803 int k = 0, w = 0, s = Count();
804 while (k < other.
Count()) {
805 while (w < s &&
operator[](w) < other[k]) {
808 if (w >= s ||
operator[](w) > other[k]) {
828 int k = 0, w = 0, z = 0;
829 while (k < other.
Count() && w < Count()) {
830 if (
operator[](w) < other[k]) {
833 else if (
operator[](w) == other[k]) {
854 int k = 0, w = 0, z = 0;
855 while (k < other.
Count() && w < Count()) {
856 if (v[w] < other[k]) {
859 else if (v[w] == other[k]) {
866 while (w < Count()) {
881 if (count != other.
Count())
return false;
882 for (
int k = 0; k < count; ++k) {
883 if (v[k] != other[k])
return false;
897 if (count > other.
Count())
return false;
899 for (
int k = 0; k < count; ++k) {
900 while (k2 < other.
Count() && v[k2] < v[k]) {
903 if (k2 >= other.
Count() || other[k2] != v[k]) {
920 if (index < 0) index = Count();
923 int oldCount = Count();
925 if (index > oldCount) index = oldCount;
926 for (
int k = oldCount - 1; k >= index; --k) {
929 for (
int k = 0; k < w; ++k) {
930 v[index + k] = src[k];
946 if (index < 0) index = Count();
947 int oldCount = Count();
949 if (index > oldCount) index = oldCount;
950 for (
int k = oldCount - 1; k >= index; --k) {
964 for (
int k = 0; k < count / 2; ++k) {
965 Exch(k, count - 1 - k);
985 int n1 = Count(), n2 = other.
Count();
988 result = abs(n1 - n2);
990 for (
int i = 0; i < m; ++i) {
991 if (v[i] != other[i]) ++result;
994 else if (type == 1) {
996 for (
int i = 0; i < m; ++i) {
997 result += abs(v[i] - other[i]);
1000 else if (type == 2) {
1001 result = Max(n1, n2);
1002 for (
int i = 0; i + 1 < n1; ++i) {
1003 int j = other.
Find(v[i]);
1004 if (j >= 0 && j + 1 < n2 && v[i + 1] == other[j + 1]) {
1009 else if (type == 3) {
1010 int rows = n1 + 1, cols = n2 + 1;
1013 for (
int i = 0; i < rows; ++i) mtx[i] = i;
1014 for (
int j = 1; j < cols; ++j) mtx[j * rows] = j;
1016 for (
int i = 1; i < rows; ++i) {
1017 for (
int j = 1; j < cols; ++j) {
1018 int cost = (v[i - 1] == other[j - 1] ? 0 : 1);
1019 int val = mtx[(i - 1) + rows * (j - 1)] + cost;
1020 val = Min(val, mtx[(i - 1) + rows * j] + 1);
1021 val = Min(val, mtx[i + rows * (j - 1)] + 1);
1022 mtx[i + rows * j] = val;
1025 result = mtx[n1 + rows * n2];
Interface para geração de números aleatórios independentes do sistema operativo.
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< 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.
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< Item > & Remove(Item const &i)
Remove todas as ocorrências de um dado elemento.
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.
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.
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.