partition_point

本文介绍如何使用partition_point算法在已分组序列中找到分组边界。通过示例代码展示其用法,返回不符合条件的第一个元素位置。

partition_point用于在已经分好组的序列中,查找分组的分界点,返回不满足条件的分组中的第一个元素的位置。

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool comp(int i) { return i < 5; }

int main()
{
    vector<int> v{ 1,3,2,4,5,9,8,7,6 };

    auto iter = partition_point(v.begin(), v.end(), comp);

    for (auto it = v.begin(); it < iter; it++)
        cout << *it << "->";
    cout << endl;

    for (auto it = iter; it < v.end(); it++)
        cout << *it << "->";
    cout << endl;
     
    return 0;
}

输出结果:

1->3->2->4->
5->9->8->7->6->

分析从代码 ################################## # # author:liqianning@tp-link.com.hk # describe:拓展slp openwrt lib # ################################## get_raw_16 = $(shell echo $(1)|sed "s/0x//g") cal_sub_16 = `echo "obase=16;ibase=16;$(subst 0x,,$(1))-$(subst 0x,,$(2))"|bc` cal_add_16 = `echo "obase=16;ibase=16;$(subst 0x,,$(1))+$(subst 0x,,$(2))"|bc` cal_div_16 = `echo "obase=16;ibase=16;$(subst 0x,,$(1))/$(subst 0x,,$(2))"|bc` text_cut = cut_num=$(firstword $(shell cat $1|grep -n $2|awk -F ":" '{print $$1}')); \ cat $1|awk -v line_num=$$cut_num 'NR<line_num{print}' > $3 text_upper = $(shell echo $1|tr a-z A-Z) check_file_exit = $(shell if [ -e $1 ];then echo y;fi) check_file_exit_cmd = if [ -e $1 ];then \ $2;\ fi add_front_addx = $(2)_$(1) add_end_addx = $(1)_$(2) cal_16_to_10 = `echo $(1)|sed 's/0x//g'|xargs -i echo 'obase=10;ibase=16;{}'|bc` cal_10_to_16 = `echo $(1)|sed 's/0x//g'|xargs -i echo 'obase=16;ibase=10;{}'|bc` add_num_10 = `echo "$(1) + $(2)"|bc` define SAFE_COPY ifdef $(2) $(CP) $1 $(BIN_DIR)/build_fw_dir/$($(2)).bin endif endef define mini_install mkdir -p $(STAGING_DIR)/mini_compile $(CP) $1 $(STAGING_DIR)/mini_compile/ endef # 读dir/suffix_dir的mk define INCLUDE_MUTI_MK $(eval $(foreach _dir,$1,include $(_dir)/$(2)$(notdir $(_dir)))) endef read_file_len = `ls -l|grep $(1)|awk '{print $5}'` out_include_file:=partition_table.h build_pad_bin = dd if=$(1) of=$(2) ibs=$(3) obs=$(BLOCK_SIZE) conv=sync build_raw_bin = dd if=$(1) of=$(2) bs=$(BLOCK_SIZE) conv=sync ifeq ($(DUMP_OUT),) fix_size=$(if $1,$1,0x0) fix_name=$(if $2,$2,$1) define SAFE_COPY ifdef $(2) $(CP) $1 $(BIN_DIR)/build_fw_dir/$($(2)).bin endif endef define _ADD_TO_TABLE partition_table_list += partition_table_$1 partition_table_size += $(call fix_size,$2) partition_table_$1 = $(call fix_size,$2) partition_table_valid_size += $(call fix_size,$4) partition_table_version += $(call fix_size,$3) endef define _ADD_UBI_VOL ubi_$1 += ubi_create_vol_$1_$2 partition_ubi_list += ubi_$1 partition_ubivol_list += partition_ubivol_$1_$2 partition_ubivol_size += $(call fix_size,$3) partition_ubivol_valid_size += $(call fix_size,$5) partition_ubivol_version += $(call fix_size,$4) partition_ubivol_$1_$2_size = $(call fix_size,$3) partition_ubivol_$1_$2_valid_size = $(call fix_size,$5) partition_ubivol_$1_$2_file = $(call fix_name,$1_$2,$6).bin endef define _REGISTER_TO_FLASH ifeq ($1,pt_table) is_pt_table_flash:=1 endif ifeq ($1,pt_table_restore) is_pt_table_restore_flash:=1 endif partition_flash_$1_file = $(call fix_name,$1,$2)$(if $(filter pt_table%,$1),_flash,).bin partition_flash_list += partition_flash_$1 order_flash_length += $(partition_table_$1) endef define _REGISTER_TO_UPBOOT ifeq ($1,pt_table) is_pt_table_up:=1 endif partition_upboot_list += partition_upboot_$1 partition_upboot_$1_file = $(call fix_name,$1,$2)$(if $(filter pt_table%,$1),_upboot,).bin ifneq ($(3),) partition_upboot_ex_pack_list += $1 partition_upboot_ex_pack_type_list += $3_DMIC endif endef define _CHECK_PARTITION partition_check_list += partition_check_$1 partition_check_$1_file = $(call fix_name,$1,$2).bin endef define _FORBID_CHANGE partition_forbid_list += $1 endef define _NEED_DYNAMIC_FS partition_dynamic_fs_list += $1 endef define _PARTITION_MAP_SET map_list += partition_map_$1 partition_map_$1: echo \#define partition_map_$1 \"$2\" >> $(out_include_file) endef define _EXPORT_PARTITION partition_export_$2_name =$1 partition_export_$2_len =$(partition_table_$1) endef define ADD_TO_TABLE $(eval $(call _ADD_TO_TABLE,$1,$2,$3,$4)) endef define ADD_UBI_VOL $(eval $(call _ADD_UBI_VOL,$1,$2,$3,$4,$5,$6)) endef define REGISTER_TO_FLASH $(eval $(call _REGISTER_TO_FLASH,$1,$2)) endef define REGISTER_TO_UPBOOT $(eval $(call _REGISTER_TO_UPBOOT,$1,$2,$3,$4)) endef define CHECK_PARTITION $(eval $(call _CHECK_PARTITION,$1,$2)) endef define NEED_DYNAMIC_FS $(eval $(call _NEED_DYNAMIC_FS,$1)) endef define PARTITION_MAP_SET $(eval $(call _PARTITION_MAP_SET,$1,$2)) endef define FORBID_OP $(eval $(call _FORBID_CHANGE,$1)) endef define EXPORT_PARTITION $(eval $(call _EXPORT_PARTITION,$1,$2)) endef define create_ubi $1:$($1) $$(foreach vol,$($1),$$(shell if [ -f $$(vol) ];then cat $$(vol) >> $$@;fi)) endef else fix_empty=$(strip $1) point="," partition_dynamic_fs_list:=$(foreach table,$(partition_table_list), \ $(if $(ubi_$(patsubst partition_table_%,%,$(table))), \ $(foreach ubi_vol,$(ubi_$(patsubst partition_table_%,%,$(table))),\ $(if $(filter $(patsubst ubi_create_vol_%,%,$(ubi_vol)),$(partition_dynamic_fs_list)),\ $(patsubst ubi_create_vol_%,%,$(ubi_vol)),$(empty))),\ $(if $(filter $(patsubst partition_table_%,%,$(table)),$(partition_dynamic_fs_list)),\ $(patsubst partition_table_%,%,$(table)),$(empty)))) partition_table_list:=$(call fix_empty,$(partition_table_list)) partition_table_size:=$(call fix_empty,$(partition_table_size)) partition_ubivol_list:=$(call fix_empty,$(partition_ubivol_list)) partition_check_list:=$(call fix_empty,$(partition_check_list)) partition_ubivol_size:=$(call fix_empty,$(partition_ubivol_size)) partition_flash_list:=$(call fix_empty,$(partition_flash_list)) partition_upboot_list:=$(call fix_empty,$(partition_upboot_list)) partition_dynamic_fs_list:=$(call fix_empty,$(partition_dynamic_fs_list)) ifeq ($(is_pt_table_flash),) $(error must have pt_table partition in flash) endif ifeq ($(is_pt_table_restore_flash),) $(error must have pt_table_restore partition in flash) endif ifeq ($(is_pt_table_up),) $(error must have pt_table partition in up) endif partition_ubi_list:=$(sort $(partition_ubi_list)) $(foreach ubi,$(partition_ubi_list),$(eval $(call create_ubi,$(ubi)))) partition_ubivol_size:=$(subst $(empty) $(empty),$(point),$(partition_ubivol_size)) partition_table_size:=$(subst $(empty) $(empty),$(point),$(partition_table_size)) partition_table_%: table=$(patsubst partition_table_%,%,$@); \ echo \#define $@ \"$${table}\" >> $(out_include_file) partition_table_finish: echo \#define partition_table_list \{$(subst $(empty) $(empty),$(point),$(partition_table_list))\,NULL\} >> $(out_include_file) echo \#define partition_table_size \{$(partition_table_size)\} >> $(out_include_file) echo \#define partition_table_valid_size \{$(subst $(empty) $(empty),$(point),$(partition_table_valid_size))\} >> $(out_include_file) echo \#define partition_table_version \{$(subst $(empty) $(empty),$(point),$(partition_table_version))\} >> $(out_include_file) echo >> $(out_include_file) partition_flash_%: flash=$(partition_flash_$(patsubst partition_flash_%,%,$@)_file); \ echo \#define $@ \"$${flash}\" >> $(out_include_file) partition_flash_finish: echo \#define partition_flash_list \{$(subst $(empty) $(empty),$(point),$(partition_flash_list))\,NULL\} >> $(out_include_file) echo \#define partition_flash_name \{$(foreach flash,$(partition_flash_list),\"$(patsubst partition_flash_%,%,$(flash))\",)NULL\} >> $(out_include_file) echo >> $(out_include_file) partition_upboot_%: upboot=$(partition_upboot_$(patsubst partition_upboot_%,%,$@)_file); \ echo \#define $@ \"$${upboot}\" >> $(out_include_file) partition_upboot_finish: echo \#define partition_upboot_list \{$(subst $(empty) $(empty),$(point),$(partition_upboot_list))\,NULL\} >> $(out_include_file) echo \#define partition_upboot_name \{$(foreach ubi_vol,$(partition_upboot_list),\"$(patsubst partition_upboot_%,%,$(ubi_vol))\",)NULL\} >> $(out_include_file) echo >> $(out_include_file) partition_ubivol_finish: echo \#define partition_ubivol_list \{$(foreach ubi_vol,$(partition_ubivol_list),\"$(patsubst partition_ubivol_%,%,$(ubi_vol))\",)NULL\} >> $(out_include_file) echo \#define partition_ubivol_file_list \{$(foreach ubi_vol,$(partition_ubivol_list),\"$($(ubi_vol)_file)\",)NULL\} >> $(out_include_file) echo \#define partition_ubivol_size \{$(partition_ubivol_size)\} >> $(out_include_file) echo \#define partition_ubivol_valid_size \{$(subst $(empty) $(empty),$(point),$(partition_ubivol_valid_size))\} >> $(out_include_file) echo \#define partition_ubivol_version \{$(subst $(empty) $(empty),$(point),$(partition_ubivol_version))\} >> $(out_include_file) echo >> $(out_include_file) partition_check_finish: echo \#define partition_check_name \{$(foreach check,$(partition_check_list),\"$(patsubst partition_check_%,%,$(check))\",)NULL\} >> $(out_include_file) echo >> $(out_include_file) partition_upboot_ex_pack_finish: echo \#define partition_expack_list \{$(foreach expack,$(partition_upboot_ex_pack_list),\"$(expack)\",)NULL\} >> $(out_include_file) echo \#define partition_expack_type_list \{$(subst $(empty) $(empty),$(point),$(partition_upboot_ex_pack_type_list))\} >> $(out_include_file) echo >> $(out_include_file) ubi_create_vol_%: ubi_vol=$(patsubst ubi_create_vol_%,%,$@); \ ubi_id=$(shell ls -l|grep ubi_create_vol_$(firstword $(subst _, ,$(patsubst ubi_create_vol_%,%,$@)))|wc -l); \ echo \[$${ubi_vol}\] >> $@; \ echo mode=ubi >> $@; \ if [ -f $(partition_ubivol_$(patsubst ubi_create_vol_%,%,$@)_file) ];then \ echo image=$(partition_ubivol_$(patsubst ubi_create_vol_%,%,$@)_file) >> $@; else \ echo image=empty.bin >> $@;fi;\ echo vol_size=$(partition_ubivol_$(patsubst ubi_create_vol_%,%,$@)_size) >> $@; \ echo vol_type=dynamic >> $@; \ echo vol_alignment=1 >> $@; \ echo vol_name=$${ubi_vol} >> $@; \ echo vol_id=$${ubi_id} >> $@; \ echo >> $@; pre_create: rm -rf ubi_* rm -rf $(out_include_file) touch $(out_include_file) echo \#ifndef __PARTITION_TABLE_H_ >> $(out_include_file) echo \#define __PARTITION_TABLE_H_ >> $(out_include_file) echo >> $(out_include_file) $(out_include_file): pre_create $(partition_table_list) partition_table_finish \ $(partition_flash_list) partition_flash_finish \ $(partition_upboot_list) partition_upboot_finish partition_ubivol_finish partition_check_finish $(map_list) partition_upboot_ex_pack_finish echo \#define partition_dynamic_fs_list \{$(foreach fs,$(partition_dynamic_fs_list),\"$(fs)\",)NULL\} >> $@ echo \#define partition_forbid_list \{$(foreach fd,$(partition_forbid_list),\"$(fd)\",)NULL\} >> $@ echo \#define partition_table_num $(words $(partition_table_list)) >> $@ echo \#define FLASH_PAGE_SIZE $(FLASH_PAGE_SIZE) >> $@ echo \#define factory_info_max_size $(FAC_INFO_MAX_SIZE) >> $@ echo >> $@ echo \#define FLASH_UPDATE_MEM_ADDR $(call cal_16_to_10,$(FLASH_UPDATE_MEM_ADDR)) >> $@ echo \#define FLASH_MAX_SIZE $(FLASH_MAX_SIZE) >> $@ echo \#define FLASH_BLOCK_SIZE $(U_FLASH_BLOCK_SIZE) >> $@ echo \#endif >> $@ $(call text_cut,$(input_file),auto_cut,device_info) define table_auto_build all:$1 endef define make_ubi all:$(partition_ubi_list) $(foreach ubi,$(partition_ubi_list),if [ -f $(ubi) ];then ubinize -m $(FLASH_PAGE_SIZE) -p `echo "obase=10;ibase=16;$(subst 0x,,$(U_FLASH_BLOCK_SIZE))/$(subst 0x,,0x400)"|bc`KiB -o $(patsubst ubi_%,%,$(ubi)).bin $(ubi);fi;) endef endif
最新发布
09-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值