Pebble Coding

ソフトウェアエンジニアによるIT技術、数学の備忘録

2015-01-01から1年間の記事一覧

swift文字列走査

swiftで文字列の走査をする方法。 swiftは型に厳しいので、stringのsubscriptに指定する型はString.Indexである必要がある。 もっとも単純にやる場合、次のようになる。 var str = "Hello" var i:String.Index = str.startIndex // 0 var end:String.Index =…

swiftで使えるAtomic系API (OSX/iOS)

swiftで使えるAtomic系APIがあったので備忘録としてリストアップしておく。 OSX/iOSで確認した。 func OSAtomicAdd32(__theAmount: Int32, __theValue: UnsafeMutablePointer<Int32>) -> Int32 func OSAtomicAdd32Barrier(__theAmount: Int32, __theValue: UnsafeMu</int32>…

pthread_mutex_t と NSLockはどのくらい速度に違いがあるのか

2つのスレッドでウェイトを入れながら、一つの変数に排他アクセスをそれぞれ5000回行ったときの時間です。 MacOSX 10.10.4 MacBook Air (13-inch, Early 2015) @synchronized 0.092384 sec objc_sync_enter 0.090988 sec pthread_mutex_t 0.091280 sec NSLoc…

swiftで構造体またはクラスをArrayにする際に気をつけること(2)

class CPoint :Printable { var x:Int = 0 var y:Int = 0 init() {} var description:String { return "x[\(x)] y[\(y)]" } } struct SPoint :Printable { var x:Int = 0 var y:Int = 0 var description:String { return "x[\(x)] y[\(y)]" } } var v2:[SPoi…

swiftで構造体またはクラスをArrayにする際に気をつけること(1)

swiftでは構造体は値渡し、クラスは参照渡しと言われますが、実際に使ってみて失敗した方が理解が早いです。 以下のサンプルと実行結果を見てみます。 class CPoint :Printable { var x:Int = 0 var y:Int = 0 init() {} var description:String { return "x…

Swiftでビット演算判定

C言語でのビット演算後の判定処理と同じことをSwiftで書く場合、少し工夫がいるので、メモ。 int value1 = (1<<0); int value2 = (1<<0|1<<1); if( value1 & value2 ){ printf( "good!" ); } swiftではifの中身はBoolしか受け付けないのでこう書かなくてはい…

swift3で小数点値フォーマット

C言語での小数点値フォーマット指定をswiftで実現する方法を調べました。 C言語では小数点以下の幅を指定した場合、一つ下の桁が四捨五入されます。 C printf( "%.2f\n", 12.3456 ); // 12.35 printf( "%.2f\n", 12.0 ); // 12.00 swift let num = NSNumber(…

SwiftのEnumでクロージャを使う

スタンフォード大学 Paul Hegarty先生のDeveloping iOS 8 Apps with SwiftでSwiftを勉強していますが、難解なところがあったので、自分でソースを書いてみました。 import Cocoa class MyObject { enum Op { case Function2((Double, Double) -> Double) cas…

C++11のmove()でvectorの所有権を移動すると、元のvectorの要素数は0になる

いまいちC++11のmove()の動作が覚えられないので、動作サンプルを書き溜めて覚えたい。 // 検証環境: OSX 10.10.3 Xcode6.3.2 GNU++11 #include <utility> // move() #include <vector> using namespace std; struct JOB { int reward; }; vector<JOB> get_job(void) { vector<JOB> v_jo</job></job></vector></utility>…

C++11でのcondition_variableを使ったマルチスレッド制御

#include <thread> #include <mutex> #include <queue> #include <unistd.h> // usleep using namespace std; mutex print_mutex; // printf()呼び出し排他用 mutex queue_mutex; // v_queue排他アクセス用 queue<int> v_queue; // データキュー condition_variable ready_cond; // 条件変数 void </int></unistd.h></queue></mutex></thread>…

C++11でのスレッドの生成とmutexによるリソースアクセス排他処理

#include <thread> #include <mutex> using namespace std; mutex m; // printf()呼び出し排他用 void worker( void* p ) { int data = *(int*)p; { // printfはスレッドセーフではないので、2つのスレッドから排他的にアクセスする。 // lock_guardはlock()のようなメソッ</mutex></thread>…

C++ sorted vectorで範囲削除

sorted vectorに対し、lower_boundで指定値以上、以下のデータを削除するC++のサンプルです。 覚えられないのでメモ。 #include <vector> int main(int argc, const char * argv[]) { auto description = [](std::vector<int> v){ for( auto value : v ){ printf( "%d\n",</int></vector>…

MacVimのコンパイルに失敗する

vim

この手順でコンパイルしようとすると、configureコマンドで失敗する。 github.com エラーログの最後はこんな感じ。 checking for tgetent in -ltermlib... no checking for tgetent in -ltermcap... no checking for tgetent in -lcurses... no no terminal …

The executable was signed with invalid entitlements.

Distribution用にいろいろ作業していたら、なぜかテスト用デバイスに上記のエラーで転送できなくなった。 Provisioning ProfileがDebug,Release共通になっていたので、Releaseの方はDistribute用、Debugの方をXcodeによって自動生成されたものに置き換えたら…

no matching provisioning profiles found for appex com.security.application-groups

no matching provisioning profiles found for appex com.security.application-groups iOSアプリのvalidationで上のようなエラーが出て1時間ほどハマっていたので、メモ。 解決方法は、Apple DeveloperのProvisioning ProfileのDistributionの対応アプリの…

ActionController::RoutingError (No route matches [GET] "/assets/application-XXX

production環境でこんなエラーが出た。 rake assets:precompileはしてあるんだけどまだ何か足りないっぽい。 私の環境はnginx + unicorn + rails4なんだけど、 nginxではstaticルート設定してないので、rails側でserveする設定にしないといけないようだ。 co…

RubyMineからMacVimを起動する設定追加手順

Preference - Tools - External Tools で+ボタンを押す。 Nameに「MacVim」と入力する。 Programに「/Applications/mvim」と入れる。 Parametersに「 +$LineNumber$ $FilePath$」と入れる。 Working directoryに「/Applications」と入れてOKを押す。 これで…

NSString型のプロパティ

Objective-CでNSString型のプロパティににはstrong属性ではなく、copy属性を使った方がよいという記事をみかけますが、 私はデフォルトのstrongのまま使います。 copy属性を使った方がよい理由として、NSMutableStringの値を設定したときに、copy属性の方が…

OpenSSL::SSL::SSLError

railsアプリがなんか次のような500エラーを吐くようになった。 OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 state=SSLv3 read finished A: unexpected message) 外部のサーバにhttpsでアクセスしているところで出ているっぽい。 サーバのnginx.…

rails redirect_to model

redirect_to modelの形がよく出てくるけど、いまいちドキュメントに例が載ってなくて分からない。StackOverFlowに回答が載っていた。 what does redirect_to(@model) means in rails? - Stack Overflow @userがUserモデルのid=1のインスタンス変数の場合 red…

ruby文法その4

演算子の優先順位 (高) ! ~ ** * / % + - << >> & | ^ > >= < <= == != && || .. ... ?: = not and or (低) <と&&では<のほうが優先順位が高い。なので次の結果は納得できるだろう。 irb(main):001:0> num = 1 < 2 && 5 5 irb(main):001:0> num = 1 > 2 && 5…

ruby文法その3

前回のソースコードをもう少し見ていきます。 少し変更して次のようにしてみるとどうなるでしょうか? # MyMathModule.rb module MyMathModule def plus(x, y) x + y + (@something || 0) end end # MyApp.rb require './MyMathModule' class MyApp include …

ruby文法その2

Moduleの使い方。 ディレクトリにMyMathModule.rbとMyApp.rbの2つのファイルを作成してみる。 # MyMathModule.rb module MyMathModule def plus(x, y) x + y end end # MyApp.rb require "./MyMathModule" class MyApp include MyMathModule end myapp = My…

Xcode C++ link error

"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::at(unsigned long) const", referenced from: 上記のリンクエラーがでるときは、以下のライブラリを追加すればよい。 libc++.dylib</char></char,>

rbenvコマンドメモ

過去の記事と内容かぶっている気がするが、とりあえず書いとく。 インストールされているrubyのバージョンリスト表示 $ rbenv versions system 1.9.3-p484 * 2.0.0-p247 (set by /Users/pebble8888/.rbenv/version)

指定のrails バージョンをインストールする

例えばrails 4.0.0をインストールする場合 gem install rails -v 4.0.0

jQuery $(function() {...});とは何なのか

例えば、jQueryでこんなサンプルを見かけます。 この$(function() {... });のイディオムはなんために存在するのか? <body> <script> $(function() { $( "#selectable" ).selectable(); }); </script> <ol id="selectable"> </ol> </body> $()関数はドキュメントツリーが完成したタイミングで実行されるようです。 $()…

Yosemiteにアップデート後bundle updateがコケる

YosemiteにOSをアップデートしたあと、bundle updateしたら、 fatal error: 'string.h' file not found となりjsonのインストールがこける。 Cのコンパイルで失敗しているようだが、標準インクルードファイルのパスが通っていない模様。 以下のコマンドで解…

rails で各コントローラー毎のcss,jsを読み込むようにする。

ほぼ参照サイトままです。 やったことは、3つ app/views/layouts/application.html.erb に以下の2行を追加。 <%= stylesheet_link_tag controller.controller_name, media: "all" %> <%= javascript_include_tag controller.controller_name %> app/assets…

ruby 複数のクラスに共通のメソッドを追加する

共通のモジュールを作成し、使用したいクラスにMixInする。 action_util.rb module ActionUtil def shake(x) print( "shake #{x}!" ) end end cafe.rb require './action_util' // この./はrails内では必要ない class Cafe include ActionUtil def serve sha…