473,396 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Suggestion for implementing : vector<uint12_t>

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;
}

Feb 19 '07 #1
11 4507
mathieu wrote:
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 ?
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
Feb 19 '07 #2
On Feb 18, 4:26 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
mathieu wrote:
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 ?

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>?
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;
};
Feb 19 '07 #3
Kaz Kylheku wrote:
On Feb 18, 4:26 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>mathieu wrote:
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 ?

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>?

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;
};
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
Feb 19 '07 #4
On Feb 18, 8:34 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Kaz Kylheku wrote:
On Feb 18, 4:26 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
mathieu wrote:
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 ?
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>?
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;
};

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.
Hi,

Sorry for the confusion. My goal is to provide a std::vector-like
interface to a 12bits unsigned int array (packed storage), see small
example attached to my first post. It's really the same problematic as
vector<bool>. In the end I should be able to write transparently.

std::vector<uint12_tv1 =
std::vector<uint16_tv2 =
std::copy(v1.begin(), v1.end(), v2.begin());

Actually in the end I need to support multiple 12bits input data,
such as (*). So again I am looking to provide an *interface* for my
users, so they can use all functions from <algorithm(std::fill,
std::transform...) on those different vectors. I have started
something like:
typedef uint16_t uint12_t;
// TODO: provide UINT12_MAX

template <typename TAlloc>
class vector<uint12_t, TAlloc>
{
std::vector<char, TAllocinternals;
public:
// copy everything from: http://www.sgi.com/tech/stl/Vector.html
};
Thanks,
-Mathieu
(*)
Bits Allocated = 16
Bits Stored = 12
High Bit = 11

|<------------------ pixel -----------------
>|
______________ ______________ ______________
______________
|XXXXXXXXXXXXXX| | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0

---------------------------

Bits Allocated = 16
Bits Stored = 12
High Bit = 15

|<------------------ pixel ----------------->|
______________ ______________ ______________
______________
| | | |
XXXXXXXXXXXXXX|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0

---------------------------

Bits Allocated = 12
Bits Stored = 12
High Bit = 11

------ 2 ----->|<------------------ pixel 1 ---------------
>|
______________ ______________ ______________
______________
| | | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0
-------------- 3 ------------>|<------------ 2
--------------
______________ ______________ ______________
______________
| | | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0
|<------------------ pixel 4 --------------->|<----- 3
------
______________ ______________ ______________
______________
| | | |
|
|______________|______________|______________|
______________|
15 12 11 8 7 4 3
0

See my previous post for implementation:
[ template and bit-field]
http://groups.google.com/group/comp....d7be13711933cb

Feb 19 '07 #5
* mathieu:
>
My goal is to provide a std::vector-like
interface to a 12bits unsigned int array (packed storage)
Pack and unpack.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 19 '07 #6
On Feb 18, 11:04 pm, "Alf P. Steinbach" <a...@start.nowrote:
* mathieu:
My goal is to provide a std::vector-like
interface to a 12bits unsigned int array (packed storage)

Pack and unpack.
The whole idea is that I am trying to use template programming so that
copying a std::vector<uint12_tinto a std::vector<uint12_tis simply
a memcpy. I should only resolve to pack/unpack when my type are
different (copying into vector<uint16_t>).
I do not believe this is a hard task (simply very tedious), but in the
end it should be extremely flexible. I thought I could reuse some
component (STL, boost) to avoid reinventing the wheel.

-Mathieu

Feb 19 '07 #7
On 18 Feb 2007 20:26:38 -0800, "mathieu" <ma***************@gmail.comwrote:
>On Feb 18, 11:04 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* mathieu:
My goal is to provide a std::vector-like
interface to a 12bits unsigned int array (packed storage)

Pack and unpack.

The whole idea is that I am trying to use template programming so that
copying a std::vector<uint12_tinto a std::vector<uint12_tis simply
a memcpy. I should only resolve to pack/unpack when my type are
different (copying into vector<uint16_t>).
I do not believe this is a hard task (simply very tedious), but in the
end it should be extremely flexible. I thought I could reuse some
component (STL, boost) to avoid reinventing the wheel.

-Mathieu
You could create a facade: specialize std::vector for your type, define it as
a class that implements the parts of the std::vector interface you're
interested in, and use a std::vector<unsigned charinternally for
implementation. Your iterators will then contain the necessary index increment
and decrement logic to keep the view consistent.

-dr
Feb 19 '07 #8
On Feb 19, 1:03 am, "mathieu" <mathieu.malate...@gmail.comwrote:
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
[snip]
You will not be able to reuse your vendors std::vector except in the
unlikely event that you happen to be in an environment where char has
12 bits. Thus, there is no way out but to create your own container
type (perhaps having a std::vector<charfor storage, but in exactly
your case, I do not believe that it would give you much).
I believe you also need to implement your own std::copy specialisation
if you must optimise copying between two uint_12 containers.

/Peter

Feb 19 '07 #9
s5n
On Feb 19, 5:26 am, "mathieu" <mathieu.malate...@gmail.comwrote:
The whole idea is that I am trying to use template programming so that
copying a std::vector<uint12_tinto a std::vector<uint12_tis simply
a memcpy. I should only resolve to pack/unpack when my type are
Keep in mind that memcpy works on BYTE addresses and boundaries, not
BIT boundaries, so you would have misalignment problems when using
memcpy. From the memcpy man page:

void *memcpy(void *dest, const void *src, size_t n);

The memcpy() function copies n bytes from memory area src
to memory area dest.

The key word there is "bytes".
Feb 19 '07 #10
On Feb 19, 6:32 am, "s5n" <step...@s11n.netwrote:
On Feb 19, 5:26 am, "mathieu" <mathieu.malate...@gmail.comwrote:
The whole idea is that I am trying to use template programming so that
copying a std::vector<uint12_tinto a std::vector<uint12_tis simply
a memcpy. I should only resolve to pack/unpack when my type are

Keep in mind that memcpy works on BYTE addresses and boundaries, not
BIT boundaries, so you would have misalignment problems when using
memcpy. From the memcpy man page:

void *memcpy(void *dest, const void *src, size_t n);

The memcpy() function copies n bytes from memory area src
to memory area dest.

The key word there is "bytes".
Very good point ! I will need an additional concept of 'size' is
multiple of two.

-M

Feb 19 '07 #11
On Feb 19, 4:32 am, "peter koch" <peter.koch.lar...@gmail.comwrote:
On Feb 19, 1:03 am, "mathieu" <mathieu.malate...@gmail.comwrote: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

[snip]
You will not be able to reuse your vendors std::vector except in the
unlikely event that you happen to be in an environment where char has
12 bits. Thus, there is no way out but to create your own container
type (perhaps having a std::vector<charfor storage, but in exactly
your case, I do not believe that it would give you much).
I believe you also need to implement your own std::copy specialisation
if you must optimise copying between two uint_12 containers.
Thanks, that's all I needed to know.

-M

Feb 19 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: WhiskyRomeo | last post by:
I need to duplicate something that is easy to do in Access using a recordset with its movenext, moveprevious, movefirst, movelast, and move(i) where i is a positive or negative integer. I do not...
2
by: reycri | last post by:
I have a .Net class (a collection) that already supports serialization. It implements ISerializable... Now, I need it to also support the COM interface IPersistStream. Among other things, I need...
3
by: Gunnar | last post by:
Hello. Problem: How can I select K random values from the elements 0,1,2,3,4...,N-1 ? I don't know if there's an easy way of doing this, but here are suggestions for doing it. One way is...
8
by: Ben | last post by:
Hi all, I implemented a stack in C++ in 2 different ways and I'd like to know which approach is better than the other.. and if there is any difference between the two? I'd also like to know if...
3
by: Sori Schwimmer | last post by:
0) Sorry, I don't know how to post a reply in the same thread. 1) Grant Edwards wrote: > The "i += 1" line is almost certainly wrong. You're certainly write, as I acknowledged in a follow up...
3
by: Br | last post by:
I'm going to go into a fair bit of detail as I'm hoping my methods may be of assistance to anyone else wanting to implement something similar (or totally confusing:) One of systems I've...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
3
by: Giulio Petrucci | last post by:
Hi there, I need to implement a light Http layer over a TCP/IP Socket as I have to create an application running on FW1.1 (so I cannot use the HttpListener class :-( ). Which approach would you...
0
by: Sengly | last post by:
Dear all, I am implementing a search engine for my website. I would like to seek your suggestion on re-ranking methodology. My problem is that I have a set of resulting documents to a query and...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.