File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
3
+ struct DetectSquares {
4
+ points : Vec < ( i32 , i32 ) > ,
5
+ counts : HashMap < ( i32 , i32 ) , i32 >
6
+ }
7
+
8
+ impl DetectSquares {
9
+
10
+ fn new ( ) -> Self {
11
+ Self {
12
+ points : vec ! [ ] ,
13
+ counts : HashMap :: new ( )
14
+ }
15
+ }
16
+
17
+ fn add ( & mut self , point : Vec < i32 > ) {
18
+ let p = ( point[ 0 ] , point[ 1 ] ) ;
19
+ self . points . push ( p) ;
20
+ * self . counts . entry ( p) . or_default ( ) += 1 ;
21
+ }
22
+
23
+ fn count ( & self , point : Vec < i32 > ) -> i32 {
24
+ let mut res = 0 ;
25
+ let ( px, py) = ( point[ 0 ] , point[ 1 ] ) ;
26
+ for ( x, y) in self . points . iter ( ) {
27
+ if ( py - y) . abs ( ) != ( px - x) . abs ( ) || * x == px || * y == py {
28
+ continue ;
29
+ }
30
+ res += self . counts . get ( & ( * x, py) ) . unwrap_or ( & 0 ) * self . counts . get ( & ( px, * y) ) . unwrap_or ( & 0 ) ;
31
+ }
32
+
33
+ res
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments