473,756 Members | 6,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4594
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<uin t12_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<uin t12_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<ui nt12_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_t is 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<uin t12_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_t is 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<uin t12_tv1 =
std::vector<uin t16_tv2 =
std::copy(v1.be gin(), 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<cha r, 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.now rote:
* 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<uin t12_tinto a std::vector<uin t12_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.comwr ote:
>On Feb 18, 11:04 pm, "Alf P. Steinbach" <a...@start.now rote:
>* 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<uin t12_tinto a std::vector<uin t12_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<uns igned 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.comwr ote:
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<cha rfor 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.comwr ote:
The whole idea is that I am trying to use template programming so that
copying a std::vector<uin t12_tinto a std::vector<uin t12_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

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

Similar topics

5
1679
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 know how to do this in .NET. None of the existing data structures dataset, arraylist, etc support this type of access. I need pull in the photos in a particular order and show 3 at a time in 3 images boxes. The user has First, Next, Previous,...
2
1977
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 to be able to pass instances of this class to methods of queued components (in COM+). I want to "piggy back" my implementation of IPersistStream to what is already being used for serialization. So, in the Save() method of IPersistStream, the...
3
1809
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 to select K random elements (using a rand()% N), and then see if any number was choosen twice, and then re-select the duplicats until the
8
2740
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 the destructor i'm using is correct.. I got segmentation fault in my second approach while I quit the program. I appreciate any help.... My first appoach goes like this:
3
335
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 "suggestion for (re)try statement - correction' 2) Rocco Morreti wrote: > What is so repugnant about the equivalent, currently valid way of writing it? Nothing "repugnant". We have in almost all procedural
3
3305
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 developed has three levels of security. Admins - can see all records Manager - can only see records based on an organisation structure held in a table (simple tree structure) Employee - can only see own records
4
3334
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, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
3
2867
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 use? 1) The socket reads all the data; after that it converts the read bytes into a string andd create an HttpRequest (designed ad hoc to parse the request data that my client will send); 2) An HttpRequest class is created and the Socket is...
0
899
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 each one of them with a matching score and also a list of relatedness score between each two of them. I would like to re-rank my resulting documents by downgrading the score of those near duplicate results. I would like to know whether there...
2
2250
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 more code, but all the "checking the root" code would be separated out in the tree class. The node class would be very smooth. I'll try this when I have
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.