OSDN Git Service

51e5aeaea430265f6af050f2dadc7b0618743b57
[simplecms/utakata.git] / src / type.cpp
1 #include <algorithm>
2 #include <string>
3 #include "src/type.h"
4
5 namespace type = utakata::type;
6
7 // 宣言のコメントを参照してください。
8 type::Type::Type(const std::string& str) : hash_(0), hash_string_(str) {
9   MakeHash();
10 }
11
12 // 宣言のコメントを参照してください。
13 type::Type::Type(const type::Type& t) : hash_(t.hash_),
14                                         hash_string_(t.hash_string_) {}
15
16 // 宣言のコメントを参照してください。
17 type::Type& type::Type::operator=(const type::Type& p) {
18   type::Type tmp(p);
19   Swap(tmp);
20   return *this;
21 }
22
23 // ここでのハッシュ値は、次のようなアルゴリズムで生成されます。
24 // 渡された文字列 s のn文字目の文字をSnと表したとき、
25 // hash = (S1 + !(S1 << 4)) + (S2 + !(S2 << 4))...(Sn + !(Sn << 4))
26 // とした結果のハッシュ値となります。
27 void type::Type::MakeHash() {
28   std::string::const_iterator beg = hash_string_.begin(),
29       end = hash_string_.end();
30
31   for (; beg != end; ++beg) {
32     // 各文字毎に簡単なハッシュ値を算出する。
33     hash_ += (*beg + (!(*beg << 4)));
34   }
35 }
36
37 // 宣言のコメントを参照してください。
38 void type::Type::Swap(type::Type& type) {
39   std::swap(hash_, type.hash_);
40   std::swap(hash_string_, type.hash_string_);
41 }