Skip to content

Commit 9eccef1

Browse files
committed
Revert "Revert "initial commit""
This reverts commit c6d2f76.
1 parent c6d2f76 commit 9eccef1

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

example_constant.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* C library computing the partial sums of a 2D matrix in O(m*n) time.
3+
* Query operations run in constant time.
4+
*
5+
* Author: Christopher W. Schankula
6+
* Last updated: December 16th, 2017
7+
* Code is licensed under the CC BY 4.0 license.
8+
*
9+
* THE CODE PROVIDED HEREIN IS PROVIDED "AS-IS"
10+
* THE AUTHOR ASSUMES NO RESPONSBILITY OR WARRANTY
11+
* FOR USE OF THE CODE PROVIDED HEREIN
12+
* EXTENSIVE TESTING OF THIS CODE IS REQUIRED
13+
* TO ENSURE ITS CORRECTNESS AS PART OF THE INTENDED
14+
* APPLICATION THEREOF
15+
*/
16+
17+
#include <stdio.h>
18+
#include <stdlib.h>
19+
#include "partialsums.h"
20+
21+
#define W 10
22+
#define H 30
23+
#define V 5
24+
25+
#define X1 0
26+
#define Y1 0
27+
#define X2 4
28+
#define Y2 4
29+
30+
int main(){
31+
unsigned int A[W*H];
32+
33+
for (int i = 0; i < W * H; i++)
34+
A[i] = V;
35+
36+
unsigned long int *res = generate_lookup(A, H, W);
37+
38+
printf("Original Matrix\n");
39+
for (int j = 0; j < H; j++){
40+
for (int i = 0; i < W; i++)
41+
printf("%5d", A[j*W + i]);
42+
printf("\n");
43+
}
44+
45+
printf("\n");
46+
47+
printf("Sum Lookup Matrix\n");
48+
for (int j = 0; j < H; j++){
49+
for (int i = 0; i < W; i++)
50+
printf("%5ld", res[j*W + i]);
51+
printf("\n");
52+
}
53+
printf("\n");
54+
55+
printf("Sum of region with size %d x %d: %ld\n", X2 - X1 + 1, Y2 - Y1 + 1, query_sum(res, W, X1, Y1, X2, Y2));
56+
57+
free(res);
58+
return 0;
59+
}

example_mean_filt.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* C library computing the partial sums of a 2D matrix in O(m*n) time.
3+
* Query operations run in constant time.
4+
*
5+
* Author: Christopher W. Schankula
6+
* Last updated: December 16th, 2017
7+
* Code is licensed under the CC BY 4.0 license.
8+
*
9+
* THE CODE PROVIDED HEREIN IS PROVIDED "AS-IS"
10+
* THE AUTHOR ASSUMES NO RESPONSBILITY OR WARRANTY
11+
* FOR USE OF THE CODE PROVIDED HEREIN
12+
* EXTENSIVE TESTING OF THIS CODE IS REQUIRED
13+
* TO ENSURE ITS CORRECTNESS AS PART OF THE INTENDED
14+
* APPLICATION THEREOF
15+
*/
16+
17+
#include <stdio.h>
18+
#include "partialsums.h"
19+
#include <stdlib.h>
20+
21+
#define H 8
22+
#define W 8
23+
#define S 3
24+
25+
unsigned int picture[64] =
26+
{
27+
255,255,255,255,255,255,255,255
28+
,255,255,255,255,255,255,255,255
29+
,255, 0 ,255,255,255,255, 0 ,255
30+
,255,255,255,255,255,255,255,255
31+
,255,255,255,255,255,255,255,255
32+
,255, 0 ,255,255,255,255, 0 ,255
33+
,255,255, 0 , 0 , 0 , 0 ,255,255
34+
,255,255,255,255,255,255,255,255
35+
};
36+
37+
int main(){
38+
unsigned long int *sum_lookup = generate_lookup(picture, 8, 8);
39+
unsigned int *new_picture = malloc(H * W * sizeof(int));
40+
41+
printf("Original picture\n");
42+
for (int j = 0; j < H; j++){
43+
for (int i = 0; i < W; i++)
44+
printf("%5d", picture[j*W + i]);
45+
printf("\n");
46+
}
47+
48+
for (int j = 0; j < H; j++){
49+
for (int i = 0; i < W; i++){
50+
/* Handle edge cases */
51+
int x1 = i - S / 2 > 0 ? i - S / 2 : 0;
52+
int y1 = j - S / 2 > 0 ? j - S / 2 : 0;
53+
int x2 = i + S / 2 < W ? i + S / 2 : W - 1;
54+
int y2 = j + S / 2 < H ? j + S / 2 : H - 1;
55+
56+
*(new_picture + j * W + i) = query_sum(sum_lookup, W, x1, y1, x2, y2) / ((x2 - x1 + 1) * (y2 - y1 + 1));
57+
}
58+
}
59+
60+
printf("New picture\n");
61+
for (int j = 0; j < H; j++){
62+
for (int i = 0; i < W; i++)
63+
printf("%5d", new_picture[j*W + i]);
64+
printf("\n");
65+
}
66+
67+
free(new_picture);
68+
free(sum_lookup);
69+
return 0;
70+
}

makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CFLAGS = -Wall -O2 -std=c99 -pedantic
2+
all: example_constant example_mean_filt
3+
4+
example_constant: example_constant.o partialsums.o
5+
$(CC) $(CFLAGS) -o $@ $?
6+
7+
example_mean_filt: example_mean_filt.o partialsums.o
8+
$(CC) $(CFLAGS) -o $@ $?
9+
10+
c: clean
11+
12+
clean:
13+
rm *.o example_constant example_mean_filt

partialsums.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* C library computing the partial sums of a 2D matrix in O(m*n) time.
3+
* Query operations run in constant time.
4+
*
5+
* Author: Christopher W. Schankula
6+
* Last updated: December 16th, 2017
7+
* Code is licensed under the CC BY 4.0 license.
8+
*
9+
* THE CODE PROVIDED HEREIN IS PROVIDED "AS-IS"
10+
* THE AUTHOR ASSUMES NO RESPONSBILITY OR WARRANTY
11+
* FOR USE OF THE CODE PROVIDED HEREIN
12+
* EXTENSIVE TESTING OF THIS CODE IS REQUIRED
13+
* TO ENSURE ITS CORRECTNESS AS PART OF THE INTENDED
14+
* APPLICATION THEREOF
15+
*/
16+
17+
#include <stdlib.h>
18+
#include <stdio.h>
19+
#include "partialsums.h"
20+
21+
/*
22+
* Generates the lookup table for a given matrix with m rows and n columns.
23+
* Adapted from user rolfl's O(n) algorithm:
24+
* https://codereview.stackexchange.com/questions/42906/partial-sums-of-two-dimensional-array
25+
*/
26+
unsigned long int *generate_lookup(unsigned int *A, int m, int n){
27+
unsigned long int *B = malloc(m*n*sizeof(unsigned long int));
28+
for (int j = 0; j < m; j++){
29+
for (int i = 0; i < n; i++){
30+
unsigned long int abovesum = j > 0 ? *(B + (j - 1) * n + i) : 0;
31+
unsigned long int leftsum = i > 0 ? *(B + j * n + i - 1) : 0;
32+
unsigned long int aboveleftsum = (i > 0 && j > 0) ? *(B + (j - 1) * n + i - 1) : 0;
33+
int val = *(A + j * n + i);
34+
35+
*(B + j * n + i) = val
36+
+ aboveleftsum
37+
+ (leftsum - aboveleftsum)
38+
+ (abovesum - aboveleftsum);
39+
}
40+
}
41+
return B;
42+
}
43+
44+
/*
45+
* Queries a 2D partial-sum lookup table, returning the sum of the area enclosed by
46+
* the rectangle (x1,y1,x2,y2) where (x1,y1) is the top-left corner and (x2,y2) is the
47+
* bottom-right corner.
48+
*
49+
*/
50+
unsigned long int query_sum(unsigned long int *A, int n, int x1, int y1, int x2, int y2){
51+
unsigned long int r1 = *(A + y2 * n + x2);
52+
unsigned long int r2 = x1 > 0 ? *(A + y2 * n + x1 - 1) : 0;
53+
unsigned long int r3 = y1 > 0 ? *(A + (y1 - 1) * n + x2) : 0;
54+
unsigned long int r4 = x1 > 0 && y1 > 0 ? * (A + (y1 - 1) * n + x1 - 1) : 0;
55+
return r1 - r2 - r3 + r4;
56+
}

partialsums.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* C library computing the partial sums of a 2D matrix in O(m*n) time.
3+
* Query operations run in constant time.
4+
*
5+
* Author: Christopher W. Schankula
6+
* Last updated: December 16th, 2017
7+
* Code is licensed under the CC BY 4.0 license.
8+
*
9+
* THE CODE PROVIDED HEREIN IS PROVIDED "AS-IS"
10+
* THE AUTHOR ASSUMES NO RESPONSBILITY OR WARRANTY
11+
* FOR USE OF THE CODE PROVIDED HEREIN
12+
* EXTENSIVE TESTING OF THIS CODE IS REQUIRED
13+
* TO ENSURE ITS CORRECTNESS AS PART OF THE INTENDED
14+
* APPLICATION THEREOF
15+
*/
16+
17+
unsigned long int *generate_lookup(unsigned int *A, int m, int n);
18+
unsigned long int query_sum(unsigned long int *A, int n, int x1, int y1, int x2, int y2);

0 commit comments

Comments
 (0)