定义
类型列表,字面意思就是一个存储类型的列表,例如std::tuple
就是一个类型列表。
templatetypename ...Ts> struct type_list {};
基础操作
操作约束:对于所有操作,均要求参数合法,即要求type_list
中至少有一个类型,或者提供的下标不越界。
-
is_empty
-
front
-
pop_front
-
push_front
-
push_back
-
back
-
pop_back
-
reverse
-
largest(支持自定义compare)
-
merge
-
insert
is_empty
templatetypename Ts>
struct is_empty_impl : std::integral_constantbool, false> {};
template>
struct is_empty_impltype_list>> : std::integral_constantbool, true> {};
templatetypename Ts>
constexpr static bool is_empty = is_empty_implTs>::value;
Front
templatetypename T> struct front_impl {};
templatetypename T, typename ...Ts>
struct front_impltype_listT, Ts...>> {
using type = T;
};
templatetypename T> using front = front_implT>::type;
Pop front
templatetypename T> struct pop_front_impl {};
templatetypename T, typename ...Ts>
struct pop_front_impltype_listT, Ts...>> {
using type = type_listTs...>;
};
templatetypename T> using pop_front = pop_front_implT>::type;
Push front
templatetypename Tl, typename T> struct push_front_impl {};
templatetypename ...Ts, typename T>
struct push_front_impltype_listTs...>, T> {
using type = type_listT, Ts...>;
};
templatetypename Tl, typename T>
using push_front = push_front_implTl, T>::type;
Push back
templatetypename Tl, typename T> struct push_back_impl {};
templatetypename ...Ts, typename T>
struct push_back_impltype_listTs...>, T> {
using type = type_listTs..., T>;
};
templatetypename Tl, typename T>
using push_back = push_back_implTl, T>::type;
Back
templatetypename Tl> struct back_impl {};
templatetypename T, typename ...Ts>
struct back_impltype_listT, Ts...>> : back_impltype_listTs...>> {};
templatetypename T>
struct back_impltype_listT>> { using type = T; };
templatetypename Tl> using back = back_implTl>::type;
Pop back
templatetypename Tl> struct pop_back_impl {};
templatetypename T, typename ...Ts>
struct pop_back_impltype_listT, Ts...>>
: push_front_impltypename pop_back_impltype_listTs...>>::type, T> {};
templatetypename T>
struct pop_back_impltype_listT>> { using type = type_list>; };
templatetypename Tl> using pop_back = pop_back_implTl>::type;
Reverse
templatetypename Tl, bool empty = is_emptyTl>> struct reverse_impl {};
templatetypename Tl>
struct reverse_implTl, false>
: push_back_impltypename reverse_implpop_frontTl>>::type, frontTl>> {};
templatetypename Tl>
struct reverse_implTl, true> { using type = type_list>; };
templatetypename Tl>
using reverse = reverse_implTl>::type;
Largest (可接收自定义类型比较函数:参考compare实现)
templatebool judge, typename T1, typename T2>
struct type_choose {
using type = T2;
};
templatetypename T1, typename T2>
struct type_choosetrue, T1, T2> {
using type = T1;
};
templatetypename T1, typename T2>
struct compare : std::integral_constantbool, false> {};
templatetypename T1, typename T2>
requires (sizeof(T1) > sizeof(T2))
struct compareT1, T2> : std::integral_constantbool, true> {};
templatetypename Tl, templatetypename ...> typename C> struct largest_impl {};
templatetypename T, templatetypename ...> typename C>
struct largest_impltype_listT>, C> {
using type = T;
};
templatetypename T, typename ...Ts, templatetypename ...> typename C>
struct largest_impltype_listT, Ts...>, C>
: type_chooseCT, typename largest_impltype_listTs...>, C>::type>::value,
T, typename largest_impltype_listTs...>, C>::type> {};
templatetypename Tl, templatetypename ...> typename C>
using largest = largest_implTl, C>::type;
merge
templatetypename Tl1, typename Tl2> struct merge_impl {};
templatetypename ...Ts1, typename ...Ts2>
struct merge_impltype_listTs1...>, type_listTs2...>> {
using type = type_listTs1..., Ts2...>;
};
templatetypename Tl1, typename Tl2>
using merge = merge_implTl1, Tl2>::type;
insert
两个子模板会在insert_impl, 0, T>
sizeof...(Ts) > 0
的时候冲突,所以加上一个requires
约束一下。
templatetypename Tl, int index, typename T> struct insert_impl {};
templatetypename Tf, typename ...Ts, int index, typename T>
requires (index > 0)
struc服务器托管网t insert_impltype_listTf, Ts...>, index, T>
: push_front_impltypename insert_impltype_listTs...>, index - 1, T>::type, Tf> {};
templatetypename ...Ts, typename T>
struct insert_impltype_listTs...>, 0, T>
: push_front_impltype_listTs...>, T> {};
templatetypename Tl, int index, typename T>
using insert = insert_implTl, index, T>::type;
TEST
符合TDD,简单测试一下:
int main() {
// is empty: 1 0 0
std::cout "is empty: ";
std::cout is_emptytype_list>> " "
is_emptytype_listint>> " "
is_emptytype_listint, float, double>> "nn";
// front: 1 0
std::cout "front: ";
std::cout std::is_same_vint, fronttype_listint, float>>> " "
std::is_same_vfloat, fronttype_listint, float>>> "nn";
// pop front: 1 0 1
std::cout "pop front: ";
std::cout std::is_same_vtype_list>, pop_fronttype_listint>>> " "
std::is_same_vtype_listint>, pop_fronttype_listint>>> " "
std::is_same_vtype_listint>, pop_fronttype_listfloat, int>>> "nn";
// push front: 1 0 1
std::cout "push front: ";
std::cout std::is_same_vtype_listint>, push_fronttype_list>, int>> " "
std::is_same_vtype_listint, float>, push_fronttype_listint>, float>> " "
std::is_same_vtype_listfloat, int>, push_fronttype_listint>, float>> "nn";
// push back: 1 1 0
std::cout "push back: ";
std::cout std::is_same_vtype_listint>, push_backtype_list>, int>> " "
std::is_same_vtype_listint, float>, push_backtype_listint>, float>> " "
std::is_same_vtype_listfloat, int>, push_backtype_listint>, float>> "nn";
// back: 1 0 1
std::cout "back: ";
std::cout std::is_same_vint, backtype_listint>>> " "
std::is_same_vint, backtype_listint, float>>> " "
std::is_same_vfloat, backtype_listint, float>>> "nn";
// pop back: 1 1 0
std::cout "pop back: ";
std::cout std::is_same_vtype_list>, pop_backtype_listint>>> " "
std::is_same_vtype_listfloat>, pop_backtype_listfloat, int>>> " "
std::is_same_vtype_listint>, pop_backtype_listfloat, int>>> "nn";
// reverse: 1 0 1 1
std::cout "reverse: ";
std::cout std::is_same_vtype_list>, reversetype_list>>> " "
std::is_same_vtype_listint, float>, reversetype_listint, float>>> " "
std::is_same_vtype_listfloat, int>, reversetype_listint, float>>> " "
std::is_same_vtype_listint, float, double>, reversetype_listdouble, float, int>>> "nn";
// largest: 1, 0, 1
// char, short, int32_t, int64_t, double
std::cout sizeof(char) " " sizeof(short) " " sizeof(int32_t)
" " sizeof(int64_t) " " sizeof(double) "n";
using type1 = type_listchar, short, int32_t, int64_t, double>;
using type2 = type_listchar, short, int32_t, double, int64_t>;
std::cout "largest: ";
std::cout std::is_same_vdouble, largesttype1, compare>> " "
std::is_same_vdouble, largesttype2, compare>> " "
std::is_same_vint64_t, largesttype2, compare>> "nn";
// merge: 1 1 1 0
std::cout "merge: ";
std::cout std::is_same_vtype_listint>, mergetype_list>, type_listint>>> " "
std::is_same_vtype_listint>, mergetype_listint>, type_list>>> " "
std::is_same_vtype_listint, float>, mergetype_listint>, type_listfloat>>> " "
std::is_same_vtype_listint, float>, mergetype_listfloat>, type_listint>>> "nn";
// insert: 1 1 1
std::cout "insert: ";
std::cout std::is_same_vtype_listint>, inserttype_list>, 0, int>> " "
std::is_same_vtype_listint, float, double>, inserttype_listint, double>, 1, float>> " "
std::is_same_vtype_listint, float, double>, inserttype_listint, float>, 2, double>> "n";
return 0;
}
Algorithm
Insert sort
维护尾部的有序性,每次加入一个新值,同时保证尾部的有效性,那么先实现一个insert_in_sorted, T>
- 找到第一个满足
C
的位置,并且在这个值前面插入
templatetypename Tl, typename T, templatetypename ...> typename C,
bool empty = is_emptyTl>>
struct insert服务器托管网_in_sorted_impl {};
templatetypename ...Ts, typename T, templatetypename ...> typename C>
struct insert_in_sorted_impltype_listTs...>, T, C, false>
: type_choose(Cfronttype_listTs...>>, T>::value),
push_fronttype_listTs...>, T>,
push_front
typename insert_in_sorted_implpop_fronttype_listTs...>>, T, C>::type,
fronttype_listTs...>>>
> {};
templatetypename ...Ts, typename T, templatetypename ...> typename C>
struct insert_in_sorted_impltype_listTs...>, T, C, true> {
using type = type_listT>;
};
templatetypename Tl, typename T, templatetypename ...> typename C>
using insert_in_sorted = insert_in_sorted_implTl, T, C>::type;
- 排序实现
templatetypename Tl, templatetypename ...> typename C,
bool empty = is_emptyTl>> struct insert_sort_impl {};
templatetypename ...Ts, templatetypename ...> typename C>
struct insert_sort_impltype_listTs...>, C, true> {
using type = type_list>;
};
templatetypename ...Ts, templatetypename ...> typename C>
struct insert_sort_impltype_listTs...>, C, false>
: insert_in_sorted_impl
typename insert_sort_implpop_fronttype_listTs...>>, C>::type,
fronttype_listTs...>>, C> {};
templatetypename Tl, templatetypename ...> typename C>
using insert_sort = insert_sort_implTl, C>::type;
- 测试一下结果对不对
int main() {
// insert in sorted: 1 1 1 1 1
std::cout "insert in sorted: ";
std::cout std::is_same_vtype_listint32_t>, insert_in_sortedtype_list>, int32_t , compare>> " "
std::is_same_vtype_listchar, short, int32_t, int64_t>, insert_in_sortedtype_listchar, short, int32_t>, int64_t, compare>> " "
std::is_same_vtype_listchar, short, int32_t, int64_t>, insert_in_sortedtype_listchar, short, int64_t>, int32_t, compare>> " "
std::is_same_vtype_listchar, short, int32_t, float, int64_t>, insert_in_sortedtype_listchar, short, int32_t, int64_t>, float, compare>> " "
std::is_same_vtype_listchar, int32_t, long long, float, int64_t>, insert_in_sortedtype_listchar, long long, float, int64_t>, int32_t, compare>> "n";
// insert sort: 1 1 1 1
std::cout "insert sort: ";
std::cout std::is_same_vtype_list>, insert_sorttype_list>, compare>> " "
std::is_same_vtype_listchar>, insert_sorttype_listchar>, compare>> " "
std::is_same_vtype_listfloat, int>, insert_sorttype_listint, float>, compare>> " ";
// 非稳定排序,相同大小的类型,初始在前面的会跑到后面去
using type_list_sort = type_listshort int, char, short, int, long long, unsigned int, unsigned, unsigned long long, unsigned char>;
using type_list_sort_ans = type_listunsigned char, char, short, short int, unsigned, unsigned int, int, unsigned long long, long long>;
std::cout std::is_same_vtype_list_sort_ans, insert_sorttype_list_sort, compare>> "n";
return 0;
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
一、复制文件夹到Content内 二、读取文件内容,直接使用相对路径就可以了/Content,Resource Bundle存储文件夹名的变量。Load Text为自定义的读取json文件的方法,我之前的文章讲了怎么操作。 ue5读取外部文件_艺菲的博客-CS…