Created
September 9, 2015 15:16
-
-
Save mzsima/598984dea93caa55af6d to your computer and use it in GitHub Desktop.
wave mountain sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.mm | |
// MountainSorting | |
// | |
// Created by MizushimaYusuke on 9/9/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
#import "ViewController.h" | |
#include <vector> | |
#include <memory> | |
using namespace std; | |
class MountainSorter { | |
public: | |
void sort(vector<int> bit) { | |
int a[bit.size()]; | |
for (int i=0; i<bit.size(); i++) { | |
for (int j=0; j<bit.size(); j++) { | |
if (bit[i] & 1 << j) a[i] = j; | |
} | |
} | |
for (int i=0; i<bit.size(); i++) { | |
int p = 0; | |
for (int j=i; j<bit.size(); j++) { | |
if (a[j] <= i) { | |
p = j; | |
break; | |
} | |
} | |
for (int j=p; j > i; j--) { | |
swap(a[j], a[j-1]); | |
notify(j, j-1); | |
} | |
} | |
} | |
private: | |
void notify(int a, int b) { | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"my message" object:@[@(a), @(b)]]; | |
} | |
}; | |
@interface ViewController () | |
@property (nonatomic, strong) NSMutableArray *spreadSheet; | |
@end | |
@implementation ViewController { | |
vector<int> bit; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self createSpreadSheet]; | |
[self createBit]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(swapRow:) name:@"my message" object:nil]; | |
} | |
- (void)createSpreadSheet { | |
self.spreadSheet = [NSMutableArray array]; | |
self.view.backgroundColor = [UIColor blackColor]; | |
float s = 50; | |
for (int i=0; i<36; i++) { | |
float x = (i % 6) * s + 40; | |
float y = (i / 6) * s + 100; | |
UILabel *cell = [[UILabel alloc] initWithFrame:CGRectMake(x, y, s * 0.9, s * 0.9)]; | |
cell.backgroundColor = [UIColor whiteColor]; | |
cell.textAlignment = NSTextAlignmentCenter; | |
[self.view addSubview:cell]; | |
[self.spreadSheet addObject:cell]; | |
} | |
} | |
- (void)createBit { | |
bit = { | |
0b000001, | |
0b000011, | |
0b000111, | |
0b001111, | |
0b011111, | |
0b111111 | |
}; | |
random_shuffle(bit.begin(), bit.end()); | |
int row = 0; | |
for (int a : bit) { | |
for (int i=0; i<6; i++) { | |
UILabel *cell = [self cellForRow:row col:i]; | |
cell.text = (a & 1 << i) ? @"1" : @"0"; | |
cell.backgroundColor = (a & 1 << i) ? [UIColor colorWithHue:0.6 saturation:0.1 brightness:1 alpha:1] : [UIColor colorWithHue:0.5 saturation:0.15 brightness:1 alpha:1]; | |
} | |
row++; | |
} | |
} | |
- (UILabel *)cellForRow:(int)r col:(int)c { | |
return self.spreadSheet[r * 6 + c]; | |
} | |
- (void)swapRow:(NSNotification *)note { | |
static int count = 0; | |
++count; | |
int a = [note.object[0] intValue]; | |
int b = [note.object[1] intValue]; | |
for (int i=0; i<6; i++) { | |
UIView *c1 = [self cellForRow:a col:i]; | |
UIView *c2 = [self cellForRow:b col:i]; | |
[self.spreadSheet exchangeObjectAtIndex:a * 6 + i withObjectAtIndex:b * 6 + i]; | |
CGPoint p1 = c1.center; | |
CGPoint p2 = c2.center; | |
[UIView animateWithDuration:0.2 delay:0.3 * count + 0.05 * i options:0 animations:^{ | |
c1.center = p2; | |
c2.center = p1; | |
} completion:nil]; | |
} | |
} | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
shared_ptr<MountainSorter> cppClass(new MountainSorter()); | |
cppClass->sort(bit); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=4333