HOG2
FourBitArray.cpp
Go to the documentation of this file.
1 //
2 // FourBitArray.cpp
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 4/8/13.
6 // Copyright (c) 2013 University of Denver. All rights reserved.
7 //
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include "FourBitArray.h"
12 #include <string.h>
13 #include <cinttypes>
14 
15 // uint8_t *mem;
16 // uint64_t entries;
17 
18 FourBitArray::FourBitArray(uint64_t numEntries)
19 {
20  mem = 0;
21  Resize(numEntries);
22 }
23 
25 {
26  delete [] mem;
27 }
28 
30 {
31  memset(mem, 0xFF, (entries+1)/2);
32 }
33 
35 {
36  memset(mem, 0x0, (entries+1)/2);
37 }
38 
39 void FourBitArray::Resize(uint64_t newMaxEntries)
40 {
41  entries = newMaxEntries; // 4 bit entries
42  newMaxEntries = (newMaxEntries+1)/2; // bytes
43  delete [] mem;
44  mem = new uint8_t[newMaxEntries];
45 }
46 
47 uint64_t FourBitArray::Size() const
48 {
49  return entries;
50 }
51 
52 uint8_t FourBitArray::Get(uint64_t index) const
53 {
54  uint8_t val = mem[index/2];
55  if (index&1)
56  {
57  return val>>4;
58  }
59  return val&0xF;
60 }
61 
62 void FourBitArray::Set(uint64_t index, uint8_t val)
63 {
64  if (index&1)
65  {
66  mem[index/2] = (mem[index/2]&0xF)|(val<<4);
67  }
68  else {
69  mem[index/2] = (mem[index/2]&0xF0)|val;
70  }
71 }
72 
73 void FourBitArray::Write(const char *file)
74 {
75  FILE *f = fopen(file, "w+");
76  fprintf(f, "%" PRId64 "\n", entries);
77  fwrite(mem, sizeof(uint8_t), (entries+1)/2, f);
78  fclose(f);
79 }
80 
81 void FourBitArray::Read(const char *file)
82 {
83  FILE *f = fopen(file, "r");
84  fscanf(f, "%" PRId64 "\n", &entries);
85  Resize(entries);
86  fread(mem, sizeof(uint8_t), (entries+1)/2, f);
87  fclose(f);
88 }
FourBitArray.h
FourBitArray::Size
uint64_t Size() const
Definition: FourBitArray.cpp:47
FourBitArray::Set
void Set(uint64_t index, uint8_t val)
Definition: FourBitArray.cpp:62
FourBitArray::Clear
void Clear()
Definition: FourBitArray.cpp:34
FourBitArray::Get
uint8_t Get(uint64_t index) const
Definition: FourBitArray.cpp:52
FourBitArray::Resize
void Resize(uint64_t newMaxEntries)
Definition: FourBitArray.cpp:39
FourBitArray::Write
void Write(const char *)
Definition: FourBitArray.cpp:73
FourBitArray::entries
uint64_t entries
Definition: FourBitArray.h:31
FourBitArray::mem
uint8_t * mem
Definition: FourBitArray.h:30
FourBitArray::FillMax
void FillMax()
Definition: FourBitArray.cpp:29
FourBitArray::Read
void Read(const char *)
Definition: FourBitArray.cpp:81
FourBitArray::~FourBitArray
~FourBitArray()
Definition: FourBitArray.cpp:24
FourBitArray::FourBitArray
FourBitArray(uint64_t numEntries=0)
Definition: FourBitArray.cpp:18