土曜日, 11月 16, 2013

C++ STL - Partition関数

STL algorithmのpartition関数は特定の要素をリスト中で仕分けできる便利な関数なので覚えておくとよいことが色々あるかも.以下の例はファイル名のリスト中jpegファイル名の要素とそうでない要素を分類する例.

#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;

static vector<string> JPEG_POSTFIX;

void init() {

 JPEG_POSTFIX.push_back(".jpg");
 JPEG_POSTFIX.push_back(".jpeg");

}

bool IsJpeg(const string& s) {
        // should use boost's to_lower, though!
 vector<string>::iterator iter;
 for(iter = JPEG_POSTFIX.begin();iter != JPEG_POSTFIX.end();iter++ ) {
  if(s.find(*iter) != string::npos)
   return true;
 }
 return false;

}

int main() {

 init();
 istringstream f("neko.jpg tako.jpeg ika.png rakko.gif fugu.jpg");
 vector<string> fileList;
 string s;
 while(getline(f,s,' ')) {
  fileList.push_back(s);
 }
 cout << "## before partitioning.." << is_partitioned( fileList.begin(),fileList.end(),IsJpeg ) << endl;
 for_each(fileList.begin(),fileList.end(),[](string& s){cout << s << " "; });
 partition(fileList.begin(),fileList.end(),IsJpeg);
 cout << endl << "## after partitioning.." << is_partitioned( fileList.begin(),fileList.end(),IsJpeg ) << endl;
 for_each(fileList.begin(),fileList.end(),[](string& s){cout << s << " "; });
 cout << endl;

 return 0;

}


月曜日, 11月 11, 2013

Java - StrSubstitutor

Apache Commons langからまた一つ便利なクラスのご紹介をさせていただきたい.
StrSubstitutorクラスは名前の通り文字列中のキーワード変換を行う.キーワードのプレフィックス・ポストフィックスの変更並びに変換後の値中のキーワードに対しての変換もサポートしている優れものである.この車輪の再発明はしないのが賢明だろう.

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.text.StrSubstitutor;
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;

public class StrSubstTest {

 @Test
 public void testSubst() {
  
  final String TEST_STR = "${PATTERN} cat";
  final String EXPECTED_STR = "black cat";
  
  StrSubstitutor subst = new StrSubstitutor( prepareValueMap() );
  assertThat( subst.replace( TEST_STR ), is( EXPECTED_STR ) );
  
 }
 
 @Test
 public void testSubstWithPrefix() {
  
  final String TEST_STR = "<<PATTERN>> cat";
  final String EXPECTED_STR = "black cat";
  
  StrSubstitutor subst = new StrSubstitutor( prepareValueMap(), "<<", ">>" );
  assertThat( subst.replace( TEST_STR ), is( EXPECTED_STR ) );
  
 }
 
 @Test
 public void testSubstInVariable() {
  
  final String TEST_STR = "${PATTERN} cat";
  final String EXPECTED_STR = "calico(white,black,brown) cat";
  
  StrSubstitutor subst = new StrSubstitutor( prepareValueMap2() );
  subst.setEnableSubstitutionInVariables( true );
  assertThat( subst.replace( TEST_STR ), is( EXPECTED_STR ) );
  
 }
 
 private Map<String,String> prepareValueMap() {
  
  Map<String,String> map = new HashMap<String,String>();
  map.put( "PATTERN", "black" );
  map.put( "SIZE", "big" );
  return map;
  
 }
 
 private Map<String,String> prepareValueMap2() {
  
  Map<String,String> map = new HashMap<String,String>();
  map.put( "PATTERN", "calico(${COLORS})" );
  map.put( "COLORS", "white,black,brown" );
  return map;
  
 }

}