Qual é a Melhor Forma de Embaralhar um NSMutableArray?
-
09-06-2019 - |
Pergunta
Se você tem um NSMutableArray
, como você misturar os elementos aleatoriamente?
(Eu tenho minha resposta para este, que está publicada abaixo, mas eu sou novo para o Cacau e estou interessado em saber se há uma maneira melhor.)
Atualização:Como observado por @Mukesh, como do iOS 10+ e macOS 10.12+, há um -[NSMutableArray shuffledArray]
método que pode ser usado para misturar.Ver https://developer.apple.com/documentation/foundation/nsarray/1640855-shuffledarray?language=objc para obter detalhes.(Mas repare que isto cria uma nova matriz, em vez de baralhar os elementos no lugar.)
Solução
Você não precisa do swapObjectAtIndex método. exchangeObjectAtIndex:withObjectAtIndex: já existe.
Outras dicas
Eu resolvi isso adicionando uma categoria para NSMutableArray.
Editar: Removido do método desnecessárias obrigado a responder por Ladd.
Editar: Alterado (arc4random() % nElements)
para arc4random_uniform(nElements)
graças a responder pela Gregory Goltsov e comentários por miho e blahdiblah
Editar: Ciclo de melhoria, graças comentário por Ron
Editar: Adicionado verificar que a matriz a é não vazio, graças a comentário por Mahesh Agrawal
// NSMutableArray_Shuffling.h
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#include <Cocoa/Cocoa.h>
#endif
// This category enhances NSMutableArray by providing
// methods to randomly shuffle the elements.
@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end
// NSMutableArray_Shuffling.m
#import "NSMutableArray_Shuffling.h"
@implementation NSMutableArray (Shuffling)
- (void)shuffle
{
NSUInteger count = [self count];
if (count <= 1) return;
for (NSUInteger i = 0; i < count - 1; ++i) {
NSInteger remainingCount = count - i;
NSInteger exchangeIndex = i + arc4random_uniform((u_int32_t )remainingCount);
[self exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
}
@end
Desde que eu ainda não pode comentar, eu pensei que eu iria contribuir com uma resposta completa.Eu modificada Kristopher Johnson implementação para o meu projeto em um número de maneiras (realmente tentando torná-lo tão concisa quanto possível), sendo um deles arc4random_uniform()
porque evita módulo de viés.
// NSMutableArray+Shuffling.h
#import <Foundation/Foundation.h>
/** This category enhances NSMutableArray by providing methods to randomly
* shuffle the elements using the Fisher-Yates algorithm.
*/
@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end
// NSMutableArray+Shuffling.m
#import "NSMutableArray+Shuffling.h"
@implementation NSMutableArray (Shuffling)
- (void)shuffle
{
NSUInteger count = [self count];
for (uint i = 0; i < count - 1; ++i)
{
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = arc4random_uniform(nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
@end
A partir do iOS 10 você pode usar o novo shuffled
API:
https://developer.apple.com/reference/foundation/nsarray/1640855-shuffled
let shuffledArray = array.shuffled()
Um pouco melhorado e concisa solução (em comparação com as respostas principais).
O algoritmo é o mesmo e é descrito na literatura como "Fisher-Yates shuffle".
Em Objective-C:
@implementation NSMutableArray (Shuffle)
// Fisher-Yates shuffle
- (void)shuffle
{
for (NSUInteger i = self.count; i > 1; i--)
[self exchangeObjectAtIndex:i - 1 withObjectAtIndex:arc4random_uniform((u_int32_t)i)];
}
@end
Em Rápida 3.2 e 4.x:
extension Array {
/// Fisher-Yates shuffle
mutating func shuffle() {
for i in stride(from: count - 1, to: 0, by: -1) {
swapAt(i, Int(arc4random_uniform(UInt32(i + 1))))
}
}
}
Em Swift 3.0 e 3.1:
extension Array {
/// Fisher-Yates shuffle
mutating func shuffle() {
for i in stride(from: count - 1, to: 0, by: -1) {
let j = Int(arc4random_uniform(UInt32(i + 1)))
(self[i], self[j]) = (self[j], self[i])
}
}
}
Nota: Uma solução mais concisa em Swift é possível a partir de iOS10 usando GameplayKit
.
Esta é a forma mais simples e rápida para misturar NSArrays ou NSMutableArrays (objeto de quebra-cabeças é um NSMutableArray, ele contém objetos de quebra-cabeça.Eu adicionei ao quebra-cabeça de variável de objeto, o índice que indica a posição inicial no array)
int randomSort(id obj1, id obj2, void *context ) {
// returns random number -1 0 1
return (random()%3 - 1);
}
- (void)shuffle {
// call custom sort function
[puzzles sortUsingFunction:randomSort context:nil];
// show in log how is our array sorted
int i = 0;
for (Puzzle * puzzle in puzzles) {
NSLog(@" #%d has index %d", i, puzzle.index);
i++;
}
}
registo de saída:
#0 has index #6
#1 has index #3
#2 has index #9
#3 has index #15
#4 has index #8
#5 has index #0
#6 has index #1
#7 has index #4
#8 has index #7
#9 has index #12
#10 has index #14
#11 has index #16
#12 has index #17
#13 has index #10
#14 has index #11
#15 has index #13
#16 has index #5
#17 has index #2
assim como você pode comparar obj1 com obj2 e decidir o que você quer voltar os valores possíveis são:
- NSOrderedAscending = -1
- NSOrderedSame = 0
- NSOrderedDescending = 1
Há uma boa biblioteca popular, que tem este método como é parte, chamado de SSToolKit no GitHub.Arquivo NSMutableArray+SSToolkitAdditions.h contém shuffle método.Você pode usá-lo também.Entre esta, parece que há toneladas de coisas úteis.
A página principal da biblioteca é aqui.
Se você usar isso, seu código vai ser assim:
#import <SSCategories.h>
NSMutableArray *tableData = [NSMutableArray arrayWithArray:[temp shuffledArray]];
Esta biblioteca também tem um Pod (ver CocoaPods)
A partir do iOS 10, você pode usar NSArray shuffled()
a partir de GameplayKit.Aqui é um auxiliar para a Matriz em Swift 3:
import GameplayKit
extension Array {
@available(iOS 10.0, macOS 10.12, tvOS 10.0, *)
func shuffled() -> [Element] {
return (self as NSArray).shuffled() as! [Element]
}
@available(iOS 10.0, macOS 10.12, tvOS 10.0, *)
mutating func shuffle() {
replaceSubrange(0..<count, with: shuffled())
}
}
Se os elementos se repete.
exemplo:matriz:A A A B B B B A A A
única solução é:A B A B A
sequenceSelected
é um NSMutableArray que armazena elementos da classe obj, que são ponteiros para alguns seqüência.
- (void)shuffleSequenceSelected {
[sequenceSelected shuffle];
[self shuffleSequenceSelectedLoop];
}
- (void)shuffleSequenceSelectedLoop {
NSUInteger count = sequenceSelected.count;
for (NSUInteger i = 1; i < count-1; i++) {
// Select a random element between i and end of array to swap with.
NSInteger nElements = count - i;
NSInteger n;
if (i < count-2) { // i is between second and second last element
obj *A = [sequenceSelected objectAtIndex:i-1];
obj *B = [sequenceSelected objectAtIndex:i];
if (A == B) { // shuffle if current & previous same
do {
n = arc4random_uniform(nElements) + i;
B = [sequenceSelected objectAtIndex:n];
} while (A == B);
[sequenceSelected exchangeObjectAtIndex:i withObjectAtIndex:n];
}
} else if (i == count-2) { // second last value to be shuffled with last value
obj *A = [sequenceSelected objectAtIndex:i-1];// previous value
obj *B = [sequenceSelected objectAtIndex:i]; // second last value
obj *C = [sequenceSelected lastObject]; // last value
if (A == B && B == C) {
//reshufle
sequenceSelected = [[[sequenceSelected reverseObjectEnumerator] allObjects] mutableCopy];
[self shuffleSequenceSelectedLoop];
return;
}
if (A == B) {
if (B != C) {
[sequenceSelected exchangeObjectAtIndex:i withObjectAtIndex:count-1];
} else {
// reshuffle
sequenceSelected = [[[sequenceSelected reverseObjectEnumerator] allObjects] mutableCopy];
[self shuffleSequenceSelectedLoop];
return;
}
}
}
}
}
NSUInteger randomIndex = arc4random() % [theArray count];
Kristopher Johnson resposta é muito bom, mas não é totalmente aleatória.
Dado um array de 2 elementos, esta função devolve sempre a inversa da matriz, porque você está gerando o alcance de sua aleatórios sobre o resto dos índices.Um mais preciso shuffle()
função seria como
- (void)shuffle
{
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
NSInteger exchangeIndex = arc4random_uniform(count);
if (i != exchangeIndex) {
[self exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
}
}
Editar: Isso não é correto. Para fins de referência, eu não delete este post.Ver comentários sobre o motivo por que esta abordagem não é correto.
Simples código aqui:
- (NSArray *)shuffledArray:(NSArray *)array
{
return [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if (arc4random() % 2) {
return NSOrderedAscending;
} else {
return NSOrderedDescending;
}
}];
}