Skip to content

Commit 35ed1b3

Browse files
committed
C++/EqualRange: Add lowerBound, upperBound; loop invarant\
1 parent 766091b commit 35ed1b3

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

C++/EqualRange/main.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "pch.h"
2+
3+
#include <assert.h>
4+
5+
#include <functional>
6+
7+
template<typename IterT, typename T>
8+
IterT lowerBound(IterT begin, IterT end, const T& val) {
9+
IterT last = end - 1;
10+
if (val <= *begin) return begin;
11+
if (val > *last) return end;
12+
13+
while (last - begin > 1) {
14+
IterT mid = begin + (last - begin) / 2;
15+
if (val > *mid) begin = mid;
16+
else last = mid;
17+
}
18+
19+
return last;
20+
}
21+
template<typename IterT, typename T>
22+
IterT upperBound(IterT begin, IterT end, const T& val) {
23+
IterT last = end - 1;
24+
if (val < *begin) return begin;
25+
if (val >= *last) return end;
26+
27+
while (last - begin > 1) {
28+
IterT mid = begin + (last - begin) / 2;
29+
if (val >= *mid) begin = mid;
30+
else last = mid;
31+
}
32+
33+
return last;
34+
}
35+
36+
#define COUNT(a) (sizeof(a)/sizeof((a)[0]))
37+
38+
int main() {
39+
int a1[] = {3, };
40+
int a2[] = {3, 4, 9, 9, 9, 9, };
41+
int a3[] = {2, 2, 5, 7, 8, 10, 10, };
42+
int a4[] = {1, 4, 5, 5, 5, 6, 9, 10};
43+
44+
for (int i = -2; i < 12; ++i) {
45+
assert(lowerBound(a1, a1 + COUNT(a1), i) == lower_bound(a1, a1 + COUNT(a1), i));
46+
assert(lowerBound(a2, a2 + COUNT(a2), i) == lower_bound(a2, a2 + COUNT(a2), i));
47+
assert(lowerBound(a3, a3 + COUNT(a3), i) == lower_bound(a3, a3 + COUNT(a3), i));
48+
assert(lowerBound(a4, a4 + COUNT(a4), i) == lower_bound(a4, a4 + COUNT(a4), i));
49+
50+
assert(upperBound(a1, a1 + COUNT(a1), i) == upper_bound(a1, a1 + COUNT(a1), i));
51+
assert(upperBound(a2, a2 + COUNT(a2), i) == upper_bound(a2, a2 + COUNT(a2), i));
52+
assert(upperBound(a3, a3 + COUNT(a3), i) == upper_bound(a3, a3 + COUNT(a3), i));
53+
assert(upperBound(a4, a4 + COUNT(a4), i) == upper_bound(a4, a4 + COUNT(a4), i));
54+
}
55+
}

C++/EqualRange/makefile

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
.PHONY: all build rebuild rel_build rel_rebuild clean update_pch build_actions clean_actions
2+
all: build
3+
4+
include makefile_var
5+
6+
build_type = debug
7+
8+
CXX =g++
9+
CXXFLAGS += -MMD
10+
CXXFLAGS += -Wall
11+
CXXFLAGS += $(foreach i,$(macro_defs),-D $(i))
12+
CXXFLAGS += $(foreach i,$(include_dirs),-I '$(i)')
13+
CXXFLAGS += $(foreach i,$(lib_dirs),-L '$(i)')
14+
CXXFLAGS += $(foreach i,$(lib_files),-l '$(i)')
15+
ifeq ($(use_cpp0x),1)
16+
CXXFLAGS += -std=c++0x
17+
endif
18+
ifeq ($(build_dll),1)
19+
CXXFLAGS += -shared -fPIC
20+
endif
21+
ifeq ($(build_type),debug)
22+
CXXFLAGS += -g
23+
else
24+
CXXFLAGS += -O3 -DNDEBUG
25+
endif
26+
ifeq ($(do_profiling),1)
27+
CXXFLAGS += -pg
28+
endif
29+
30+
srcs = $(wildcard *.cpp)
31+
objs = $(srcs:.cpp=.o)
32+
all_deps = $(srcs:.cpp=.d)
33+
exist_deps = $(wildcard *.d)
34+
not_exist_deps = $(filter-out $(exist_deps), $(all_deps))
35+
pch_file =pch.h
36+
37+
build: build_actions update_pch main
38+
rebuild: clean
39+
$(MAKE) build
40+
rel_build:
41+
$(MAKE) build -e build_type=release
42+
rel_rebuild: clean
43+
$(MAKE) rel_build
44+
clean: clean_actions
45+
rm -f main $(objs) $(all_deps) $(pch_file).gch gmon.out
46+
update_pch: $(pch_file).gch
47+
48+
main: $(objs)
49+
$(CXX) -o $@ $(objs) $(CXXFLAGS)
50+
51+
$(pch_file).gch: $(pch_file)
52+
$(CXX) $(filter-out -MMD,$(CXXFLAGS)) $<
53+
54+
ifneq ($(exist_deps),)
55+
include $(exist_deps)
56+
endif
57+
ifneq ($(not_exist_deps),)
58+
$(not_exist_deps:.d=.o):%.o:%.cpp
59+
endif

C++/EqualRange/makefile_var

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
do_profiling=0
2+
use_cpp0x=1
3+
build_dll=0
4+
macro_defs=
5+
include_dirs=
6+
lib_dirs=
7+
lib_files=
8+
9+
.PHONY:
10+
11+
build_actions:
12+
13+
clean_actions:
14+

C++/EqualRange/pch.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef PCH_H
2+
#define PCH_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include <memory.h>
8+
#include <time.h>
9+
#include <assert.h>
10+
11+
#include <iostream>
12+
#include <vector>
13+
#include <string>
14+
#include <algorithm>
15+
using namespace std;
16+
17+
#endif

0 commit comments

Comments
 (0)