473,769 Members | 2,143 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
11 4596
On Feb 19, 6:32 am, "s5n" <step...@s11n.n etwrote:
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".
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.comwr ote:
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.
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
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
1979
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
1811
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
2741
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
2868
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
900
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
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9990
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
8869
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
6668
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
5297
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.