实施建议:vector< uint12_t> [英] Suggestion for implementing : vector<uint12_t>

查看:67
本文介绍了实施建议:vector< uint12_t>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想实现一个''vector< uint12_t>''结构,其中

uint12_t是一个12位无符号整数。 AFAIK我需要完全重复
复制执行让说矢量< booland适应

代码以满足我的需要。我无法找出是否有办法让我这么做?
重用我的供应商std :: vector?


建议欢迎,

Mathieu


#include< iostream>


int main(int,char * [])

{

//让我们假装那些是6 uint12_t值:

// 0x012 0x345 0x678 0x9ab

const unsigned char in [] = {0x01,0x23,0x45,0x67,0x89,0xab};

//将它们存储在16位:

// 0x0012 0x0345 0x0678 0x09ab

const unsigned int Nin = sizeof(in); // sizeof(unsigned char)== 1

const unsigned int Nout = Nin * 2/3;

unsigned short out [Nout];

const unsigned char * p_in = in;

for(unsigned short * p_out = out; p_out!= out + Nout; / * p_out + = 2 * /)

{

const unsigned char b0 = * p_in ++;

const unsigned char b1 = * p_in ++;

const unsigned char b2 = * p_in ++;

* p_out ++ =((b0> 4)<< 8)+((b0& 0x0f)<<<<<<"<<<<"<<<<<" b $ b * p_out ++ =((b1& 0x0f)<< 8)+((b2> 4)<<<<<<<<<<"<<<<<<"<<<<<<"<<<"


for(unsigned int i = 0; i< Nout; ++ i)

std :: cout<< std :: hex<< out [i]<< " " ;;

std :: cout<< std :: endl;


返回0;

}

Hello,

I would like to implement a ''vector<uint12_t>'' structure, where
uint12_t is a 12bits unsigned integer. AFAIK I need to completely
duplicate the implementation of let say vector<booland adapt the
code to suit my need. I cannot find out if there is way for me to
reuse my vendor std::vector ?

Suggestions welcome,
Mathieu

#include <iostream>

int main(int, char *[])
{
// Let''s pretend for a moment those are 6 uint12_t values:
// 0x012 0x345 0x678 0x9ab
const unsigned char in[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab };
// store them on 16bits:
// 0x0012 0x0345 0x0678 0x09ab
const unsigned int Nin = sizeof(in); // sizeof(unsigned char) == 1
const unsigned int Nout = Nin * 2 / 3;
unsigned short out[Nout];
const unsigned char *p_in = in;
for(unsigned short *p_out = out; p_out != out + Nout; /*p_out+=2*/)
{
const unsigned char b0 = *p_in++;
const unsigned char b1 = *p_in++;
const unsigned char b2 = *p_in++;
*p_out++ = ((b0 >4) << 8) + ((b0 & 0x0f) << 4) + (b1 >4) ;
*p_out++ = ((b1 & 0x0f) << 8) + ((b2 >4) << 4) + (b2 & 0x0f);
}

for(unsigned int i=0; i < Nout; ++i)
std::cout << std::hex << out[i] << " ";
std::cout << std::endl;

return 0;
}

推荐答案

mathieu写道:
mathieu wrote:

你好,


我想实现一个''vector< uint12_t>' '结构,其中

uint12_t是一个12位无符号整数。 AFAIK我需要完全重复
复制执行让说矢量< booland适应

代码以满足我的需要。我无法找出是否有办法让我能够重新使用我的供应商std :: vector?
Hello,

I would like to implement a ''vector<uint12_t>'' structure, where
uint12_t is a 12bits unsigned integer. AFAIK I need to completely
duplicate the implementation of let say vector<booland adapt the
code to suit my need. I cannot find out if there is way for me to
reuse my vendor std::vector ?



我还不明白这些要求。因此,让我一两个问题:


a)你有一个uint12_t,还是必须实现?


b)如果你确实有uint12_t,那么

std :: vector< uint12_t>?

Best


Kai-Uwe Bux

I do not yet understand the requirements. Thus, let me just as two
questions:

a) Do you have a uint12_t, or would that have to be implemented, too?

b) If you do have uint12_t already, what is wrong with
std::vector<uint12_t>?
Best

Kai-Uwe Bux


2月18日下午4:26,Kai-Uwe Bux< jkherci ... @ gmx.netwrote:
On Feb 18, 4:26 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

mathieu写道:
mathieu wrote:

你好,
Hello,


我想实现一个''vector< uint12_t>''结构,其中

