通过删除重复项来生成新的integer_sequence

如何解决通过删除重复项来生成新的integer_sequence

我实现了编译时检查,以检查是否使用下面给出的代码对某些内容进行了排序:

template<typename IntegerSequence>
struct is_sorted {
    static constexpr bool value = true;
};

template<typename Integer,Integer Head,Integer Next,Integer... Tail>
struct is_sorted<std::integer_sequence<Integer,Head,Next,Tail...>> {
    static constexpr bool value = Head <= Next && is_sorted<std::integer_sequence<Integer,Tail...>>::value;
};

上面的代码有效。我打算用这些排序检查创建另外两个元函数,这些函数将生成没有重复的新序列

using in_seq = std::integer_sequence<int,1,2,3,4>;

using mod_seq = is_sorted<in_seq>::value ? remove_duplicates<in_seq>::uniq_seq : in_seq;
// Examples
// in_seq = 1,4 -> mod_seq = 1,4
// in_seq = 1,4

如何在编译时使用模板从整数序列中删除重复项。 也可以在执行排序检查时删除重复项,在这种情况下,如果我们在模板检测到序列未排序后立即停止删除重复项,那很好。

// partial sort example 4,4,5,1 -> 4,1 (not sure if this is possible,but just curious)

我不确定如何动态生成新的std::integer_sequence

解决方法

由于所有std算法现在在C ++ 20中都是constexpr,因此我们可以使用它以自然方式进行编译时编程(就像@cigien这样说):

template <typename T,T... Ints>
constexpr auto unique_until_nonsorted(std::integer_sequence<T,Ints...>) {
  // constexpr structured bindings are not allow :(
  constexpr auto pair = [] {
    std::array<T,sizeof...(Ints)> arr{Ints...};
    // get last iterator of unique
    auto sorted_end = std::is_sorted_until(arr.begin(),arr.end());
    // unique until last iterator
    auto unique_end = std::unique(arr.begin(),sorted_end);
    // copy nonsorted elements to last iterator
    auto copy_end = std::copy(sorted_end,arr.end(),unique_end);
    // get final arr size
    auto size = std::distance(arr.begin(),copy_end);
    return std::pair{arr,size};
  }();
  constexpr auto arr  = pair.first;
  constexpr auto size = pair.second;
  // using template lambda to expand pack
  return [&arr]<std::size_t... Is>(std::index_sequence<Is...>) {
    return std::integer_sequence<T,arr[Is]...>{};
  }(std::make_index_sequence<size>{});
}    

Works for GCC and Clang.

template <typename X,X... Xs,typename Y,Y... Ys>
constexpr bool operator==(std::integer_sequence<X,Xs...>,std::integer_sequence<Y,Ys...>) noexcept {
  return ((Xs == Ys) && ...);
}

static_assert(unique_until_nonsorted(std::index_sequence<4,4,5,3,2,1>{}) ==
                                     std::index_sequence<4,1>{});
static_assert(unique_until_nonsorted(std::index_sequence<1,4>{}) == 
                                     std::index_sequence<1,4>{});
static_assert(unique_until_nonsorted(std::index_sequence<1,4>{});
static_assert(unique_until_nonsorted(std::index_sequence<2,1,1>{}) == 
                                     std::index_sequence<2,2>{}) == 
                                     std::index_sequence<1,2>{});
// corner case
static_assert(unique_until_nonsorted(std::index_sequence<>{}) == 
                                     std::index_sequence<>{});
,

在执行排序检查时也可以删除重复项,在这种情况下,如果我们在模板检测到序列未排序后立即停止删除重复项,那很好。

是的,有可能。

也许可以简化一些,但是...使用和滥用模板专业化...

给出如下的帮助器类

// csardh: check sort and remove duplicates helper
template <typename T,typename>
struct csardh
 { using type = T; };

template <typename T,T ... ordered,T v0,T v1,T ... vs>
struct csardh<std::integer_sequence<T,ordered...>,std::integer_sequence<T,v0,v1,vs...>>
   : public std::conditional_t<
        (v0 < v1),csardh<std::integer_sequence<T,ordered...,v0>,vs...>>,vs...>,void>>
 { };

template <typename T,vs...>>
   : public csardh<std::integer_sequence<T,vs...>>
 { };

template <typename T,T v0>
struct csardh<std::integer_sequence<T,v0>>
   : public csardh<std::integer_sequence<T,void>
 { };

主要班级成为

// csard: check sort and remove duplicates
template <typename>
struct csard;

template <typename T,T... vals>
struct csard<std::integer_sequence<T,vals...>>
   : public csardh<std::integer_sequence<T>,vals...>>
 { };

using可以简化使用

template <typename T>
using csard_t = typename csard<T>::type;

以下是完整的编译示例(C ++ 14足够)

#include <utility>
#include <type_traits>

// csardh: check sort and remove duplicates helper
template <typename T,void>
 { };

// csard: check sort and remove duplicates
template <typename>
struct csard;

template <typename T,vals...>>
 { };

template <typename T>
using csard_t = typename csard<T>::type;

int main()
 {
   using T0 = std::integer_sequence<int,1>;
   using T1 = csard_t<T0>;
   using T2 = std::integer_sequence<int,1>;

   static_assert( std::is_same<T1,T2>::value,"!" );

   using U0 = std::integer_sequence<int,4>;
   using U1 = csard_t<U0>;
   using U2 = U0;

   static_assert( std::is_same<U1,U2>::value,"!" );

   using V0 = std::integer_sequence<int,4>;
   using V1 = csard_t<V0>;
   using V2 = U2;

   static_assert( std::is_same<V1,V2>::value,"!" );
 }
,

让我们使用Boost.Mp11进行此操作。此处,“整数序列”的概念略有不同,我们处理类型列表。 mp_list_c<int,2>是类型mp_list<mp_int<1>,mp_int<2>>。这使许多元编程变得更加容易。这里的一切都是单线的。

首先,is_sorted。 Mp11(震惊器)当前缺少的一种算法是能够执行zip_with的功能-也就是说,获取一个列表并压缩列表,使其自身偏移一个:

template <typename L,template <typename...> class P>
using zip_with = mp_transform<P,mp_pop_back<L>,mp_pop_front<L>>;

现在,is_sorted基本上是:获取该列表,并检查每对中第一个元素是否少于第二个元素:

template <typename L>
using is_sorted = mp_rename<zip_with<L,mp_less>,mp_all>;

要删除重复项?那就是已经有一种算法用于mp_unique

完全放入

using mod_seq = mp_if_c<is_sorted<L>::value,mp_unique<L>,L>;

没有类型的条件运算符,您需要类似mp_if(或std::conditional_t)的东西。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-