Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

varint.cc

Go to the documentation of this file.
00001 //***************************************************************************
00002 // This source code is copyrighted 2002 by Google Inc.  All rights
00003 // reserved.  You are given a limited license to use this source code for
00004 // purposes of participating in the Google programming contest.  If you
00005 // choose to use or distribute the source code for any other purpose, you
00006 // must either (1) first obtain written approval from Google, or (2)
00007 // prominently display the foregoing copyright notice and the following
00008 // warranty and liability disclaimer on each copy used or distributed.
00009 // 
00010 // The source code and repository (the "Software") is provided "AS IS",
00011 // with no warranty, express or implied, including but not limited to the
00012 // implied warranties of merchantability and fitness for a particular
00013 // use.  In no event shall Google Inc. be liable for any damages, direct
00014 // or indirect, even if advised of the possibility of such damages.
00015 //***************************************************************************
00016 
00017 
00018 #include "varint.h"
00019 
00020 char* Varint::Encode32(char* ptr, uint32 v) {
00021   static const int B = 128;
00022   if (v < (1<<7)) {
00023     *(ptr++) = v;
00024   } else if (v < (1<<14)) {
00025     *(ptr++) = v | B;
00026     *(ptr++) = v>>7;
00027   } else if (v < (1<<21)) {
00028     *(ptr++) = v | B;
00029     *(ptr++) = (v>>7) | B;
00030     *(ptr++) = v>>14;
00031   } else if (v < (1<<28)) {
00032     *(ptr++) = v | B;
00033     *(ptr++) = (v>>7) | B;
00034     *(ptr++) = (v>>14) | B;
00035     *(ptr++) = v>>21;
00036   } else {
00037     *(ptr++) = v | B;
00038     *(ptr++) = (v>>7) | B;
00039     *(ptr++) = (v>>14) | B;
00040     *(ptr++) = (v>>21) | B;
00041     *(ptr++) = v>>28;
00042   }
00043   return ptr;
00044 }
00045 
00046 char* Varint::Encode64(char* ptr, uint64 v) {
00047   static const int B = 128;
00048   do {
00049     // Encode next 7 bits + terminator bit
00050     int bits = (((int) v) & 127);
00051     v >>= 7;
00052     *(ptr++) = static_cast<unsigned char>(bits + ((v != 0) ? B : 0));
00053   } while (v != 0);
00054   return ptr;
00055 }

Generated on Wed May 29 11:37:16 2002 for MarkovPR by doxygen1.2.15