uint12_t是一个12位无符号整数。 AFAIK我需要完全重复
复制执行让说矢量< booland适应

代码以满足我的需要。我无法找出是否有办法让我能够重新使用我的供应商std :: vector?
I would like to implement a ''vector<uint12_t>'' structure, where
uint12_t is a 12bits unsigned integer. AFAIK I need to completely
duplicate the implementation of let say vector<booland adapt the
code to suit my need. I cannot find out if there is way for me to
reuse my vendor std::vector ?



我还不明白这些要求。因此,让我一两个问题:


a)你有一个uint12_t,还是必须实现?


b)如果你确实有uint12_t,那么

std :: vector< uint12_t>?


I do not yet understand the requirements. Thus, let me just as two
questions:

a) Do you have a uint12_t, or would that have to be implemented, too?

b) If you do have uint12_t already, what is wrong with
std::vector<uint12_t>?



可能与天真实现的

向量< bool>错误相同,即每个元素占用sizeof(bool)存储量。


如果uint12_t存在,则不会在数组中提供打包存储,因为需要将每个uint12_t与可寻址边界对齐,所以
。 br />

//在数组中浪费至少四位:

struct uint12_t {

无符号值:12;

};

Probably the same thing that is wrong with a naively implemented
vector<bool>, namely taking up sizeof (bool) storage per element.

A uint12_t, if it existed, would not provide packed storage in arrays,
due to the need to align each uint12_t to an addressable boundary.

// wastes at least four bits in arrays:
struct uint12_t {
unsigned value : 12;
};


Kaz Kylheku写道:
Kaz Kylheku wrote:

2月18日下午4:26,Kai -Uwe Bux< jkherci ... @ gmx.netwrote:
On Feb 18, 4:26 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

> mathieu写道:
>mathieu wrote:

你好,
Hello,


我想实现一个''vector< uint12_t>''结构,其中

uint12_t是12位无符号整数。 AFAIK我需要完全重复
复制执行让说矢量< booland适应

代码以满足我的需要。我无法找出是否有办法让我能够重新使用我的供应商std :: vector?
I would like to implement a ''vector<uint12_t>'' structure, where
uint12_t is a 12bits unsigned integer. AFAIK I need to completely
duplicate the implementation of let say vector<booland adapt the
code to suit my need. I cannot find out if there is way for me to
reuse my vendor std::vector ?


我还不了解这些要求。因此,让我问两个问题:

a)你有uint12_t,还是必须实施?

b)如果你确实有uint12_t,
std :: vector< uint12_t> ;?有什么问题?


I do not yet understand the requirements. Thus, let me just as two
questions:

a) Do you have a uint12_t, or would that have to be implemented, too?

b) If you do have uint12_t already, what is wrong with
std::vector<uint12_t>?



可能与天真实现的

向量< bool>错误相同,即每个元素占用sizeof(bool)存储。


如果uint12_t存在,则不会在数组中提供打包存储,因为需要将每个uint12_t与可寻址边界对齐,所以
。 br />

//在数组中浪费至少四位:

struct uint12_t {

无符号值:12;

};


Probably the same thing that is wrong with a naively implemented
vector<bool>, namely taking up sizeof (bool) storage per element.

A uint12_t, if it existed, would not provide packed storage in arrays,
due to the need to align each uint12_t to an addressable boundary.

// wastes at least four bits in arrays:
struct uint12_t {
unsigned value : 12;
};



如果是25%的开销,我认为除非测量显示空间消耗,否则不值得花费b / b
是不可接受的。保持

介意实现专业化向量< uint12_t是一个权衡:你需要交易时间(对于程序员和CPU)的空间。除非有令人信服的理由,否则我会选择更简单的版本。


另一方面,我可以想象有需要这些位在内存中是连续的,例如,如果这是做一些花哨的图形的一部分

的东西。在那种情况下,别无选择。


由于这些问题不明确,我只想通过OP来澄清。

Best


Kai-Uwe Bux

If it is the 25% overhead, I would argue that it''s not worth the effort
unless measurement shows that space consumption is innacceptable. Keep in
mind that realizing a specialization vector<uint12_tis a tradeoff: you
trade time (for both the programmer and the CPU) for space. Unless there is
a compelling reason not to, I would go with the simpler version.

On the other hand, I could imagine that there is a need for the bits to be
contiguous in memory, e.g., if this is part of doing some fancy graphics
stuff. In that case, there would be no choice.

Since those issues are not clear, I just like the OP to clarify.
Best

Kai-Uwe Bux


这篇关于实施建议:vector&lt; uint12_t&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